Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. String problem in HKLM\MS\Windows\CurrentVersion\Run

String problem in HKLM\MS\Windows\CurrentVersion\Run

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelp
8 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    LukeV
    wrote on last edited by
    #1

    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! :)

    M P 2 Replies Last reply
    0
    • L LukeV

      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! :)

      M Offline
      M Offline
      Michael Dunn
      wrote on last edited by
      #2

      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

      L 2 Replies Last reply
      0
      • M Michael Dunn

        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

        L Offline
        L Offline
        LukeV
        wrote on last edited by
        #3

        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!

        1 Reply Last reply
        0
        • M Michael Dunn

          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

          L Offline
          L Offline
          LukeV
          wrote on last edited by
          #4

          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!

          L 1 Reply Last reply
          0
          • L LukeV

            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! :)

            P Offline
            P Offline
            Peter Weyzen
            wrote on last edited by
            #5

            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)

            L 1 Reply Last reply
            0
            • L LukeV

              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!

              L Offline
              L Offline
              LukeV
              wrote on last edited by
              #6

              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()...

              M 1 Reply Last reply
              0
              • P Peter Weyzen

                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)

                L Offline
                L Offline
                LukeV
                wrote on last edited by
                #7

                No it's not using UNICODE. It's in bytes, not characters, but I think I found the solution by replacing sizeof() with _tcslen(). Is this safe?

                1 Reply Last reply
                0
                • L LukeV

                  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()...

                  M Offline
                  M Offline
                  Michael Dunn
                  wrote on last edited by
                  #8

                  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

                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups