public static bool UninstallModem(string COMPort) { Guid classguid = new Guid("4d36e96d-e325-11ce-bfc1-08002be10318"); IntPtr hDeviceInfoSet = Win32Wrapper.SetupDiGetClassDevs(ref classguid, IntPtr.Zero, IntPtr.Zero, (uint)Win32Wrapper.DIGCF.DIGCF_PRESENT); bool matchFound = false; if (hDeviceInfoSet != IntPtr.Zero && hDeviceInfoSet.ToInt64() != INVALID_HANDLE_VALUE) { Win32Wrapper.SP_DEVINFO_DATA devInfoElem = new Win32Wrapper.SP_DEVINFO_DATA(); uint index = 0; devInfoElem.cbSize = (uint)Marshal.SizeOf(devInfoElem); while (Win32Wrapper.SetupDiEnumDeviceInfo(hDeviceInfoSet, index, ref devInfoElem)) { index = index + 1; IntPtr hKeyDev = Win32Wrapper.SetupDiOpenDevRegKey(hDeviceInfoSet, ref devInfoElem, (uint)Win32Wrapper.DICS_FLAG.DICS_FLAG_GLOBAL, 0, (uint)Win32Wrapper.DIREG.DIREG_DRV, (uint)Win32Wrapper.REGKEYSECURITY.KEY_READ); int test = Marshal.GetLastWin32Error(); StringBuilder szDevDesc = new StringBuilder(20, 256); if (hKeyDev.ToInt64() != INVALID_HANDLE_VALUE) { uint pData = 256; uint lpType = 0; int res = Win32Wrapper.RegQueryValueEx(hKeyDev, "AttachedTo", 0, out lpType, szDevDesc, ref pData); if (res == (int)Win32Wrapper.WinErrors.ERROR_SUCCESS) { Win32Wrapper.RegCloseKey(hKeyDev); if (COMPort == szDevDesc.ToString()) { Console.WriteLine(String.Format("Found : {0}", COMPort)); matchFound = true; uint DIF_REMOVE = 0x00000005; if (!Win32Wrapper.SetupDiCallClassInstaller(DIF_REMOVE, hDeviceInfoSet, ref devInfoElem)) { Win32Wrapper.SetupDiDestroyDeviceInfoList(hDeviceInfoSet); } break; } } } } } return(matchFound); }
public static bool InstallModemDriver(string COMPort) { Win32Wrapper.SP_DEVINFO_DATA DeviceInfoData = new Win32Wrapper.SP_DEVINFO_DATA(); DeviceInfoData.cbSize = (uint)Marshal.SizeOf(DeviceInfoData); Guid classguid = new Guid("4d36e96d-e325-11ce-bfc1-08002be10318"); string infFile = Environment.CurrentDirectory + "\\Plugins\\M5-MDM-LS.inf"; //primeworks copies the inf file into its output directory which is plugins //Console.WriteLine("inf file is here: " + infFile); //writer.WriteLine("inf file is here: " + infFile); #if DEBUG //hi debugger, can you copy and paste your inf file into here please? You'll find it in Primeworks//primeprobe2 :) //infFile = "C:\\WINDOWS\\M5-MDM-LS.inf"; infFile = "C:\\Program Files(x86)\\Primayer\\PrimeWorks\\Plugins\\M5-MDM-LS.inf"; #endif string HardwareID = "M5PNPLS"; int lasterror = 0; //writer.WriteLine("Find exisitng device with hardware id = " + HardwareID.ToString()); if (FindExistingDevice(HardwareID, out lasterror)) { //writer.WriteLine("Found device"); //INSTALLFLAG_READONLY = 2 bool restart = false; // writer.WriteLine("Update driver for plug and play device"); if (!Win32Wrapper.UpdateDriverForPlugAndPlayDevices(IntPtr.Zero, HardwareID, infFile, 2, out restart)) { // writer.WriteLine("Failed to update plug and play device"); //int err = Marshal.GetLastWin32Error(); //writer.WriteLine("Get last error returned; " + err.ToString()); return(false); } } else { //ERROR_NO_MORE_ITEMS = 259 if (lasterror != (int)Win32Wrapper.WinErrors.ERROR_NO_MORE_ITEMS) { //writer.WriteLine("Error No more items, returned from find device"); return(false); } //writer.WriteLine("Not found , but ok to continue and install"); } bool res = Win32Wrapper.SetupDiGetINFClass(infFile, ref classguid, "Modem", 256, IntPtr.Zero); IntPtr DeviceInfoSet = Win32Wrapper.SetupDiCreateDeviceInfoList(ref classguid, IntPtr.Zero); if (DeviceInfoSet.ToInt64() == INVALID_HANDLE_VALUE) { //writer.WriteLine("SetupDiCreateDeviceInfoList falied"); return(false); } if (!Win32Wrapper.SetupDiCreateDeviceInfo(DeviceInfoSet, "Modem", ref classguid, "@mdmhayes.inf,%m2700%;Modem M5", IntPtr.Zero, 0x1, ref DeviceInfoData)) { int err = Marshal.GetLastWin32Error(); //writer.WriteLine("SetupDiDestroyDeviceInfoList falied"); Win32Wrapper.SetupDiDestroyDeviceInfoList(DeviceInfoSet); return(false); } if (!Win32Wrapper.SetupDiSetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData, (uint)Win32Wrapper.SPDRP.SPDRP_HARDWAREID, HardwareID, (HardwareID.Length + 2) * Marshal.SystemDefaultCharSize)) { // writer.WriteLine("SetupDiSetDeviceRegistryProperty falied"); Win32Wrapper.SetupDiDestroyDeviceInfoList(DeviceInfoSet); return(false); } Win32Wrapper.SP_DRVINFO_DATA drvData = new Win32Wrapper.SP_DRVINFO_DATA(); drvData.cbSize = (uint)Marshal.SizeOf(drvData); uint DIF_REMOVE = 0x00000005; bool result = true; // writer.WriteLine("register modem..."); if (!RegisterModem(DeviceInfoSet, ref DeviceInfoData, COMPort, ref drvData)) { result = false; } try { //INSTALLFLAG_FORCE = 1 bool restart = false; if (!Win32Wrapper.UpdateDriverForPlugAndPlayDevices(IntPtr.Zero, HardwareID, infFile, 1, out restart)) { //writer.WriteLine("failed to update driver for plug and play"); //int err = Marshal.GetLastWin32Error(); //writer.WriteLine("Get last error returned; " + err.ToString()); result = false; } if (!result) { Win32Wrapper.SetupDiCallClassInstaller(DIF_REMOVE, DeviceInfoSet, ref DeviceInfoData); } Win32Wrapper.SetupDiDestroyDeviceInfoList(DeviceInfoSet); return(result);; } catch { //writer.WriteLine("Exception"); Win32Wrapper.SetupDiDestroyDeviceInfoList(DeviceInfoSet); return(false); } }
public static bool InstallModemDriver(string COMPort) { Win32Wrapper.SP_DEVINFO_DATA DeviceInfoData = new Win32Wrapper.SP_DEVINFO_DATA(); DeviceInfoData.cbSize = (uint)Marshal.SizeOf(DeviceInfoData); Guid classguid = new Guid("4d36e96d-e325-11ce-bfc1-08002be10318"); //little test string infFile = "C:\\WINDOWS\\inf\\M5-MDM-LS.inf"; string HardwareID = "M5PNPLS"; int lasterror = 0; if (FindExistingDevice(HardwareID, out lasterror)) { //INSTALLFLAG_READONLY = 2 bool restart = false; if (!Win32Wrapper.UpdateDriverForPlugAndPlayDevices(IntPtr.Zero, HardwareID, infFile, 2, out restart)) { return(false); } } else { //ERROR_NO_MORE_ITEMS = 259 if (lasterror != (int)Win32Wrapper.WinErrors.ERROR_NO_MORE_ITEMS) { return(false); } } bool res = Win32Wrapper.SetupDiGetINFClass(infFile, ref classguid, "Modem", 256, IntPtr.Zero); IntPtr DeviceInfoSet = Win32Wrapper.SetupDiCreateDeviceInfoList(ref classguid, IntPtr.Zero); if (DeviceInfoSet.ToInt32() == INVALID_HANDLE_VALUE) { return(false); } if (!Win32Wrapper.SetupDiCreateDeviceInfo(DeviceInfoSet, "Modem", ref classguid, "@mdmhayes.inf,%m2700%;Modem M5", IntPtr.Zero, 0x1, ref DeviceInfoData)) { int err = Marshal.GetLastWin32Error(); Win32Wrapper.SetupDiDestroyDeviceInfoList(DeviceInfoSet); return(false); } if (!Win32Wrapper.SetupDiSetDeviceRegistryProperty(DeviceInfoSet, ref DeviceInfoData, (uint)Win32Wrapper.SPDRP.SPDRP_HARDWAREID, HardwareID, (HardwareID.Length + 2) * Marshal.SystemDefaultCharSize)) { Win32Wrapper.SetupDiDestroyDeviceInfoList(DeviceInfoSet); return(false); } Win32Wrapper.SP_DRVINFO_DATA drvData = new Win32Wrapper.SP_DRVINFO_DATA(); drvData.cbSize = (uint)Marshal.SizeOf(drvData); uint DIF_REMOVE = 0x00000005; bool result = true; if (!RegisterModem(DeviceInfoSet, ref DeviceInfoData, COMPort, ref drvData)) { result = false; } try { //INSTALLFLAG_FORCE = 1 bool restart = false; if (!Win32Wrapper.UpdateDriverForPlugAndPlayDevices(IntPtr.Zero, HardwareID, infFile, 1, out restart)) { int err = Marshal.GetLastWin32Error(); result = false; } if (!result) { Win32Wrapper.SetupDiCallClassInstaller(DIF_REMOVE, DeviceInfoSet, ref DeviceInfoData); } Win32Wrapper.SetupDiDestroyDeviceInfoList(DeviceInfoSet); return(result);; } catch { Win32Wrapper.SetupDiDestroyDeviceInfoList(DeviceInfoSet); return(false); } }