TCHAR exepath[MAX_PATH];
GetModuleFileName(0, exepath, MAX_PATH);
HKEY hKey;
LONG lnRes = RegOpenKeyEx(
HKEY_CURRENT_USER,
"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run",
0,KEY_WRITE,
&hKey
);
if( ERROR_SUCCESS == lnRes )
{
lnRes = RegSetValueEx(hKey,
"YourProgramsName",
0,
REG_SZ,
exepath,
strlen(exepath));
}
To add as a HKLM (For all users) startup item instead of HKCU (Current User), change HKEY_CURRENT_USER to HKEY_LOCAL_MACHINE.
I'm posting this because I had a hard time figuring this out myself, and hopefully this will save some people a lot of searches.
So, if you want your program to startup automatically when the user logs in, just paste this code into it.