【问题标题】:C#: Error in getting device instance handleC#:获取设备实例句柄时出错
【发布时间】:2019-02-06 12:55:36
【问题描述】:

在我的 C# 代码中,我尝试使用 C++ 函数:CM_Locate_DevNodeWCM_Open_DevNode_Key(使用 pinvoke)。 我的代码看起来像这样:

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)
    }
}

调用CM_Locate_DevNodeW 后,devInst 变为1cmStatus 为 0 = CR_SUCCESS。但是对CM_Open_DevNode_Key 的调用失败了。 我不知道CM_Locate_DevNodeW 是否返回CR_SUCCESS 但在devInst 中放入了不正确的数据? ('1' 看起来不像真正的设备实例句柄......)

或者对CM_Open_DevNode_Key的调用是错误的?

我这样声明函数:

[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);

任何帮助将不胜感激!

【问题讨论】:

  • CM_Locate_DevNodeW 不是C++ 函数。这是 Windows API 提供的C 函数。

标签: c# winapi pinvoke device-instance-id


【解决方案1】:

我摆弄了你的代码,这就是我到目前为止所得到的。在阅读了一些文档后,我发现 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);

我尝试运行以下代码

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)
    {
        // 
    }
}

我得到了opencmStatus 51,它对应于CR_ACCESS_DENIED。然后我想“嗯,我不只是请求很多访问权限吗?让我们尝试只读取访问选项”所以我用1KEY_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)
    {
        //
    }
}

它按预期工作。最后,这个版本给了我opencmStatus等于0

我对我的键盘 PCI 标识符进行了所有测试,不知道这是否重要。

【讨论】:

  • 添加 out 关键字足以解决我的问题。即使使用KEY_ALL_ACCESS,我也没有得到CR_ACCESS_DENIED,这可能是因为我的应用程序以管理员权限运行。感谢您快速高效的支持!
猜你喜欢
  • 1970-01-01
  • 2014-05-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-02
  • 2019-01-11
相关资源
最近更新 更多