Example #1
0
        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);
        }
Example #2
0
        public static bool GetUSBDevice(UInt32 VID, UInt32 PID, ref DeviceProperties DP)
        {
            IntPtr IntPtrBuffer = Marshal.AllocHGlobal(BUFFER_SIZE);
            IntPtr h            = IntPtr.Zero;

            Win32Wrapper.WinErrors LastError;
            bool Status = false;

            try
            {
                string DevEnum          = "USB";
                string ExpectedDeviceID = "VID_" + VID.ToString("X4") + "&" + "PID_" + PID.ToString("X4");
                ExpectedDeviceID = ExpectedDeviceID.ToLowerInvariant();

                h = Win32Wrapper.SetupDiGetClassDevs(IntPtr.Zero, DevEnum, IntPtr.Zero, (int)(Win32Wrapper.DIGCF.DIGCF_PRESENT | Win32Wrapper.DIGCF.DIGCF_ALLCLASSES));
                if (h.ToInt32() != INVALID_HANDLE_VALUE)
                {
                    bool Success = true;
                    uint i       = 0;
                    while (Success)
                    {
                        if (Success)
                        {
                            UInt32 RequiredSize = 0;
                            UInt32 RegType      = 0;
                            IntPtr Ptr          = IntPtr.Zero;

                            //Create a Device Info Data structure
                            Win32Wrapper.SP_DEVINFO_DATA DevInfoData = new Win32Wrapper.SP_DEVINFO_DATA();
                            DevInfoData.cbSize = (uint)Marshal.SizeOf(DevInfoData);
                            Success            = Win32Wrapper.SetupDiEnumDeviceInfo(h, i, ref DevInfoData);

                            if (Success)
                            {
                                //Get the required buffer size
                                //First query for the size of the hardware ID, so we can know how big a buffer to allocate for the data.
                                Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_HARDWAREID, ref RegType, IntPtr.Zero, 0, ref RequiredSize);

                                LastError = (Win32Wrapper.WinErrors)Marshal.GetLastWin32Error();
                                if (LastError == Win32Wrapper.WinErrors.ERROR_INSUFFICIENT_BUFFER)
                                {
                                    if (RequiredSize > BUFFER_SIZE)
                                    {
                                        Status = false;
                                    }
                                    else
                                    {
                                        if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_HARDWAREID, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                        {
                                            string HardwareID = Marshal.PtrToStringAuto(IntPtrBuffer);
                                            HardwareID = HardwareID.ToLowerInvariant();
                                            if (HardwareID.Contains(ExpectedDeviceID))
                                            {
                                                Status = true; //Found device
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_FRIENDLYNAME, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.FriendlyName = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_DEVTYPE, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceType = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_CLASS, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceClass = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_MFG, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceManufacturer = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_LOCATION_INFORMATION, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceLocation = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_LOCATION_PATHS, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DevicePath = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_PHYSICAL_DEVICE_OBJECT_NAME, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DevicePhysicalObjectName = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                if (Win32Wrapper.SetupDiGetDeviceRegistryProperty(h, ref DevInfoData, (UInt32)Win32Wrapper.SPDRP.SPDRP_DEVICEDESC, ref RegType, IntPtrBuffer, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceDescription = Marshal.PtrToStringAuto(IntPtrBuffer);
                                                }
                                                StringBuilder sb = new StringBuilder(BUFFER_SIZE);
                                                if (Win32Wrapper.SetupDiGetDeviceInstanceId(h, ref DevInfoData, sb, BUFFER_SIZE, ref RequiredSize))
                                                {
                                                    DP.DeviceInstancePath = sb.ToString();
                                                }
                                                string path = DP.DeviceInstancePath;
                                                string key  = path.Replace("USB\\", "");
                                                key = key.Replace("\\", "+");
                                                key = key.Remove(0, 17);
                                                string deviceID = "VID_" + VID.ToString("X4") + "+" + "PID_" + PID.ToString("X4");
                                                key = deviceID + key;

                                                DP.COMPort = (string)Registry.GetValue(@"HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\FTDIBUS\" + key + @"\0000\Device Parameters",
                                                                                       "PortName", null);

                                                break;
                                            }
                                            else
                                            {
                                                Status = false;
                                            } //End of if (HardwareID.Contains(ExpectedDeviceID))
                                        }
                                    }         //End of if (RequiredSize > BUFFER_SIZE)
                                }             //End of if (LastError == Win32Wrapper.WinErrors.ERROR_INSUFFICIENT_BUFFER)
                            }                 // End of if (Success)
                        }                     // End of if (Success)
                        else
                        {
                            LastError = (Win32Wrapper.WinErrors)Marshal.GetLastWin32Error();
                            Status    = false;
                        }
                        i++;
                    } // End of while (Success)
                }     //End of if (h.ToInt32() != INVALID_HANDLE_VALUE)

                return(Status);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                Win32Wrapper.SetupDiDestroyDeviceInfoList(h); //Clean up the old structure we no longer need.
                Marshal.FreeHGlobal(IntPtrBuffer);
            }
        }
Example #3
0
        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);
            }
        }
Example #4
0
        private static bool FindExistingDevice(string HardwareID, out int lastError)
        {
            const int ERROR_INVALID_DATA        = 13;
            const int ERROR_INSUFFICIENT_BUFFER = 122;

            lastError = 0;
            Guid   id             = Guid.Empty;
            IntPtr hDeviceInfoSet = Win32Wrapper.SetupDiGetClassDevs(ref id, IntPtr.Zero, IntPtr.Zero, (uint)(Win32Wrapper.DIGCF.DIGCF_ALLCLASSES | Win32Wrapper.DIGCF.DIGCF_PRESENT));

            if (hDeviceInfoSet.ToInt64() == INVALID_HANDLE_VALUE)
            {
                return(false);
            }
            Win32Wrapper.SP_DEVINFO_DATA DeviceInfoData = new Win32Wrapper.SP_DEVINFO_DATA();
            DeviceInfoData.cbSize = (uint)Marshal.SizeOf(DeviceInfoData);

            uint i        = 0;
            uint DataType = 0;

            byte[] buffer    = new byte[4];
            IntPtr bufferPtr = Marshal.AllocHGlobal(buffer.Length);

            Marshal.Copy(buffer, 0, bufferPtr, buffer.Length);

            uint requiredSize = 0;

            bool error = false;

            while (Win32Wrapper.SetupDiEnumDeviceInfo(hDeviceInfoSet, i, ref DeviceInfoData))
            {
                i = i + 1;
                while (!Win32Wrapper.SetupDiGetDeviceRegistryProperty(hDeviceInfoSet, ref DeviceInfoData, (uint)Win32Wrapper.SPDRP.SPDRP_HARDWAREID,
                                                                      ref DataType, bufferPtr, (uint)buffer.Length, ref requiredSize))
                {
                    if (Marshal.GetLastWin32Error() == ERROR_INVALID_DATA)
                    {
                        break;
                    }
                    else if (Marshal.GetLastWin32Error() == ERROR_INSUFFICIENT_BUFFER)
                    {
                        //resize buffer
                        Array.Resize(ref buffer, (int)requiredSize);
                        bufferPtr = Marshal.AllocHGlobal(buffer.Length);
                    }
                    else
                    {
                        error = true;
                        break;
                    }
                }

                if (Marshal.GetLastWin32Error() == ERROR_INVALID_DATA)
                {
                    continue;
                }
                if (error)
                {
                    break;
                }

                if (buffer.Length > 0)
                {
                    string str = Marshal.PtrToStringUni(bufferPtr);
                    if (str.ToUpper() == HardwareID.ToUpper())
                    {
                        return(true);
                    }
                }
            }

            Marshal.FreeHGlobal(bufferPtr);
            lastError = Marshal.GetLastWin32Error();

            Win32Wrapper.SetupDiDestroyDeviceInfoList(hDeviceInfoSet);

            return(false);
        }
Example #5
0
        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);
            }
        }