Re: registry code sample



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,

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

If you want other people to read code that you post, it helps if you
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.


Relevant Pages

  • Re: VS2005 compatibility
    ... There's no 'this' pointer passed to registry calls... ... as calling the Registry APIs directly. ... since MFC projects cannot even run on the target. ...
    (microsoft.public.windowsce.app.development)
  • Re: Need help with COM dll registration at runtime
    ... It's a flag in another format specifier, it can't be used on its own. ... Iff, instead of ri, or some variable where I have copied the CLSID ... that DLL, call DllGetClassObject in it, and obtain an interface pointer. ... You seem to be hard-coding CLSID_IPTPlugin into the registry key name, ...
    (microsoft.public.vc.language)
  • Re: XXX_Open not being called !!
    ... I checked the registry and it checks out as follows: ... The TRC_Init function returns a pointer to a structure. ... PUSBTRC_CONTEXT pDrvCtxt = NULL; ... // Allocate a device instance structure. ...
    (microsoft.public.windowsce.platbuilder)