2

我正在尝试使用 win32 API 访问与 C# 中的设备相关联的驱动程序的信息。

我已经设法启用/禁用设备(所以我正在检索的句柄似乎没问题),但是在尝试调用 SetupDiEnumDriverInfo 时我没有运气。

这是我正在使用的代码:

private List<String> ListCompatibleDrivers(IntPtr hDevInfo, SP_DEVINFO_DATA devInfoData)
{
   List<String> result = new List<String>();
   try
   {
      SP_DRVINFO_DATA drvInfo = new SP_DRVINFO_DATA();

      for (int i = 0; SetupDiEnumDriverInfo(hDevInfo, devInfoData, SPDIT_CLASSDRIVER, i, drvInfo); i++)
      {
         result.Add(drvInfo.Description);
      }

      if (result.Count < 1)
      {
         result.Add(Marshal.GetLastWin32Error().ToString());
      }

      return result;
   }
   catch
   {
      throw;
   }
}

可以假设参数没问题的地方(正如我所说,设置 API 中的其他方法可以成功使用它们)。

这些是可能损坏的结构和 DllImport:

[StructLayout(LayoutKind.Sequential)]
public class SP_DRVINFO_DATA 
{
   public Int32     cbSize;
   public Int32     driverType;
   public UIntPtr    reserved;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
   public String    description;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
   public String    mfgName;
   [MarshalAs(UnmanagedType.ByValTStr, SizeConst=256)]
   public String    providerName;
   public FILETIME   driverDate;
   public Int64  driverVersion;
};

[DllImport("setupapi.dll", SetLastError = true)]
public static extern bool SetupDiEnumDriverInfo(IntPtr lpInfoSet, SP_DEVINFO_DATA deviceInfoData, UInt32 driverType, Int32 memberIndex, SP_DRVINFO_DATA driverInfoData);

API 调用立即返回 false,即Marshal.GetLastWin32Error().ToString()返回.259ERROR_NO_MORE_ITEMS

我只是不明白,我的希望很高我只是犯了一些我看不到的愚蠢错误,因为我最近除了 msdn 几乎没有阅读任何东西,而且它变得非常累......

非常感谢任何帮助,非常感谢!

4

3 回答 3

1

好吧,我可以告诉你你不匹配应该是的函数签名:

[DllImport("setupapi.dll", SetLastError = true, charset=Charset.Unicode)]
[return:MarshalAs(UnmanagedType.Bool)]
private static extern bool SetupDiEnumDriverInfo(
       [In] IntPtr lpInfoSet,
       [In, Optional] SP_DEVINFO_DATA deviceInfoData,
       [In] UInt32 driverType,
       [In] Int32 memberIndex,
       [Out] out SP_DRVINFO_DATA driverInfoData);

这个输出很重要,因为它指定它需要从 PInvoke 中提取数据。

于 2013-06-27T13:55:44.600 回答
1

这个结构有几个问题,最烦人的是必须指定 pack=4 以便本机代码找到正确的入口点。
这有效:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode, Pack = 4)]
    public struct SP_DRVINFO_DATA
    {
        public int cbSize;
        public int DriverType;
        public UInt32 Reserved;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string Description;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string MfgName;
        [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)]
        public string ProviderName;
        public System.Runtime.InteropServices.ComTypes.FILETIME DriverDate;
        public long DriverVersion;
    }

当然,实际上在 P/Invokes 前面加上 也是一个好主意Charset=Charset.Unicode

于 2013-07-02T10:34:59.823 回答
1

以下是适用于 x64 和 x86 的 API 和结构定义。我还添加了 SetupDiGetDriverInfoDetail,很有可能您也需要它。

接口:

DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode)]
       public static extern bool SetupDiGetDriverInfoDetail(
       IntPtr DeviceInfoSet,
       ref SP_DEVINFO_DATA DeviceInfoData,
       ref SP_DRVINFO_DATA DriverInfoData,
       ref SP_DRVINFO_DETAIL_DATA DriverInfoDetailData,
       Int32 DriverInfoDetailDataSize,
       ref Int32 RequiredSize);

[DllImport("setupapi.dll", SetLastError = true, CharSet = CharSet.Unicode)] 
        public static extern bool SetupDiEnumDriverInfo( 
        IntPtr DeviceInfoSet,  
        ref SP_DEVINFO_DATA DeviceInfoData,  
        int DriverType,  
        int MemberIndex,                                          
        ref SP_DRVINFO_DATA DriverInfoData); 

结构:

#if !WIN64
 [StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Unicode)]
#else  
 [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Unicode)]
#endif
        public struct SP_DRVINFO_DATA 
        { 
            public int cbSize; 
            public uint DriverType; 
            public UIntPtr Reserved; 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
            public string Description; 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
            public string MfgName; 
            [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 256)] 
            public string ProviderName;
            public System.Runtime.InteropServices.ComTypes.FILETIME DriverDate; 
            public ulong DriverVersion; 
       }


#if !WIN64
 [StructLayout(LayoutKind.Sequential, Pack = 2, CharSet = CharSet.Unicode)]
#else  
 [StructLayout(LayoutKind.Sequential, Pack = 8, CharSet = CharSet.Unicode)]
#endif
        public 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;
        };
于 2015-03-12T14:14:49.797 回答