Home > Net >  . Framework3.5 32-bit application reads the registry on a 64 - bit system
. Framework3.5 32-bit application reads the registry on a 64 - bit system

Time:11-19

Due to adaptation xp, Windows 7, win10 system, such as choice. Framework3.5, compile option is Mixed Platforms
Query information is 32-bit applications can't directly read a 64 - bit system registry, online to find the code to run the result read less than the corresponding values
 class ReadRegedit64 
{
64 # region 32-bit programs to read and write registry

The static UIntPtr HKEY_CLASSES_ROOT=(UIntPtr) 0 x80000000;
The static UIntPtr HKEY_CURRENT_USER=0 x80000001 (UIntPtr);
The static UIntPtr HKEY_LOCAL_MACHINE=0 x80000002 (UIntPtr);
The static UIntPtr HKEY_USERS=0 x80000003 (UIntPtr);
The static UIntPtr HKEY_CURRENT_CONFIG=0 x80000005 (UIntPtr);

//close 64 (file system) operation to
[DllImport (" Kernel32. DLL, "CharSet=CharSet. Auto, SetLastError=true)]
Public static extern bool Wow64DisableWow64FsRedirection (ref IntPtr PTR);
//to open 64 (file system) to
[DllImport (" Kernel32. DLL, "CharSet=CharSet. Auto, SetLastError=true)]
Public static extern bool Wow64RevertWow64FsRedirection IntPtr (PTR);

//the retrieval is the Key value handle
[DllImport (" Advapi32. DLL, "CharSet=CharSet. Auto, SetLastError=true)]
Public static extern uint RegOpenKeyEx (UIntPtr hkeys, string lpSubKey, uint ulOptions, int samDesired, out IntPtr phkResult);
//close the registry to disable specific item registry reflection)
[DllImport (" Advapi32. DLL, "CharSet=CharSet. Auto, SetLastError=true)]
Public static extern long RegDisableReflectionKey (IntPtr hkeys);
//can make the registry to specific items (open registry reflection)
[DllImport (" Advapi32. DLL, "CharSet=CharSet. Auto, SetLastError=true)]
Public static extern long RegEnableReflectionKey (IntPtr hkeys);
//get the Key value (i.e., the handle to the Key value mark the Key objects of value)
[DllImport (" Advapi32. DLL, "CharSet=CharSet. Auto, SetLastError=true)]
Private static extern int RegQueryValueEx (IntPtr hkeys, string lpValueName, int lpReserved,
Out uint lpType, System. Text. StringBuilder lpData,
Ref uint lpcbData);

Private static UIntPtr TransferKeyName (string keyName)
{
The switch (keyName)
{
Case "HKEY_CLASSES_ROOT" :
Return the HKEY_CLASSES_ROOT;
Case "HKEY_CURRENT_USER" :
Return HKEY_CURRENT_USER;
Case "HKEY_LOCAL_MACHINE" :
Return HKEY_LOCAL_MACHINE.
Case "HKEY_USERS" :
Return the HKEY_USERS;
Case "HKEY_CURRENT_CONFIG" :
Return HKEY_CURRENT_CONFIG;
}

Return the HKEY_CLASSES_ROOT;
}

Public static string Get64BitRegistryKey (string parentKeyName, string subKeyName, string keyName)
{
Int KEY_QUERY_VALUE=https://bbs.csdn.net/topics/(0 x0001);
Int KEY_WOW64_64KEY=(0 x0100);
Int KEY_ALL_WOW64=(KEY_QUERY_VALUE | KEY_WOW64_64KEY);

Try
{
//the Windows registry keys into a plastic handle without plus or minus (related to the platform is 32 or 64 bit)
UIntPtr hkeys=TransferKeyName (parentKeyName);

//statement is going to get a handle to the Key value
IntPtr pHKey=IntPtr. Zero;

//record read into Key value
The StringBuilder result=new StringBuilder (". "PadLeft (1024));
Uint resultSize=1024;
Uint lpType=0;

//close the file system to
IntPtr oldWOW64State=new IntPtr ();
If (Wow64DisableWow64FsRedirection (ref oldWOW64State))
{
//get a handle to the Key value
RegOpenKeyEx (hkeys, subKeyName, 0, KEY_ALL_WOW64, out pHKey);

//close to registry (banning certain of registry reflection)
RegDisableReflectionKey (pHKey);

//to get access to the Key values of
RegQueryValueEx (pHKey keyName, 0, out lpType, result, ref resultSize);

//open the registry to specific items (open registry reflection)
RegEnableReflectionKey (pHKey);
}

//open the file system to
Wow64RevertWow64FsRedirection (oldWOW64State);

//return Key value
Return the result. The ToString (). The Trim ();
}
The catch (Exception e)
{
MessageBox. Show (e. oString ());
return null;
}
}

# endregion
}

Bosses give some advice how to do
  • Related