Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm trying set a regkey 1803 to 3 and am having some problems....these are the values.
Value Name: 1803
Data Type: REG_DWORD (DWORD Value)
Value Data: (0 = downloads enabled, 3 = downloads disabled)

Here is my code...
C++
#include "stdafx.h"
#include <windows.h>
#include <winreg.h>

#pragma comment(lib,"Advapi32.lib")
//#pragma (dll,Advapi32.dll) What To Do ??????????? 
int main () {
	HKEY hKey;			
	DWORD buffersize = 1024;
	char* lpData = new char[buffersize];

	RegOpenKeyEx (HKEY_CURRENT_USER,L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3",NULL,KEY_SET_VALUE,&hKey);
	RegQueryValueEx(hKey,L"1803",NULL,REG_DWORD,(LPBYTE) lpData,&buffersize);
	RegSetValueExW(hKey,L"1803",REG_DWORD,(LPBYTE) lpData,,3);
	RegCloseKey (hKey);
	delete lpData;
}


Here are my errors...
1 IntelliSense: argument of type "int" is incompatible with parameter of type "LPDWORD"
2 IntelliSense: argument of type "LPBYTE" is incompatible with parameter of type "DWORD"
3 IntelliSense: expected an expression
Posted
Updated 11-Sep-11 20:43pm
v2

1 solution

Hi,

Many things are wrong with the original code you posted.

1.) You should check the return value of RegOpenKeyEx for ERROR_SUCCESS.
2.) You are trying to read the registry value... but you open the key with only KEY_SET_VALUE.
3.) You are allocating a dynamic array when your obviously using a constant buffer size.
4.) Some missing function parameters.
5.) You should use the correct delete[] operator.

Something like this should work:

C++
#define BUFFER_SIZE 1024 //add this to your header		
HKEY hKey;			
DWORD dwErr = NO_ERROR;

dwErr = RegOpenKeyEx (HKEY_CURRENT_USER,L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3",NULL,KEY_SET_VALUE|KEY_QUERY_VALUE,&hKey);
if(ERROR_SUCCESS == dwErr)
{
	BYTE data[BUFFER_SIZE] = {0};
	DWORD dwType = REG_NONE;

	dwErr = RegQueryValueEx(hKey,L"1803",NULL,&dwType,data,&buffersize);
	if(ERROR_SUCCESS == dwErr && REG_DWORD == dwType)
	{
		*((LPDWORD)data) = 0;
		dwErr = RegSetValueExW(hKey,L"1803",0,REG_DWORD,data,sizeof(DWORD));
	}
	RegCloseKey (hKey);
}


Also... if you are on a 64 bit operating system and writing a 32 bit application you will need to add KEY_WOW64_64KEY to your RegOpenKeyEx flags.

Best Wishes,
-David Delaune
 
Share this answer
 
v4
Comments
Member 7766180 11-Sep-11 18:29pm    
Thank you for the information, I guess I did a lot wrong!!! I put this together and I'm getting this error on dwErr. (No storage class or type specifier) Also where does it set the value to 3, I don't see it.

#include "stdafx.h"
#include <windows.h>
#include <winreg.h>
#pragma comment(lib,"Advapi32.lib")

int _main(){
HKEY hKey;
DWORD buffersize = 1024;
DWORD dwErr = NO_ERROR;

dwErr = RegOpenKeyEx (HKEY_CURRENT_USER,L"Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\Zones\\3",NULL,KEY_SET_VALUE|KEY_QUERY_VALUE,&hKey);
if(ERROR_SUCCESS == dwErr)
{
BYTE data[BUFFER_SIZE] = {0};
DWORD dwType = REG_NONE;

dwErr = RegQueryValueEx(hKey,L"1803",NULL,&dwType,data,&buffersize);
if(ERROR_SUCCESS == dwErr && REG_DWORD == dwType)
{
*((LPDWORD)data) = 0;
dwErr = RegSetValueExW(hKey,L"1803",0,REG_DWORD,data,sizeof(DWORD));
}
RegCloseKey (hKey);
}
[no name] 11-Sep-11 19:07pm    
If your getting an error "No storage class or type specifier" then that would imply that you are missing a type definition. Make sure you are #including windows.h which will pull in the windef.h header. Also make sure you #define BUFFER_SIZE

The value that is written into the registry is here:

*((LPDWORD)data) = 0;

It essentially casts the pointer from the BYTE array into a pointer to a DWORD and then sets the value to zero.

Best Wishes,
-David Delaune
Member 7766180 11-Sep-11 19:23pm    
Thank you, found the problem a "{" was missing! Now this shows up!
Warning 1 warning C4739: reference to variable 'data' exceeds its storage space
Error 2 error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
Error 3 error LNK1120: 1 unresolved externals
[no name] 11-Sep-11 19:29pm    
Sounds like you need to add: #define BUFFER_SIZE 1024
Member 7766180 11-Sep-11 19:30pm    
Thanks I saw that.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900