dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Problem with RegSetValue Function

  1. #1
    Join Date
    Apr 2005
    Posts
    1,828

    Problem with RegSetValue Function

    I have an entry
    HKLM\Software\\ControlFLASH Firmware Upgrade Kit\\ControlFLASH\\Catalog\\1756-CNB/D
    Within which I have an string named "Revisions". I want to change the value present in the string. For that I have used this code

    Code:
    LONG	retval;
    				HKEY	hParentKey;
    				CString csSubKey;
    				DWORD	dwType;
    				DWORD	cbBufSize = MAX_PATH+1;
    
    				csSubKey.Format("%s\\%s\\Revisions", 
                           CATALOG_REG_KEY,
    					   csCatalogNumber);
    
    				if ((retval = ::RegSetValue (HKEY_LOCAL_MACHINE,
    								(LPCTSTR) csSubKey,
    								dwType,
    								csRevisionEnt,
    								cbBufSize)) == ERROR_SUCCESS)
    				{
    					AfxMessageBox ("Success");
    					break;
    				}
    Somehow its not working even the value of retval is coming ERROR_SUCCESS but the change is not taking place

  2. #2
    Join Date
    Feb 2008
    Location
    India
    Posts
    28

    Re: Problem with RegSetValue Function

    change your DWORD dwType to REG_SZ
    Code:
    if (RegSetValue (HKEY_LOCAL_MACHINE,
    		 (LPCTSTR) csSubKey, 
    		REG_SZ, csRevisionEnt,
    		cbBufSize) == ERROR_SUCCESS)
    {
    //your code
    }
    In RegSetValue dwType must be the REG_SZ type. To store other data types, use the RegSetValueEx function.

  3. #3
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Problem with RegSetValue Function

    I changed it but still the result is same

    Code:
    LONG	retval;
    HKEY	hParentKey;
    CString csSubKey;
    DWORD	dwType;
    DWORD	cbBufSize = MAX_PATH+1;
    
    csSubKey.Format("%s\\%s\\Revisions", 
                           CATALOG_REG_KEY,
    			csCatalogNumber);
    
    if ((retval = ::RegSetValue (HKEY_LOCAL_MACHINE,
    		(LPCTSTR) csSubKey,
    		REG_SZ,
    		csRevisionEnt,
    		cbBufSize)) == ERROR_SUCCESS)
    	{
    		AfxMessageBox ("Success");
    		break;
    	}

  4. #4
    Join Date
    May 2008
    Posts
    300

    Re: Problem with RegSetValue Function

    You've misstaken a few things.

    1. RegSetValue can only change the default value of a subkey.

    2. A value isn't a subkey. Values are shown on the right, while keys are shown on the left in regedit.exe. And a subkey is just a key that is some other key's branch.

    3. RegSetValue's dwType only accepts 2. It doesn't matter how you express it(REG_SZ, 2, 3 - 1, or by variables)

    Use RegSetValueEx. For most functions that have -Ex versions, the original versions are existing only for compatibility, which will simply call the -Ex versions with default parameters.

  5. #5
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Problem with RegSetValue Function

    It appears that you did not open the key.

    According to MSDN:
    hKey
    [in] A handle to an open registry key. The key must have been opened with the KEY_SET_VALUE access right. For more information, see Registry Key Security and Access Rights.
    Don't forget to close the key when you're done.
    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  6. #6
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Problem with RegSetValue Function

    OK thanks for the help I have made modification to the code as suggested by you.

    Code:
    LONG	retval;
    HKEY	hParentKey;
    char	*szBuff;
    CString csSubKey;
    DWORD	dwType;
    DWORD	cbBufSize = MAX_PATH+1;
    
    sprintf (szBuff, "%s", csRevisionEnt);
    csSubKey.Format("%s\\%s\\Revisions", CATALOG_REG_KEY, csCatalogNumber);
    
    if ((retval = ::RegOpenKeyEx (HKEY_LOCAL_MACHINE,(LPCTSTR) csSubKey,0L,
    								KEY_SET_VALUE,
    								&hParentKey)) == ERROR_SUCCESS)	
    {
    	if ((retval = ::RegSetValueEx (HKEY_LOCAL_MACHINE,
    					(LPCTSTR) csSubKey,
    					NULL,
    					REG_DWORD,
    					(LPBYTE)szBuff,
    					cbBufSize)) == ERROR_SUCCESS)
    				{
    					AfxMessageBox ("Revision Successfully Deleted");
    					//break;
    				}
    }
    ::RegCloseKey (hParentKey);
    
    But this time when It enters RegSetValueEx condition, it throws an exception :(

  7. #7
    Join Date
    May 2008
    Posts
    300

    Re: Problem with RegSetValue Function

    If my eyes serve... It appears you didn't initialize szBuff.

    And for REG_DWORD the length should always be 4, not 261. I think you wanted to put a REG_SZ value.

    Something not quite related. MAX_PATH counts the terminating zero, so you don't have to make it MAX_PATH + 1.
    Nope

  8. #8
    Join Date
    Oct 2002
    Location
    Timisoara, Romania
    Posts
    14,360

    Re: Problem with RegSetValue Function

    Perhaps in the future this could help you: http://www.mariusbancila.ro/registryclasses.php.
    Marius Bancila
    Home Page
    My CodeGuru articles

    I do not offer technical support via PM or e-mail. Please use vbBulletin codes.

  9. #9
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Problem with RegSetValue Function

    OK I have made the change. I exactly followed your instructoins.

    Code:
    LONG	retval;
    HKEY	hParentKey;
    char	*szBuff;
    CString csSubKey;
    DWORD	dwType;
    DWORD	cbBufSize = 4;
    
    memset (szBuff, NULL, sizeof(szBuff));
    
    sprintf (szBuff, "%s", csRevisionEnt);
    csSubKey.Format("%s\\%s\\Revisions", CATALOG_REG_KEY, csCatalogNumber);
    
    if ((retval = ::RegOpenKeyEx (HKEY_LOCAL_MACHINE,
    			(LPCTSTR) csSubKey,
    			0L,
    			KEY_SET_VALUE,
    			&hParentKey)) == ERROR_SUCCESS)
    	{
    		if ((retval = ::RegSetValueEx (HKEY_LOCAL_MACHINE,
    				(LPCTSTR) csSubKey,
    				NULL,
    				REG_DWORD,
    				(LPBYTE)szBuff,
    				cbBufSize)) == ERROR_SUCCESS)
    		{
    			//AfxMessageBox ("Revision Successfully Deleted");
    			::RegCloseKey (hParentKey);
    			break;
    		}
    	}
    Still the problem insists. While debugging, after few more statements it goes to dbgrpt.h and in generate error
    Last edited by maverick786us; June 2nd, 2008 at 07:31 AM.

  10. #10
    Join Date
    May 2008
    Posts
    300

    Re: Problem with RegSetValue Function

    T__T

    char *szBuff = new char[4];

    or

    char szBuff[4];

    or if want you want is REG_DWORD

    DWORD szBuff = 0; and RegSetValueEx(... &szBuff);
    Nope

  11. #11
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Problem with RegSetValue Function

    There are several problems with your code, most significantly an understanding of the parameters to SetRegValueEx.

    From MSDN:
    dwType
    [in] The type of data pointed to by the lpData parameter. For a list of the possible types, see Registry Value Types.
    lpData
    [in] The data to be stored.
    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.
    Therefore to set a DWORD type, you would use something like this:
    Code:
    LONG	retval;
    HKEY	hParentKey;
    // char	*szBuff;
    DWORD dwNewValue = _tstoi(csRevisionEnt); // this would be the new value being written
    CString csSubKey;
    DWORD	dwType;
    DWORD	cbBufSize = sizeof (DWORD);
    
    // sprintf (szBuff, "%s", csRevisionEnt); // you're writing a DWORD - not a string
    csSubKey.Format("%s\\%s\\Revisions", CATALOG_REG_KEY, csCatalogNumber);
    
    if ((retval = ::RegOpenKeyEx (HKEY_LOCAL_MACHINE,(LPCTSTR) csSubKey,0L,
    								KEY_SET_VALUE,
    								&hParentKey)) == ERROR_SUCCESS)	
    {
    	if ((retval = ::RegSetValueEx (HKEY_LOCAL_MACHINE,
    					(LPCTSTR) csSubKey,
    					NULL,
    					REG_DWORD,
    					&dwNewValue,
    					cbBufSize)) == ERROR_SUCCESS)
    				{
    					AfxMessageBox ("Revision Successfully Deleted");
    					//break;
    				}
    }
    ::RegCloseKey (hParentKey);
    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  12. #12
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Problem with RegSetValue Function

    OK thanks but ...

    Code:
    if ((retval = ::RegSetValueEx (HKEY_LOCAL_MACHINE,
    									"Revisions",
    									NULL,
    									REG_DWORD,
    									&dwNewValue,									
    									cbBufSize)) == ERROR_SUCCESS)
    error C2664: 'RegSetValueExA' : cannot convert parameter 5 from 'DWORD *' to 'const BYTE *'

  13. #13
    Join Date
    Jan 2002
    Location
    Houston, TX
    Posts
    1,421

    Re: Problem with RegSetValue Function

    You can cast it to the type needed:
    Code:
    (const char*)&dwNewValue;
    Hope that helps.
    Be sure to rate those who help!
    -------------------------------------------------------------
    Karl - WK5M
    PP-ASEL-IA (N43CS)
    PGP Key: 0xDB02E193
    PGP Key Fingerprint: 8F06 5A2E 2735 892B 821C 871A 0411 94EA DB02 E193

  14. #14
    Join Date
    May 2008
    Posts
    300

    Re: Problem with RegSetValue Function

    to maverick786us:

    People give you hints and you should still think by yourself. Try your best to depend on yourself.

    (BYTE *)&dwNewValue;
    Nope

  15. #15
    Join Date
    Apr 2005
    Posts
    1,828

    Re: Problem with RegSetValue Function

    Well typecasting into const char* didn't work but I casted this way

    Code:
    (LPBYTE)(LPCTSTR)&dwNewValue,
    There wase'nt any error, but the value wase'nt set either

    Code:
    LONG	retval;
    				HKEY	hParentKey;				
    				DWORD 	dwNewValue = _tstoi(csRevisionEnt);
    				CString csSubKey;
    				DWORD	dwType;
    				DWORD	cbBufSize = sizeof(DWORD);
    				
    				csSubKey.Format("%s\\%s", 
                           CATALOG_REG_KEY,
    					   csCatalogNumber);
    
    				if ((retval = ::RegOpenKeyEx (HKEY_LOCAL_MACHINE,
    								(LPCTSTR) csSubKey,
    								0L,
    								KEY_SET_VALUE,
    								&hParentKey)) == ERROR_SUCCESS)
    				{
    					if ((retval = ::RegSetValueEx (HKEY_LOCAL_MACHINE,
    									"Revisions",
    									NULL,
    									REG_DWORD,
    									(LPBYTE)(LPCTSTR)&dwNewValue,									
    									cbBufSize)) == ERROR_SUCCESS)
    					{
    						//AfxMessageBox ("Revision Successfully Deleted");
    						//
    					}
    				}
    				::RegCloseKey (hParentKey);						
    				break;

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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)