CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 19
  1. #1
    Join Date
    Nov 2006
    Posts
    292

    Writting to registry

    I'm trying to set a registry value with the file path of the program using the following code...

    Code:
    int main(char *argv,int argc){
        HKEY hkey;
        DWORD dwDisposition, lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"Software\\Microsoft\\Testing",0,KEY_WRITE,&hkey);
        if(lRes != ERROR_SUCCESS){
         lRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\Microsoft\\Testing",0,0,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,0,&hkey,&dwDisposition);
         RegSetValueEx(hkey,"YourProgName",0,REG_SZ,(unsigned char*)argv,sizeof(argv));}
        else{
             cout << GetLastError() << endl;}
        return 0;
    }
    I first thought of passing argv but that doesn't seem to work too well. Also RegSetValueEx's 5th parameter must be REG_SZ

    From MSDN..

    For string-based types, such as REG_SZ, the string must be null-terminated. With the REG_MULTI_SZ data type, the string must be terminated with two null characters. A backslash must be preceded by another backslash as an escape character. For example, specify "C:\\mydir\\myfile" to store the string "C:\mydir\myfile".
    Except I don't know where to begin looking to find a solution that returns a file path like so "C:\\mydir\\file.exe" - Any suggestions? Thanx.

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Writting to registry

    Code:
    sizeof(argv)
    This will return the size of a (char *) not the length of the string.

    Viggy

  3. #3
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Writting to registry

    And, it should be:
    Code:
    int main (char **argv, int argc)
    Viggy

  4. #4
    Join Date
    Nov 2006
    Posts
    292

    Re: Writting to registry

    Understood, but that still doesn't set a value. Can I have a hint on how I should go about doing this?

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Writting to registry

    Too many problems here in your code:
    1. sizeof(argv) is always in Win32 equal 4 (it is the size of a pointer)
    2. To write to HKEY_LOCAL_MACHINE this code must be run with Admin rights
    3. The check (lRes != ERROR_SUCCESS) is not enough (it may be "Access denied" in which case you won't be able to create the subkey either). So you have to check the exact error code (for example to be ERROR_FILE_NOT_FOUND)
    4. You must not call RegSetValueEx unless RegCreateKeyEx returned ERROR_SUCCESS
    5. You must call RegCloseKey after the key returned by RegOpenKeyEx or RegCreateKeyEx is no more needed.
    Victor Nijegorodov

  6. #6
    Join Date
    Nov 2006
    Posts
    292

    Re: Writting to registry

    >4. You must not call RegSetValueEx unless RegCreateKeyEx returned ERROR_SUCCESS

    Isn't that what I was doing? Also this works fine if I defined a null terminated string like this ..

    Code:
    char buffer[50]="C:\\mydir\\myfile.exe";
    But the idea here is to return the path and pass it in the same format as buffer. That is what I'm trying to figure out.

  7. #7
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Writting to registry

    You're not checking the return value of the call to RegCreateKeyEx. Also, have you fixed all the other errors?

    Viggy

  8. #8
    Join Date
    Nov 2006
    Posts
    292

    Re: Writting to registry

    Quote Originally Posted by MrViggy View Post
    Also, have you fixed all the other errors?
    For the most part.

    Code:
    int main(char **argv,int argc){
        HKEY hkey;
        DWORD dwDisposition, lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\MICROSOFT\\Testing",0,KEY_WRITE,&hkey);
        if(lRes != ERROR_SUCCESS){
         DWORD rRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\MICROSOFT\\Testing",0,0,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,0,&hkey,&dwDisposition);
         if(rRes == ERROR_SUCCESS){ 
          RegSetValueEx(hkey,"YourProgName",0,REG_SZ,(unsigned char*)argv,sizeof(argv));
          RegCloseKey(hkey);}}
        else{
             cout << GetLastError() << endl;}
        return 0;
    }
    I'm having trouble understanding argv fully. Should I even be looking into it? What size should I be passing?

  9. #9
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Writting to registry

    Quote Originally Posted by dellthinker View Post
    For the most part.
    No, unfortunately only for the "less" part!
    1. You cannot simply cast char** to char* and then hope that the correct text will be passed into RegSetValueEx.
    2. sizeof(argv) is still equal 4, not as you would assume to be the length of the text.
    3. The check (lRes != ERROR_SUCCESS) after calling RegOpenKeyEx is not enough (it may be "Access denied" in which case you won't be able to create the subkey either). So you have to check the exact error code (for example to be ERROR_FILE_NOT_FOUND)
    Victor Nijegorodov

  10. #10
    Join Date
    Nov 2006
    Posts
    292

    [solved]

    I have found the solution...

    Code:
    int main(){
        HKEY hkey;
        DWORD dwDisposition, lRes = RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\MICROSOFT\\Testing",0,KEY_WRITE,&hkey);
        char path[MAX_PATH];
        GetModuleFileName(NULL, path,sizeof(path));
        if(lRes == ERROR_FILE_NOT_FOUND){
         DWORD rRes = RegCreateKeyEx(HKEY_LOCAL_MACHINE,"SOFTWARE\\MICROSOFT\\Testing",0,0,REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,0,&hkey,&dwDisposition);
         if(rRes == ERROR_SUCCESS){ 
          RegSetValueEx(hkey,"YourProgName",0,REG_SZ,(unsigned char*)path,sizeof(path));
          RegCloseKey(hkey);}}
        else{
             cout << GetLastError() << endl;}
        return 0;
    }
    That's about the jist of what I was trying to accomplish.

    >1. You cannot simply cast char** to char* and then hope that the correct text will be passed into RegSetValueEx.

    I was taking counsel from MrViggy, though it compiled correctly it still didn't run using that method.

  11. #11
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: [solved]

    Quote Originally Posted by dellthinker View Post
    I was taking counsel from MrViggy, though it compiled correctly it still didn't run using that method.
    The fact that some wrong written code good compiles never meant, never means and never will mean that this code can "good" run!
    Victor Nijegorodov

  12. #12
    Join Date
    Nov 2003
    Location
    Belgium
    Posts
    8,150

    Re: Writting to registry

    Quote Originally Posted by MrViggy View Post
    And, it should be:
    Code:
    int main (char **argv, int argc)
    Viggy
    You swapped the arguments.
    It should be:
    Code:
    int main()
    or
    Code:
    int main(int argc, char* argv[])
    which is the same as:
    Code:
    int main(int argc, char** argv)
    Marc Gregoire - NuonSoft (http://www.nuonsoft.com)
    My Blog
    Wallpaper Cycler 3.5.0.97

    Author of Professional C++, 4th Edition by Wiley/Wrox (includes C++17 features)
    ISBN: 978-1-119-42130-6
    [ http://www.facebook.com/professionalcpp ]

  13. #13
    Join Date
    Feb 2002
    Posts
    4,640

    Re: Writting to registry

    Whoops! I was going off the first post, and hadn't even realized that!

    Thanks!

    Viggy

  14. #14
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Writting to registry

    Why anyone would want to do registry work using the Win32 api's is beyond me.

    Code:
    #include <atlbase.h>
    
    CRegKey rk;
    if( ERROR_SUCCESS == rk.Open( HKEY_LOCAL_MACHINE, _T("SOFTWARE\\MICROSOFT\\Testing") ) )
    {
      rk.SetStringValue( _T("YourProgName"), szPath );
    }

  15. #15
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: Writting to registry

    Quote Originally Posted by Arjay View Post
    Why anyone would want to do registry work using the Win32 api's is beyond me.
    [/CODE]
    I guess it's because this is a C++ and WinAPI forum...
    Isn't it?
    Victor Nijegorodov

Page 1 of 2 12 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured