我在c#中使用setup.api。有成功的工作,但SetupDiGetDriverInfoDetail。当程序执行该行时,它将弹出异常消息
SetupDiGetDriverInfoDetail'使堆栈失衡。这很可能是因为托管PInvoke签名与非托管目标签名不匹配。检查PInvoke签名的调用约定和参数是否与目标匹配。
我认为错误是因为声明签名:
[StructLayout(LayoutKind.Sequential, Pack = 4, CharSet = CharSet.Ansi)]
internal struct SP_DRVINFO_DETAIL_DATA
{
public Int32 cbSize;
public System.Runtime.InteropServices.ComTypes.FILETIME InfDate;
public Int32 CompatIDsOffset;
public Int32 CompatIDsLength;
public IntPtr Reserved;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public String SectionName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 260)]
public String InfFileName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
public String DrvDescription;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 1)]
public String HardwareID;
};
[DllImport("setupapi.dll", SetLastError = true, CallingConvention = CallingConvention.Cdecl)]
static extern uint SetupDiGetDriverInfoDetail(
IntPtr DeviceInfoSet,
SP_DEVINFO_DATA DeviceInfoData,
SP_DRVINFO_DATA DriverInfoData,
ref SP_DRVINFO_DETAIL_DATA DriverInfoDetailData,
Int32 DriverInfoDetailDataSize,
ref Int32 RequiredSize);
我使用以下代码:
private string GetDrivInfoDetailHWID()
{
SP_DRVINFO_DETAIL_DATA driInfoDetailData = new SP_DRVINFO_DETAIL_DATA();
Int32 requiredSize = 0;
driInfoDetailData.cbSize = System.Runtime.InteropServices.Marshal.SizeOf(typeof(SP_DRVINFO_DETAIL_DATA));
while(SetupDiGetDriverInfoDetail(_deviceHandle, _devInfoData, _driInfoData, ref driInfoDetailData, requiredSize, ref requiredSize)==0)
{
int e = Marshal.GetLastWin32Error();
}
return driInfoDetailData.HardwareID;
}
请帮我解决这个问题,我真的很感激。