Autostart Problem...
-
Hi,
ich würde gerne erreichen das mein Programm bei jedem Computerstart gestartet wird allerdings funktioniert das nicht so wies soll.void SetUpAutostart( void ) { char myPath[256]; GetModuleFileName( NULL, myPath, 256 ); HKEY hKey; HKEY KEY = HKEY_LOCAL_MACHINE; char strPlace[100] = {"Software\\Microsoft\\Windows\\CurrentVersion\\Run"}; char strName[100] = {"MyApp"}; RegOpenKeyEx( KEY, (LPCTSTR)strPlace, 0, KEY_ALL_ACCESS, &hKey ); RegSetValueEx( hKey, strName, 0, REG_SZ, (BYTE *)myPath, strlen(myPath) ); RegCloseKey( hKey ); }Das Problem ist der Eintrag wird erstellt, aber mitstarten tut das Programm trotzdem nicht. Ich habe den Code aus meinem alten Projekt übernommen dort hat es geklappt dies war allerdings eine Konsolenanwendung und habe daher hier nicht auf argv[0] zugreifen können also hab ich nun mit GetMolduleFileName gearbeitet vielleicht liegt da der Fehler!?

Weiß übrigens zufällig jemand wie man in einem std::vector doppelte Einträge löschen kann?
Vielen Dank im Voraus
Gruß
-
Bitte gib einen vollen Pfad für die Applikation an. Sonst kann das rogramm nur gestartet werden, wenn es in einem der Verzeichnisse liegt, das durch PATH bestimmt wird.
-
Hi,
danke für deine Antwort ich habe nun noch eine Weile rumprobiert, aber der Pfad müsste doch eigentlich richtig sein?Ich finde den Fehler einfach nicht

Wäre daher noch immer über eine Antwort sehr dankbar

Gruß,
Maxxxx
-
Wie kann
char strName[100] = {"MyApp"};Dieser Pfad richtig sein?
-
Hab Dir mal was zusammengebastelt, das funktioniert (bei mir
):#include <windows.h> #include <Shlwapi.h> // Für MS VC++: // #pragma comment(lib, "Shlwapi.lib") bool WriteToAutoStart(const TCHAR* pszModulePath = NULL, const TCHAR* pszKeyName = NULL) { TCHAR *pszPath, *pszApp; if(pszModulePath == NULL) { pszPath = new TCHAR[MAX_PATH + 1]; GetModuleFileName(NULL, pszPath, MAX_PATH); } else { if(!PathFileExists(pszModulePath)) return (false); pszPath = new TCHAR[lstrlen(pszModulePath) + 1]; lstrcpy(pszPath, pszModulePath); } if(pszKeyName == NULL) { const PTCHAR pszFileName = PathFindFileName(pszPath); if(pszFileName != &pszPath[0]) { int iLength = lstrlen(pszFileName) - 3; pszApp = new TCHAR[iLength]; lstrcpyn(pszApp, pszFileName, iLength); pszApp[iLength - 1] = 0; } else { delete [] pszPath; return (false); } } else { pszApp = new TCHAR[lstrlen(pszKeyName) + 1]; lstrcpy(pszApp, pszKeyName); } TCHAR szLocation[] = TEXT("Software\\Microsoft\\Windows\\CurrentVersion\\Run"); HKEY hKey; bool fSuccess = true; if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, szLocation, 0, KEY_ALL_ACCESS, &hKey) != ERROR_SUCCESS) { delete [] pszApp; delete [] pszPath; return (false); } if(RegSetValueEx(hKey, pszApp, 0, REG_SZ, reinterpret_cast<BYTE*>(pszPath), lstrlen(pszPath)) != ERROR_SUCCESS) fSuccess = false; RegCloseKey(hKey); delete [] pszApp; delete [] pszPath; return (fSuccess); }Wenn Du im 1. Param. NULL übergibst, nimmt er den Pfad des aktuellen Moduls und wenn Du im 2. Param NULL übergibst, extrahiert er den Namen der Executable (ohne Dateiendung) und benutzt den als Namen für den Eintrag im Autostart.