public bool install(string InfFileName, string HardwareID, int RebootRequired, string Port, string AT, string MaximumPortSpeed) { if (File.Exists(InfFileName)) { if (!this.FindExistingDevice(HardwareID)) { this.print(">>枚举设备完毕!", 4); this.print(string.Format(">>未找到硬件ID:{0},正准备注册该ID.......", HardwareID)); if (!this.InstallRootEnumeratedDriver(InfFileName, HardwareID, RebootRequired, Port, AT, MaximumPortSpeed)) { return(false); } } else { this.print(">>枚举设备完毕!", 4); this.print(string.Format(">>找到硬件ID:{0},设备已安装,正准备更新设备.......", HardwareID)); if (InstallMainClass.UpdateDriverForPlugAndPlayDevices((IntPtr)0, HardwareID, InfFileName, 1, ref RebootRequired)) { this.print(">>更新设备成功!", 2); } else { this.print(">>更新设备失败!", 3); return(false); } } return(true); } else { InstallMainClass.bw.ReportProgress(3, "发生错误:安装信息文件丢失!!!"); return(false); } }
private bool InstallRootEnumeratedDriver(string InfFile, string HardwareID, int RebootRequired, string Port, string AT, string MaximumPortSpeed) { InstallMainClass.SP_DEVINFO_DATA sPDEVINFODATum = new InstallMainClass.SP_DEVINFO_DATA(); InstallMainClass._GUID __GUID = new InstallMainClass._GUID(); byte[] numArray = new byte[32]; int num = 0; string str = ""; if (InstallMainClass.SetupDiGetINFClass(InfFile, ref __GUID, numArray, (int)numArray.Length, ref num)) { for (int i = 0; i < num - 1; i++) { char chr = (char)numArray[i]; str = string.Concat(str, chr.ToString()); } this.print(">>正在创建设备信息列表"); IntPtr intPtr = InstallMainClass.SetupDiCreateDeviceInfoList(ref __GUID, (IntPtr)0); sPDEVINFODATum.cbSize = 28; if (InstallMainClass.SetupDiCreateDeviceInfo(intPtr, numArray, ref __GUID, "null", (IntPtr)0, 1, ref sPDEVINFODATum)) { this.print(">>创建设备信息列表成功!", 2); byte[] bytes = Encoding.ASCII.GetBytes(HardwareID); this.print(">>SetDeviceRegistryProperty........."); if (InstallMainClass.SetupDiSetDeviceRegistryProperty(intPtr, ref sPDEVINFODATum, 1, bytes, HardwareID.Length + 2)) { this.print(">>SetDeviceRegistryProperty成功!"); if (this.RegisterModem(intPtr, ref sPDEVINFODATum, Port, AT, MaximumPortSpeed)) { this.print(">>正在安装驱动........"); if (InstallMainClass.UpdateDriverForPlugAndPlayDevices((IntPtr)0, HardwareID, InfFile, 1, ref RebootRequired)) { return(true); } else { this.print(">>安装失败,正在移除设备", 3); if (InstallMainClass.SetupDiCallClassInstaller(5, intPtr, ref sPDEVINFODATum)) { this.print(">>设备移除成功"); return(false); } else { return(false); } } } else { if (InstallMainClass.SetupDiCallClassInstaller(5, intPtr, ref sPDEVINFODATum)) { return(false); } else { return(false); } } } else { InstallMainClass.SetupDiDestroyDeviceInfoList(intPtr); this.print("失败!,已销毁设备信息列表", 3); return(false); } } else { this.print(">>创建设备信息列表失败!", 3); InstallMainClass.SetupDiDestroyDeviceInfoList(intPtr); return(false); } } else { this.print(">>读取inf文件出错!!!", 3); return(false); } }