[英]C#: Error in getting device instance handle
In my C# code I am trying to use C++ functions: CM_Locate_DevNodeW
and CM_Open_DevNode_Key
(using pinvoke ).在我的 C# 代码中,我试图使用 C++ 函数:
CM_Locate_DevNodeW
和CM_Open_DevNode_Key
(使用pinvoke )。 My code seems something like this:我的代码看起来像这样:
String deviceId = "PCI\\VEN_8086&DEV_591B&SUBSYS_22128086&REV_01\\3&11583659&0&10";
int devInst = 0;
cmStatus = CM_Locate_DevNodeW(&devInst, deviceId, CM_LOCATE_DEVNODE_NORMAL);
if (cmStatus == CR_SUCCESS)
{
UIntPtr pHKey = new UIntPtr();
cmStatus = CM_Open_DevNode_Key(devInst, KEY_ALL_ACCESS, 0, RegDisposition_OpenExisting, pHKey, CM_REGISTRY_SOFTWARE);
if (cmStatus == CR_SUCCESS)
{
//but here cmStatus=3 (Invalid Pointer)
}
}
After calling to CM_Locate_DevNodeW
, the devInst
becomes 1
, and the cmStatus
is 0 = CR_SUCCESS
.调用
CM_Locate_DevNodeW
, devInst
变为1
, cmStatus
为 0 = CR_SUCCESS
。 But the call to CM_Open_DevNode_Key
fails.但是对
CM_Open_DevNode_Key
的调用失败了。 I don't know if CM_Locate_DevNodeW
returns CR_SUCCESS
but puts incorrect data within devInst
?我不知道
CM_Locate_DevNodeW
返回CR_SUCCESS
但在devInst
放入了不正确的数据? ('1' does not seems like real device instance handle...) (“1”似乎不像真正的设备实例句柄...)
Or maybe the call to CM_Open_DevNode_Key
is wrong?或者对
CM_Open_DevNode_Key
的调用CM_Open_DevNode_Key
是错误的?
I declared the functions like this:我声明了这样的函数:
[DllImport("cfgmgr32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern unsafe int CM_Locate_DevNodeW(
int* pdnDevInst,
string pDeviceID,
ulong ulFlags);
[DllImport("cfgmgr32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern unsafe int CM_Open_DevNode_Key(
int dnDevNode,
int samDesired,
int ulHardwareProfile,
int Disposition,
IntPtr phkDevice,
int ulFlags);
Any help would be appreciated!任何帮助,将不胜感激!
I fiddled around with your code and this is what I've got so far.我摆弄了你的代码,这就是我到目前为止所得到的。 After reading some docs I found out that
phkDevice
parameter of CM_Open_DevNode_Key
function was likely an out
parameter so I updated function signature在阅读了一些文档后,我发现
CM_Open_DevNode_Key
函数的phkDevice
参数可能是一个out
参数,所以我更新了函数签名
[DllImport("cfgmgr32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
public static extern unsafe int CM_Open_DevNode_Key(
int dnDevNode,
int samDesired,
int ulHardwareProfile,
int Disposition,
out IntPtr phkDevice, //added out keyword
int ulFlags);
And I tried to run the following code我尝试运行以下代码
IntPtr pHKey;
string deviceId = @"my keyboard pci id";
int devInst = 0;
int cmStatus = CM_Locate_DevNodeW(&devInst, deviceId, CM_LOCATE_DEVNODE_NORMAL);
if (cmStatus == CR_SUCCESS)
{
int opencmStatus = CM_Open_DevNode_Key(devInst, KEY_ALL_ACCESS, 0, RegDisposition_OpenExisting, out pHKey, CM_REGISTRY_SOFTWARE);
if (opencmStatus == CR_SUCCESS)
{
//
}
}
I got opencmStatus
51
which corresponds to CR_ACCESS_DENIED
.我得到了对应于
CR_ACCESS_DENIED
opencmStatus
51
。 Then I thought " Hmm, don't I just requesting to much access? Let's try only read access options " So I replaced KEY_ALL_ACCESS
with 1
( KEY_QUERY_VALUE
) and ran the following code然后我想“嗯,我不只是请求很多访问权限吗?让我们尝试只读取访问选项”所以我用
1
( KEY_QUERY_VALUE
)替换了KEY_ALL_ACCESS
并运行了以下代码
IntPtr pHKey;
string deviceId = @"my keyboard pci id";
int devInst = 0;
int cmStatus = CM_Locate_DevNodeW(&devInst, deviceId, CM_LOCATE_DEVNODE_NORMAL);
if (cmStatus == CR_SUCCESS)
{
int opencmStatus = CM_Open_DevNode_Key(devInst, 1, 0, RegDisposition_OpenExisting, out pHKey, CM_REGISTRY_SOFTWARE);
if (opencmStatus == CR_SUCCESS)
{
//
}
}
It worked as expected.它按预期工作。 Finally, this version gave me
opencmStatus
equals to 0
.最后,这个版本给了我
opencmStatus
等于0
。
I did all tests against my keyboard PCI identifier, dont know if it matters.我对我的键盘 PCI 标识符做了所有测试,不知道它是否重要。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.