这是用Reflector搞出来的
[DllImport("advapi32.dll", CharSet=CharSet.Auto)]
internal static extern int RegCreateKeyEx(SafeRegistryHandle hKey, string lpSubKey, int Reserved, string lpClass, int dwOptions, int samDesigner, SECURITY_ATTRIBUTES lpSecurityAttributes, out SafeRegistryHandle hkResult, out int lpdwDisposition);
需要把SafeRegistryHandle换成IntPtr
[StructLayout(LayoutKind.Sequential)]
internal class SECURITY_ATTRIBUTES
{
internal int nLength;
internal unsafe byte* pSecurityDescriptor = null;
internal int bInheritHandle;
}
private static string GetStringValue(uint Key, string Name)
{
uint buflen = 0;
uint type = 0;
char []str = null;
unsafe
{
//Get the length of the value:
int r = RegQueryValueEx(Key,Name,0,out type,null,ref buflen);
if (type!=(uint)RegType.REG_SZ) throw new Exception ("The key is not of string value");
//Create a buffer for the value:
byte[] rez_buf = new byte ;
//Retrieve the value to the buffer
fixed(byte *charpointer=&rez_buf[0])
{
int rez = RegQueryValueEx(Key,Name,0,out type,charpointer,ref buflen);
if (rez != 0) throw new Exception ("Can't get value");
}
str = new char ;
for (int i=0; i<buflen; i++)
str[i] = (char)rez_buf[i];
[DllImport("advapi32.dll", EntryPoint="RegCreateKeyEx")]
public static extern int RegCreateKeyEx (
int hKey,
string lpSubKey,
int Reserved,
string lpClass,
int dwOptions,
int samDesired,
ref SECURITY_ATTRIBUTES lpSecurityAttributes,
ref int phkResult,
ref int lpdwDisposition
);
[StructLayout(LayoutKind.Sequential)]
public struct SECURITY_ATTRIBUTES {
public int nLength;
public int lpSecurityDescriptor;
public int bInheritHandle;
}