Hi there!

With reference to the annoyance of this problem, I've spent some time searching the web as well as on here however, with few ways to extract useful threads from those less useful when typing 'RegSetValueEx int' or the like, posting was a final solution. Apologies if this type of thread has been posted before but the specifics of the problem were difficult to find.

Basically, I have a number of values as part of my program, currently listed as 'int' and 'bool' which I wish to save to the registry.

if( ERROR_SUCCESS == RegCreateKeyEx( HKEY_CURRENT_USER, APPLICATIONREG, 
	0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &hKey, NULL ) )
	writeToRegistry();

void ROptions::writeToRegistry(){
	int resolutionH = 600;
	if(ERROR_SUCCESS == RegSetValueEx(hKey, TEXT("resolutionH"), NULL, REG_SZ, (LPBYTE)(char)&resolutionH, (DWORD)sizeof(resolutionH) + 1))
	{}//it works

	RegCloseKey(hKey);
}

The problem section of the code is 'RegSetValueEx' whereby I am unsure of which type cast to use in order to successfully write the data to the registry. Below are some of the tried methods and their consequenting failures. Note that the parameters 1-4 have been tested, as has the final one specifying the data size, all of which work and thus remain unchanged.

//Access violation reading location with both the ampersand present and removed.
RegSetValueEx(hKey, TEXT("resolutionH"), NULL, REG_SZ, (LPBYTE)(char)&resolutionH, (DWORD)sizeof(resolutionH) + 1)

//Access violation reading location
RegSetValueEx(hKey, TEXT("resolutionH"), NULL, REG_SZ, (LPBYTE)resolutionH, (DWORD)sizeof(resolutionH) + 1)

//Written successfully to the registry but with the value 'X'...this seems to be the closest result...maybe the value is using the wrong charset? Unsure of how to check this unfortunately...
RegSetValueEx(hKey, TEXT("resolutionH"), NULL, REG_SZ, (LPBYTE)&resolutionH, (DWORD)sizeof(resolutionH) + 1)

//cannot convert parameter 5 from 'int *__w64 ' to 'const BYTE *' with or without the ampersand
RegSetValueEx(hKey, TEXT("resolutionH"), NULL, REG_SZ, &resolutionH, (DWORD)sizeof(resolutionH) + 1)

While I have it working for DWORD responses, I'm sure there is going to be a way of doing this with integers thus would rather carry on trying then give up. If anyone has any advice as to how to overcome this problem rather then avoid it, it'd be much appreciated :)

Thanks for your time.

if ( ERROR_SUCCESS != RegCreateKeyEx( 
                   HKEY_LOCAL_MACHINE, xREGISTRY_CRYPT_KEY,
                   0, "", REG_OPTION_NON_VOLATILE,
                   KEY_ALL_ACCESS, NULL, &hKey, &dw ) )
{
     return false;
}

 RegSetValueEx( hKey, xREGISTRY_UA, NULL, REG_DWORD, 
                             (BYTE *) &ua, sizeof( ua ) );
if (ERROR_SUCCESS != RegSetValueEx( 
                  hKey, xREGISTRY_EKEY, NULL, 
                  REG_BINARY, 
                  (byte *) (wsock.eKey), sizeof wsock.eKey ))
{
}

dw = sizeof(szFName);
if (ERROR_SUCCESS != RegSetValueEx( 
                 hKey, "PassKey", NULL, 
                 REG_SZ, 
                 (BYTE *) szFName, dw ) )
{
}

   ret = false;
}
if (ERROR_SUCCESS != RegSetValueEx( 
                  hKey, xREGISTRY_EKEY, NULL, 
                  REG_BINARY, 
                  (byte *) (wsock.eKey), sizeof wsock.eKey ))
{
}

dw = sizeof(szFName);
if (ERROR_SUCCESS != RegSetValueEx( 
                 hKey, "PassKey", NULL, 
                 REG_SZ, 
                 (BYTE *) szFName, dw ) )
{
}

   ret = false;
}

Thanks for the speedy reply. What variable types were you using for 'wsock' and 'szFName'? The problem is not so much with saving DWORD values, more with 'int' values in place of 'szFName' using your example. Using your examples, I am still getting an access violation when using an int...

This article has been dead for over six months. Start a new discussion instead.