【发布时间】:2019-02-06 12:55:36
【问题描述】:
在我的 C# 代码中,我尝试使用 C++ 函数:CM_Locate_DevNodeW 和 CM_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 变为1,cmStatus 为 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