Here's a code snippet form CodeProject.org about adding a program to the system registry for it to start up.
the bolded text is text i had to replace to get it to compile (from strlen() to _tcslen)) I'm in UNICODE mode... if that makes any difference
bool CWinStartup::AddApp(LPCTSTR lpszName, LPCTSTR lpszPath, StartupUser user)
if (user == CurrentUser)
hRootKey = HKEY_CURRENT_USER;
hRootKey = HKEY_LOCAL_MACHINE;
HKEY hKey = 0;
HRESULT hr = RegOpenKey(hRootKey, KEY_STARTUP, &hKey);
if (hr == ERROR_SUCCESS)
hr = RegSetValueEx(hKey, lpszName, 0, REG_SZ, (const BYTE*)lpszPath, _tcslen(lpszPath));
return (hr == ERROR_SUCCESS);
When I call that from a MFC program, it only puts a part of the lpszPath in the registry (first 11 chars of any string I give it)
Heres me calling it:
In the registry, it only shows up as D:\DEARD\EA for the path, the name is correct. I've tried using wchar_t, char, and LPCTSTR for the path, they all offer the same results. i've also put the string in _T("") in the actual function, nothing different...
LPCTSTR PATH = _T("D:\\DEARD\\EATEAR\\KDEKAR");
TCHAR NAME = _TEXT("AntiLeet");
startup.AddApp(NAME, PATH, CurrentUser);
There are typically two types of strings available. Old ANSI and Wide char (unicode).
TCHAR is a macro that resolves either to ANSI or Unicode depending on your project configuration. Similarly, _tcslen and _T are also such functions that translate into either ANSI or Unicode depending on project config.
This is very handy in that the code will work without modifications whether you compile it with ANSI or Unicode.
RegSetValueEx wants size in bytes
_tcslen returns size in characters - so you need to multiply it on sizeof (TCHAR)
Amazing! It worked :)
Originally Posted by vart
Thank you so much.
I changed it to
I'm assuming that's what you wanted (i hope) ^^
( _tcslen(lpszPath) * sizeof(TCHAR) )
Yes, that's right. RegSetValueEx makes no distinction between ANSI and Unicode, and as Unicode is 2 bytes on Windows, you need to specify the correct size manually. This can easily be done by multiplying by sizeof(TCHAR), as you've done.