String problem in HKLM\MS\Windows\CurrentVersion\Run
-
Hi, I have a weird problem where if I set a value in HKLM\MS\Windows\CurrentVersion\Run in my code, my app won't be launched. But if I double click on MyApp key, select the value, do a cut-paste of the same string, reboot the computer then it will work! HKLM\MS\Windows\CurrentVersion\Run Key: MyApp, Type: String, Value: "C:\Program Files\MyApp\MyApp.exe" (quotes are included in the string since there's a space in there). Here's my code:
TCHAR szPath[MAX_PATH+1]; TCHAR szFullPath[MAX_PATH+1]; UTLGetAppPath( szPath, sizeof( szPath ) ); //returns the path _tcscat( szPath, NTS_ISMGR_EXE ); //adds the executable memset( szFullPath, 0, sizeof( szFullPath ) ); _tcscpy( szFullPath, "\"" ); _tcscat( szFullPath, szPath ); _tcscat( szFullPath, "\"\0" ); //Add to run lRes = RegSetValueEx( hKey, NTS_REG_STARTUP_NAME, 0, REG_SZ, (BYTE*)szFullPath, sizeof( szFullPath ) );
What is wrong??? Thanks! :)
-
Hi, I have a weird problem where if I set a value in HKLM\MS\Windows\CurrentVersion\Run in my code, my app won't be launched. But if I double click on MyApp key, select the value, do a cut-paste of the same string, reboot the computer then it will work! HKLM\MS\Windows\CurrentVersion\Run Key: MyApp, Type: String, Value: "C:\Program Files\MyApp\MyApp.exe" (quotes are included in the string since there's a space in there). Here's my code:
TCHAR szPath[MAX_PATH+1]; TCHAR szFullPath[MAX_PATH+1]; UTLGetAppPath( szPath, sizeof( szPath ) ); //returns the path _tcscat( szPath, NTS_ISMGR_EXE ); //adds the executable memset( szFullPath, 0, sizeof( szFullPath ) ); _tcscpy( szFullPath, "\"" ); _tcscat( szFullPath, szPath ); _tcscat( szFullPath, "\"\0" ); //Add to run lRes = RegSetValueEx( hKey, NTS_REG_STARTUP_NAME, 0, REG_SZ, (BYTE*)szFullPath, sizeof( szFullPath ) );
What is wrong??? Thanks! :)
You do realize that the Run key is only processed at login, right? Programs don't get run right away when added to the key. (Sorry, silly question, but I had to ask to be sure you understand how the key works.) --Mike-- LINKS~! Ericahist updated! | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD
-
You do realize that the Run key is only processed at login, right? Programs don't get run right away when added to the key. (Sorry, silly question, but I had to ask to be sure you understand how the key works.) --Mike-- LINKS~! Ericahist updated! | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD
I know! ;) This is how I've tested: I use InnoSetup for my app and it creates the MyApp key when installing my app. I reboot, everything is fine, app is launched at startup. Then, I let my code handle the key. First, it deletes the key with RegDeleteValue and then creates (or should I say sets it) it with RegSetValueEx (as shown above). I then restart Windows and although the key is there (and apparently with the same value), the app won't be launched! I thought the problem was that I didn't add a null character at the end of the string but it still doesn't work!
-
You do realize that the Run key is only processed at login, right? Programs don't get run right away when added to the key. (Sorry, silly question, but I had to ask to be sure you understand how the key works.) --Mike-- LINKS~! Ericahist updated! | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD
-
Hi, I have a weird problem where if I set a value in HKLM\MS\Windows\CurrentVersion\Run in my code, my app won't be launched. But if I double click on MyApp key, select the value, do a cut-paste of the same string, reboot the computer then it will work! HKLM\MS\Windows\CurrentVersion\Run Key: MyApp, Type: String, Value: "C:\Program Files\MyApp\MyApp.exe" (quotes are included in the string since there's a space in there). Here's my code:
TCHAR szPath[MAX_PATH+1]; TCHAR szFullPath[MAX_PATH+1]; UTLGetAppPath( szPath, sizeof( szPath ) ); //returns the path _tcscat( szPath, NTS_ISMGR_EXE ); //adds the executable memset( szFullPath, 0, sizeof( szFullPath ) ); _tcscpy( szFullPath, "\"" ); _tcscat( szFullPath, szPath ); _tcscat( szFullPath, "\"\0" ); //Add to run lRes = RegSetValueEx( hKey, NTS_REG_STARTUP_NAME, 0, REG_SZ, (BYTE*)szFullPath, sizeof( szFullPath ) );
What is wrong??? Thanks! :)
Are you a UNICODE program? I ask this because you are doing a lot of TCHAR and _tcscpy. If you are, then be careful when passing sizeof() into functions. Check the API, see if it wants the buffer size in bytes or characters. If characters, then you should use (sizeof(buf)/sizeof(TCHAR))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [Santa Cruz Networks](http://www.santacruznetworks.com)
-
One thing I did notice is that when I set the key through the code, there's '...' at the end of the string! If I set the value manually, the '...' disapears!
-
Are you a UNICODE program? I ask this because you are doing a lot of TCHAR and _tcscpy. If you are, then be careful when passing sizeof() into functions. Check the API, see if it wants the buffer size in bytes or characters. If characters, then you should use (sizeof(buf)/sizeof(TCHAR))
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Peter Weyzen Staff Engineer [Santa Cruz Networks](http://www.santacruznetworks.com)
-
I think I found the solution:
lRes = RegSetValueEx( hKey, NTS_REG_STARTUP_NAME, 0, REG_SZ, (LPBYTE)szFullPath, /*sizeof( szFullPath )*/_tcslen( szFullPath ) );
Replaced sizeof() with _tcslen()...
That's almost right, you need to pass the length of the string in bytes, including the final null. So it's:
sizeof(TCHAR) * (1+_tcslen(szFullPath))
--Mike-- LINKS~! Ericahist updated! | 1ClickPicGrabber | CP SearchBar v2.0.2 | C++ Forum FAQ Strange things are afoot at the U+004B U+20DD