c# 中调用advapi32.dll中的 RegCreateKeyEx如何定义

cz_stu1 2008-07-10 02:23:48
能不能给个c#的实际例子?谢了
...全文
499 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Macosx 2008-07-10
  • 打赏
  • 举报
回复
regData应该是byte[]
cz_stu1 2008-07-10
  • 打赏
  • 举报
回复
我是这样定义的,可就是取不出值,
[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;
帮看那错了
cz_stu1 2008-07-10
  • 打赏
  • 举报
回复
还有一个帮看一下,我是这样定义的,可就是取不出值,
[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;
帮看那错了
Macosx 2008-07-10
  • 打赏
  • 举报
回复
这是用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;
}



ericzhangbo1982111 2008-07-10
  • 打赏
  • 举报
回复
// RegQueryValueEx
//

[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
你也可以不用。
pupo 2008-07-10
  • 打赏
  • 举报
回复
[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;
}
ericzhangbo1982111 2008-07-10
  • 打赏
  • 举报
回复
[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
);

111,051

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧