首页 新闻 搜索 专区 学院

程序自动安装设备驱动的问题

1
悬赏园豆:50 [已解决问题] 解决于 2009-02-14 23:13

我需要用程序自动modem驱动程序,使用的系统API:SetupDiGetINFClass、SetupDiCreateDeviceInfoList、SetupDiCreateDeviceInfo、SetupDiSetDeviceRegistryProperty、SetupDiCallClassInstaller,UpdateDriverForPlugAndPlayDevices 前面的几个API都通过了,可在用UpdateDriverForPlugAndPlayDevices 它时会出现问题。

当然没通过设备管理器安装modem后,它就会报错使用GetLastError获取到一串数字也查不到是什么意思,如果有安装后就会成功,但安装的设备信息不全。

以下是我的代码:

API:

[DllImport("setupapi.dll", SetLastError = true)]
        static extern bool SetupDiGetINFClass(
             string infName,
             ref  AllStruct.GUID ClassGuid,
             string ClassName,
             int ClassNameSize,
             int RequiredSize
         );

[DllImport("setupapi.dll", SetLastError = true)]
        static extern IntPtr SetupDiCreateDeviceInfoList(
         ref  AllStruct.GUID ClassGuid,
          IntPtr hwndParent
         );

[DllImport("Setupapi.dll")]
        public static extern bool SetupDiCreateDeviceInfo(IntPtr DeviceInfoSet, string DeviceName, ref AllStruct.GUID ClassGuid, string DeviceDescription, IntPtr hwndParent,
            int CreationFlags, ref AllStruct.SP_DEVINFO_DATA DeviceInfoData);

[DllImport("setupapi.dll", SetLastError = true)]
        static extern bool SetupDiSetDeviceRegistryProperty(
              IntPtr DeviceInfoSet,
             ref AllStruct.SP_DEVINFO_DATA DeviceInfoData,
             int Property,
             string PropertyBuffer,
            int PropertyBufferSize
        );
        [DllImport("setupapi.dll", SetLastError = true)]
        static extern bool SetupDiCallClassInstaller(
             int InstallFunction,
             IntPtr DeviceInfoSet,
             ref AllStruct.SP_DEVINFO_DATA DeviceInfoData
         );

        [DllImport("newdev.dll", SetLastError = true)]
        static extern bool UpdateDriverForPlugAndPlayDevices(
              IntPtr hwndParent,
              string HardwareId,
              string FullInfPath,
              int InstallFlags,
              bool bRebootRequired

  );

[DllImport("setupapi.dll", SetLastError = true)]
        static extern bool SetupDiDestroyDeviceInfoList(
            IntPtr DeviceInfoSet
        );

[DllImport("kernel32.dll", SetLastError = true, CharSet = CharSet.Auto)]
        static extern int GetLastError();

struct:

[StructLayout(LayoutKind.Sequential)]
     public struct SP_DEVINFO_DATA
        {
            public int cbSize;
            public Guid ClassGuid;
            public IntPtr DevInst;
            public IntPtr Reserved;
        }

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)]
        public struct GUID
        {
            public uint Data1;
            public ushort Data2;
            public ushort Data3;
            [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)]
            public byte[] Data4;
        }

代码:

 public static bool SetupModemDevices(string inf, string hardwareID)
        {
            int num = 0;
            string className = "标准 33600 bps 调制解调器";

            AllStruct.GUID guid = new AllStruct.GUID();

            bool getInfo = SetupDiGetINFClass(inf, ref guid, className, 32, 0);
            if (getInfo == false)
            {
                return false;
            }

            IntPtr DeviceInfoSet = SetupDiCreateDeviceInfoList(ref guid, IntPtr.Zero);
          
            AllStruct.SP_DEVINFO_DATA da = new AllStruct.SP_DEVINFO_DATA();
            da.cbSize = Marshal.SizeOf(typeof(AllStruct.SP_DEVINFO_DATA));
            bool success = SetupDiCreateDeviceInfo(DeviceInfoSet, "MODEM", ref guid, "标准 33600 bps 调制解调器", IntPtr.Zero, 0x00000001, ref da);
            if (success == false)
            {
                num = GetLastError();
                return false;
            }

            Buffer.szBufferHwID =hardwareID;

            bool result = SetupDiSetDeviceRegistryProperty(DeviceInfoSet, ref da, SPDRP_HARDWAREID, Buffer.szBufferHwID, 2000);
            if (result == false)
            {
                SetupDiDestroyDeviceInfoList(DeviceInfoSet);
                return false;
            }

            result = SetupDiCallClassInstaller(DIF_REGISTERDEVICE, DeviceInfoSet, ref da);
            if (result == false)
            {
                SetupDiDestroyDeviceInfoList(DeviceInfoSet);
                return false;
            }
            bool rest = false;
            result = UpdateDriverForPlugAndPlayDevices(IntPtr.Zero, hardwareID, inf, 0, rest);
            if (result == false)
            {
                num = GetLastError();
                result = SetupDiCallClassInstaller(0x00000005, DeviceInfoSet, ref da);
            }

            return success;
        }

秋叶的主页 秋叶 | 初学一级 | 园豆:130
提问于:2009-01-30 19:13
< >
分享
最佳答案
0

建议采用DevCon:

http://support.microsoft.com/kb/311272/zh-cn

用法:

http://blog.csdn.net/osion/archive/2007/10/01/1808752.aspx

用C#写一个批处理的执行,最重要的是找到VID,驱动的INF文件提供了这个参数。

winzheng | 大侠五级 |园豆:8797 | 2009-02-01 17:09
其他回答(1)
0

学习....

Jared.Nie | 园豆:1940 (小虾三级) | 2009-02-01 17:49
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册