[System.Runtime.InteropServices.DllImport("advapi32.dll")]
private static extern int RegCreateKeyEx(
uint hKey,
string lpSubKey,
uint Reserved,
string lpClass,
uint dwOptions,
uint samDesired,
uint lpSecurityAttributes,
ref uint phkResult,
ref uint lpdwDisposition
);
private static extern int RegCreateKeyEx(
uint hKey,
string lpSubKey,
uint Reserved,
string lpClass,
uint dwOptions,
uint samDesired,
uint lpSecurityAttributes,
ref uint phkResult,
ref uint lpdwDisposition
);
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;
}
//[System.Runtime.InteropServices.DllImport("coredll.dll")]
private unsafe static extern int RegQueryValueEx(
uint hKey,
string lpValueName,
uint lpReserved,
out uint lpType,
byte * lpData,
ref uint lpcbData
);
//
// RegOpenKeyEx
//
System.Runtime.InteropServices.DllImport("coredll.dll")]
private static extern int RegOpenKeyEx(
uint hKey,
string lpSubKey,
uint ulOptions,
uint samDesired,
ref uint phkResult
); 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]; }
return new string (str);
}
这是在mobile5.0上运行的把coredll改掉
我还用到了unsafe
你也可以不用。
[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;
}
[DllImport("advapi32.dll", EntryPoint = "RegQueryValueExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int RegQueryStringValue(int hKey, string lpValueName, int lpReserved, ref int lpType,string lpData, ref int lpcbData);string regData = "";
regDataLength = 255;
success = RegQueryStringValue(hKey, value, 0, ref regType, regData, ref regDataLength);
if (success == 0)
functionReturnValue = regData;
帮看那错了
[DllImport("advapi32.dll", EntryPoint = "RegQueryValueExA", CharSet = CharSet.Ansi, SetLastError = true, ExactSpelling = true)]
public static extern int RegQueryStringValue(int hKey, string lpValueName, int lpReserved, ref int lpType,string lpData, ref int lpcbData);string regData = "";
regDataLength = 255;
success = RegQueryStringValue(hKey, value, 0, ref regType, regData, ref regDataLength);
if (success == 0)
functionReturnValue = regData;
帮看那错了