Thread: Registry Problems ( RegSetValuEx )

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Registry Problems ( RegSetValuEx )

    Hey everyone,

    I'm having a bit of a problem when I save an entry into the registry. It'll story any number less than 100, however if the number (aka the variable: trigger_key ) is equal to or greater than 100 (no larger than 255), it saves a random value.


    Code:
    HKEY hk;
    DWORD dwDisp;
    TCHAR dwData[120];
    
    RegCreateKeyExA(HKEY_CURRENT_USER, "SOFTWARE\\MacroKeys", 0, NULL, REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, &hk, &dwDisp);
    
    _stprintf ( dwData, _T("%d"), trigger_key );
    RegSetValueEx(hk, _T("trigger_key"), 0, REG_DWORD, (PBYTE)&dwData, sizeof(PDWORD));
    Here is my code to retrieve the value
    Code:
    HKEY hk;
    DWORD dwType;
    DWORD dwLength;
    TCHAR val[120];
    
    RegOpenKeyExA(HKEY_CURRENT_USER, "SOFTWARE\\MacroKeys", 0, KEY_QUERY_VALUE, &hk);
    
    RegQueryValueEx(hk, _T ( "trigger_key" ), NULL, NULL, (LPBYTE)val, &dwLength);
    trigger_key = _ttoi ( val );

    Any help will be appreciated.

    Thanks,

    Matt N
    Last edited by guitarist809; 05-07-2008 at 06:16 PM.
    ~guitarist809~

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    214
    1. declare your variable dwData as a DWORD.

    2. Remove the _stprintf line.

    3. Check the return value of RegCreateKeyExA

    long ret = RegCreateKeyExA...

    4. Set the dwData value : dwData = trigger_key;

    5. Change the RegSetValueEx :
    Code:
            if( ret == ERROR_SUCCESS )
            {
                 RegSetValueEx(hk, _T("trigger_key"), 0, REG_DWORD, (CONST BYTE*)&dwData, sizeof(dwData));
            }
    These changes worked for me.
    Last edited by DaveH; 05-07-2008 at 07:47 PM.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by DaveH View Post
    1. declare your variable dwData as a DWORD.

    2. Remove the _stprintf line.

    3. Check the return value of RegCreateKeyExA

    long ret = RegCreateKeyExA...

    4. Set the dwData value : dwData = trigger_key;

    5. Change the RegSetValueEx :
    Code:
            if( ret == ERROR_SUCCESS )
            {
                 RegSetValueEx(hk, _T("trigger_key"), 0, REG_DWORD, (CONST BYTE*)&dwData, sizeof(dwData));
            }
    These changes worked for me.
    Thank you! Worked like a charm (for saving)...
    Last edited by guitarist809; 05-07-2008 at 08:34 PM.
    ~guitarist809~

  4. #4
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Ok, now for some reason, whenever I try to load the values, they get reset to 0.

    Code:
    HKEY hk;
    	DWORD dwType;
    	DWORD dwLength;
    	DWORD val;
    
    	long ret = RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE\\AntiRecoil"), 0, KEY_QUERY_VALUE, &hk);
    
    	if ( ret != ERROR_SUCCESS )
    		return 1;
    
    	RegQueryValueEx(hk, _T ( "k_trigger" ), NULL, NULL, (LPBYTE)val, &dwLength);
    	k_trigger = val;
    ~guitarist809~

  5. #5
    Madly in anger with you
    Join Date
    Nov 2005
    Posts
    211
    RegQueryValueEx(hk, _T ( "k_trigger" ), NULL, NULL, (LPBYTE)val, &dwLength);
    should be:

    RegQueryValueEx(hk, _T ( "k_trigger" ), NULL, NULL, (LPBYTE)&val, &dwLength);

    Intel Core 2 Quad Q6600 @ 2.40 GHz
    3072 MB PC2-5300 DDR2
    2 x 320 GB SATA (640 GB)
    NVIDIA GeForce 8400GS 256 MB PCI-E

  6. #6
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Looks like you're writing using "trigger_key", while you're reading "k_trigger"?

  7. #7
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by rags_to_riches View Post
    Looks like you're writing using "trigger_key", while you're reading "k_trigger"?
    Yeah, don't worry about that (i changed trigger_key to k_trigger, forgot to post it)

    And thanks Bleech, for the quick response (the mistake was so simple, now I feel like an idiot )
    ~guitarist809~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Registry, Regedit
    By franse in forum C++ Programming
    Replies: 21
    Last Post: 01-29-2009, 09:57 AM
  2. Problems defining classes
    By esmeco in forum C++ Programming
    Replies: 47
    Last Post: 10-24-2007, 01:13 PM
  3. Registry Problems
    By neandrake in forum Windows Programming
    Replies: 1
    Last Post: 12-06-2003, 11:08 PM
  4. Having Problems with Win2k and Softice
    By XenoCodex Admin in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 07-01-2002, 02:14 PM
  5. Registry Access
    By ExDigit in forum Windows Programming
    Replies: 3
    Last Post: 01-04-2002, 04:02 AM