在我的 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)
}
}
调用 to 后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);
任何帮助,将不胜感激!