C
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.