C
#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);
}
Wobei ich immer wieder sagen muss: Lern erst richtig C/C++ und dann fang mit der WinAPI an, sonst hat es keinen Sinn... (Script-Kiddie ?)