Results 1 to 6 of 6

Thread: Registry doesn't work like it should

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    5

    Registry doesn't work like it should

    Hello guys (first post here).

    I've been looking quite a while on the internet on the registry functions in Visual Studio 2010.
    I've come across the functions RegOpenKeyEx and RegSetValueEx (the ones I need).

    But; according to what I come across in EVERY forum; this should work:

    Code:
    HKEY hKey;
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hKey)
    RegSetValueEx(hKey, "TEST", 0, REG_SZ, (LPBYTE) tbuf, strlen(tbuf) + 1
    With tbuf just holding the path name of a program.
    Pretty normal code.
    The strange part is, in the first line

    Code:
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_ALL_ACCESS, &hKey)
    The program keeps complaining that in the "SOFTWARE...etc" part, the compiler says it is not of the time LPCWSTR. Seems logical to me, but in ALL examples I found on the internet, they were NOT getting this error message.
    But even if I change the type to LPCWSTR (by just typing (LPCWSTR)"SOFTWARE... etc" I do not get an error anymore, but the program just does not work.

    Can anyone explain why my compiler keeps complaining about that piece of string not being of the type LPCWSTR and ALL others don't get an error?

    Thanks.

  2. #2

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    5

    Re: Registry doesn't work like it should

    Well hello,

    I made the OpenKey function work, FINALLY.

    BUT... I have a new problem, and I really don't know what is wrong.

    Code:
    DWORD dwType=0, dwDataSize=0, dwBufSize=0;
    LONG lResult;
    LPTSTR szVal;
    LPTSTR * tmpRes;
    tmpRes = &szVal;
    
    // I let all the error checking out, so it isn't too much to read
    
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_ALL_ACCESS, &hKey);
    
    // THIS WORKS (I think), since hKey contains a value != NULL after this procedure
    
    RegQueryValueEx(hKey, _T("a"), 0, &dwType, NULL, &dwDataSize);
    
    // THIS DOES NOT WORK?????
    // And yes, in Regedit.exe I manually created a value named "a" (type REG_SZ) with the value "testje!" ("test!" in english, the other is dutch).
    // Please help me, I have read the MSDN documents until my eyes started hurting, as well as every forum post I could find about this function...........................
    // dwDataSize always remains 0.............................
    ??
    Last edited by smonjirez; Aug 23rd, 2010 at 04:49 PM.

  3. #3
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Registry doesn't work like it should

    Quote Originally Posted by smonjirez View Post
    Well hello,

    I made the OpenKey function work, FINALLY.

    BUT... I have a new problem, and I really don't know what is wrong.

    Code:
    DWORD dwType=0, dwDataSize=0, dwBufSize=0;
    LONG lResult;
    LPTSTR szVal;
    LPTSTR * tmpRes;
    tmpRes = &szVal;
    
    // I let all the error checking out, so it isn't too much to read
    
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_ALL_ACCESS, &hKey);
    
    // THIS WORKS (I think), since hKey contains a value != NULL after this procedure
    
    RegQueryValueEx(hKey, _T("a"), 0, &dwType, NULL, &dwDataSize);
    
    // THIS DOES NOT WORK?????
    // And yes, in Regedit.exe I manually created a value named "a" (type REG_SZ) with the value "testje!" ("test!" in english, the other is dutch).
    // Please help me, I have read the MSDN documents until my eyes started hurting, as well as every forum post I could find about this function...........................
    // dwDataSize always remains 0.............................
    ??
    Mind me, I don't know C++, but if I read the documentation correctly it should be something like
    Code:
    DWORD dwType=0, dwDataSize=0, dwBufSize=0;
    LONG lResult;
    LPTSTR szVal;
    LPTSTR * tmpRes;
    LPBYTE lpReturnVal;
    tmpRes = &szVal;
    
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_ALL_ACCESS, &hKey);
    
    RegQueryValueEx(hKey, _T("a"), NULL, &dwType, &lpReturnVal, &dwDataSize);
    Where lpReturnVal is the pointer to the retrieved data.
    &dwType could also be NULL

    The MSDN documentation also shows an example down the page.
    http://msdn.microsoft.com/en-us/libr...8VS.85%29.aspx

    Welcome to VBF, by the way
    Delete it. They just clutter threads anyway.

  4. #4

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    5

    Re: Registry doesn't work like it should

    Correct.

    What you are describing is the procedure for returning a pointer to the retrieved data.

    But in this piece of code, the only goal was to determine the size of the returned data (got it from a script somewhere).

    Anyways, even if I execute it like in your example, it doesn't work.

    And thanks by the way .

  5. #5
    Frenzied Member TheBigB's Avatar
    Join Date
    Mar 2006
    Location
    *Stack Trace*
    Posts
    1,511

    Re: Registry doesn't work like it should

    What does dwDataSize return?
    Delete it. They just clutter threads anyway.

  6. #6

    Thread Starter
    New Member
    Join Date
    Aug 2010
    Posts
    5

    Re: Registry doesn't work like it should

    It just returns 0.

    Just tried another example I found on internet:


    Note: this is AFTER the piece of code I used before to open the key in the first place:

    Code:
    HKEY hKey;
    RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"), 0, KEY_SET_VALUE, &hKey);
    Code:
    if (RegSetValueEx(hKey, 
    					_T(REGNAME_TO_WRITE),
    					0,
    					REG_SZ,
    					(const unsigned char *)tbuf,
    					strlen(tbuf)) == ERROR_SUCCESS) {
    		cout << "Success!\n";
    Again, the program "works just fine", but it doesn't write a value in the registry at all.

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