NO

Author Topic: Never seen this one before....  (Read 3439 times)

CommonTater

  • Guest
Never seen this one before....
« on: September 25, 2010, 03:23:59 AM »
I have a subroutine that simply loads a REG_BINARY block of about 250 bytes into memory...

Code: [Select]

// load from registry
BOOL LoadRemote(PTCHAR Name,pREMCTRL Remote)
  { DWORD ds = sizeof(REMCTRL);  
    memset(Remote,0,ds);
    if (wcslen(Name) < 1)
      return 0;
    return (SHGetValue(REG_UHIVE,REG_REMOTES,Name,NULL,Remote,&ds) == ERROR_SUCCESS); }

This works flawlessly for me when I create the target REMCTRL on the stack.
However when I malloc a block of memory it does not work....

 LoadRemote(rname,&uRemote);  works
 LoadRemote(Pointer->rname,&uRemote);  does not.

Code: [Select]
// FAIL!
if (LoadRemote(Programs[x]->Type.Remote,&Programs[x]->Remote))
    Beep(500,200);  


I've checked the LoadRemote procedure is getting the correct name. It is failing in the call to SHGetValue but only with the pointer to heap version.  

If I put in a string literal instead of the pointer, it works.
If I pass the pointer through an intermediate variable (on the stack) it works.
If I use the usual string on stack method (CHAR) it works.

Why?

Calls to RegQueryValueEx behave exactly the same way.

Now this project needs this to work both ways... The procedure is called from at least 4 different places in the code and it enables one of the core features of my project.  Need a little help here guys.

CommonTater

  • Guest
Re: Never seen this one before....
« Reply #1 on: September 25, 2010, 04:59:45 AM »
GOT IT!

Turns out to be an alignment issue caused by packing the structs involved on 1 byte boundaries.  Apparently windows likes 4 (32 bits)...  Dump the #pragma pack and everything worked.

I gotta say, that's a new one on me...