Re: registry code sample
- From: "Starglider 4 \(Marco Knoester\)" <x@xxxxxx>
- Date: Sun, 25 Jan 2009 18:41:55 +0100
Hi,
I'm sorry for the txt problem. there was a problem with my ISP.
I hope this is the right sample:
int _er=0,used_keys=2;
LPCTSTR subkey="SOFTWARE";
REGSAM rsam=KEY_ALL_ACCESS;
if(RegOpenKeyEx(HKEY_CURRENT_USER,subkey,0,rsam,iphkey[0])!=ERROR_SUCCESS)_er=1;
ihkey[0]=*iphkey[0];
subkey="company";
DWORD options=REG_OPTION_NON_VOLATILE;
LPSECURITY_ATTRIBUTES sa;
sa->nLength=sizeof(SECURITY_ATTRIBUTES);//0
sa->lpSecurityDescriptor=NULL;
sa->bInheritHandle=TRUE;//true
LPDWORD disp;
if(RegCreateKeyEx(ihkey[0],subkey,0,NULL,options,rsam,sa,iphkey[1],disp)!=ERROR_SUCCESS)_er=1;
if(*disp==REG_CREATED_NEW_KEY)
{
//int_values
ihkey[1]=*iphkey[1];
LPCTSTR value_name="val1";
const BYTE* data;
BYTE d=0;
data=&d;
DWORD size=sizeof(DWORD);
if(RegSetValueEx(ihkey[1],value_name,0,REG_DWORD,data,size)!=ERROR_SUCCESS)_er=1;
_registered=0;
value_name="val2";
d=0;
data=&d;
size=sizeof(DWORD);
if(RegSetValueEx(ihkey[1],value_name,0,REG_DWORD,data,size)!=ERROR_SUCCESS)_er=1;
}
assert(_er=0);
for(int i=0;i<used_keys;i++)RegCloseKey(ihkey[i]);
M
"Norman Bullen" <norm@xxxxxxxxxxxxxxxxxxxxxxxxx> schreef in bericht news:xNGdndW7m4O4FuHUnZ2dnUVZ_s7inZ2d@xxxxxxxxxxxxxxxx
Starglider 4 (Marco Knoester) wrote:
Hi,If you want other people to read code that you post, it helps if you
Here is some registry encoding sample code.
(only a sample of my actual solution.)
Please tell me if everything is basically correct, because
it's my first registry codeing attempt,
and i don't want to mess up my registry.
i've studied msdn very good and checked
everyhing a couple of times. but i haven't yet actually
executed the code in real-time.
Here is the code:
int _er=0,used_keys=2;
HKEY ihkey[2];
PHKEY iphkey[2];
LPCTSTR subkey="SOFTWARE";
REGSAM rsam=KEY_ALL_ACCESS;
if(RegOpenKeyEx(HKEY_CURRENT_USER,subkey,0,rsam,iphkey[0])!=ERROR_SUCCESS)_er=1; ihkey[0]=*iphkey[0]; subkey="company"; DWORD options=REG_OPTION_NON_VOLATILE; LPSECURITY_ATTRIBUTES sa; sa->nLength=sizeof(SECURITY_ATTRIBUTES);//0 sa->lpSecurityDescriptor=NULL; sa->bInheritHandle=TRUE;//true LPDWORD disp; if(RegCreateKeyEx(ihkey[0],subkey,0,NULL,options,rsam,sa,iphkey[1],disp)!=ERROR_SUCCESS)_er=1; if(*disp==REG_CREATED_NEW_KEY) { //int_values ihkey[1]=*iphkey[1]; LPCTSTR value_name="val1"; const BYTE* data; BYTE d=0; data=&d; DWORD size=sizeof(DWORD); if(RegSetValueEx(ihkey[1],value_name,0,REG_DWORD,data,size)!=ERROR_SUCCESS)_er=1; _registered=0; value_name="val2"; d=0; data=&d; size=sizeof(DWORD); if(RegSetValueEx(ihkey[1],value_name,0,REG_DWORD,data,size)!=ERROR_SUCCESS)_er=1; } assert(_er=0); for(int i=0;i<used_keys;i++)RegCloseKey(ihkey[i]);Regards,M
format the code in a readable format with white space and carriage returns.
This code will probably crash on the first if statement above.
iphkey[0] is an uninitialized pointer; a protection fault will probably
occur when the OS tries to use it to store a return value.
When you pass a pointer to a Windows API, it needs to be an initialized
pointer to an object of the correct type. In this case you could write
if (RegOpenKeyEx(HKEY_CURRENT_USER, subkey, 0, rsam, &ihkey[0])
!=ERROR_SUCCESS) _er = 1;
You can also write literals in the statement when it helps to make it
more readable:
if (RegOpenKeyEx(HKEY_CURRENT_USER, _T("SOFTWARE"), 0,
KEY_ALL_ACCESS, &ihkey[0]
)!=ERROR_SUCCESS)
_er = 1;
I haven't looked beyond this line.
--
Norm
To reply, change domain to an adult feline.
- Follow-Ups:
- Re: registry code sample
- From: Vincent Fatica
- Re: registry code sample
- From: Scott McPhillips [MVP]
- Re: registry code sample
- References:
- registry code sample
- From: Starglider 4 \(Marco Knoester\)
- Re: registry code sample
- From: Norman Bullen
- registry code sample
- Prev by Date: Re: IDC_STATIC definition missing in resource.h
- Next by Date: Re: IDC_STATIC definition missing in resource.h
- Previous by thread: Re: registry code sample
- Next by thread: Re: registry code sample
- Index(es):
Relevant Pages
|