Can't read registry key value
Hello,
I'm trying to read a registry key value into struct but only 4 bytes are read:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
#include <windows.h>
#include <winreg.h>
#include <iostream>
using namespace std;
struct mdd // mounted device data
{
unsigned __int32 disk_signature; // mbr disk signature
unsigned __int64 offset; // where partition begins (bytes)
};
// Get data from registry
int get_mdd(char *subkey,mdd *ret)
{
HKEY hkey;
DWORD type=REG_BINARY;
DWORD size=sizeof(mdd); // 12 bytes
memset(ret,0,sizeof(mdd));
RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\MountedDevices",0,KEY_READ,&hkey);
if(hkey==INVALID_HANDLE_VALUE) return -1;
RegQueryValueEx(hkey,subkey,0,&type,(BYTE*)ret,&size);
RegCloseKey(hkey);
return size;
}
int main()
{
mdd data;
get_mdd("\\DosDevices\\C:",&data);
cout <<"Disk signature: "<<hex<<data.disk_signature<<dec<<endl; // works
cout <<"Partition begins in sector "<<data.offset/512<<endl; // doesn't work -- always zero
}
|
I don't know why but
data.offset
is always zero.
Maybe I pass
mdd
to
RegQueryValueEx
function incorrectly?
Thanks for help.
DWORD size=sizeof(mdd); // 12 bytes
As you've probably already noticed, it's the size of a pointer.
DWORD size=sizeof(mdd);
is correct.
The problem with my code was that data in registry is packed while
struct mdd
was not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
|
#include <windows.h>
#include <winreg.h>
#include <iostream>
using namespace std;
#pragma pack(1)
struct mdd // mounted device data
{
unsigned __int32 disk_signature; // mbr disk signature
unsigned __int64 offset; // where partition begins (bytes)
};
#pragma pack(0)
// Get data from registry
int get_mdd(char *subkey,mdd *ret)
{
HKEY hkey;
DWORD type=REG_BINARY;
DWORD size=sizeof(mdd); // 12 bytes
memset(ret,0,sizeof(mdd));
RegOpenKeyEx(HKEY_LOCAL_MACHINE,"SYSTEM\\MountedDevices",0,KEY_READ,&hkey);
if(hkey==INVALID_HANDLE_VALUE) return -1;
RegQueryValueEx(hkey,subkey,0,&type,(BYTE*)ret,&size);
RegCloseKey(hkey);
return size;
}
int main()
{
mdd data;
get_mdd("\\DosDevices\\C:",&data);
cout <<"Disk signature: "<<hex<<data.disk_signature<<dec<<endl;
cout <<"Partition begins in sector "<<data.offset/512<<endl;
}
|
Last edited on
Quite right.
Topic archived. No new replies allowed.