Beispiel #1
0
        protected bool WriteDataFiles()
        {
            try
            {
                foreach (KeyValuePair <string, byte[]> pair in mDriverResource.Files)
                {
                    // The resource key is a path relative to the driver directory.
                    string filePath = Path.Combine(DriverDirectory, Tagger.TagString(pair.Key));
                    string fileDir  = Path.GetDirectoryName(filePath);

                    InfWizardStatus.Log(CategoryType.InfWriter, StatusType.Info, "writing driver file {0}", filePath);

                    if (!Directory.Exists(fileDir))
                    {
                        Directory.CreateDirectory(fileDir);
                    }

                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }

                    File.WriteAllBytes(filePath, pair.Value);
                }
                return(true);
            }
            catch (Exception ex)
            {
                InfWizardStatus.Log(CategoryType.InfWriter, StatusType.Error, "failed writing data file driver resource: {0}", ex);
            }
            return(false);
        }
        public override bool DeviceFound(DeviceItem item, IntPtr pDeviceInfoSet, ref SetupApi.SP_DEVINFO_DATA DeviceInfoData)
        {
            if (mRemoveDeviceOptions.RemoveByVidPid)
            {
                if (item.VendorID.ToLower() != mRemoveDeviceOptions.DeviceItem.VendorID.ToLower() ||
                    item.ProductID.ToLower() != mRemoveDeviceOptions.DeviceItem.ProductID.ToLower())
                {
                    return(true);
                }
            }
            else
            {
                if (item.mDeviceId != mRemoveDeviceOptions.DeviceItem.mDeviceId)
                {
                    return(true);
                }
            }
            bool bUninstalled;

            if (SetupApi.WindowsVersion >= WindowsVersionType.WINDOWS_7)
            {
                if ((bUninstalled = SetupApi.DiUninstallDevice(IntPtr.Zero, pDeviceInfoSet, ref DeviceInfoData, 0, IntPtr.Zero)) == true)
                {
                    mRemoved++;
                }
                else
                {
                    InfWizardStatus.Log(CategoryType.RemoveDevice, StatusType.Warning | StatusType.Win32Error, "failed uninstalling device.");
                }
            }
            else
            {
                if ((bUninstalled = SetupApi.SetupDiRemoveDevice(pDeviceInfoSet, ref DeviceInfoData)) == true)
                {
                    mRemoved++;
                }
                else
                {
                    InfWizardStatus.Log(CategoryType.RemoveDevice, StatusType.Warning | StatusType.Win32Error, "failed uninstalling device.");
                }
            }
            if (bUninstalled)
            {
                InfWizardStatus.Log(CategoryType.RemoveDevice, StatusType.Success, "device uninstall complete");
            }
            object oInfFileName;

            if (item.mDriverRegistryList != null && !item.mIsSkipServiceName)
            {
                if (item.mDriverRegistryList.TryGetValue("InfPath", out oInfFileName))
                {
                    if (!(SetupApi.SetupUninstallOEMInf(oInfFileName.ToString(), SetupApi.SUOI.FORCEDELETE, IntPtr.Zero)))
                    {
                        InfWizardStatus.Log(CategoryType.RemoveDevice, StatusType.Warning | StatusType.Win32Error, "SetupUninstallOEMInf failed");
                    }
                }
            }
            return(mRemoveDeviceOptions.RemoveByVidPid);
        }
        public static int EnumerateDevices(DeviceEnumeratorInfo deviceEnumeratorInfo)
        {
            int devIndex;

            const uint CM_PROB_PHANTOM = (0x0000002D);   // The devinst currently exists only in the registry

            // Initialize the SP_DEVINFO_DATA structure
            SetupApi.SP_DEVINFO_DATA devInfoData = new SetupApi.SP_DEVINFO_DATA();
            devInfoData.cbSize = (uint)Marshal.SizeOf(typeof(SetupApi.SP_DEVINFO_DATA));

            // Used to parse the DeviceID tokens.
            RegHardwareID regHardwareID = RegHardwareID.GlobalInstance;

            // Used as a buffer for:
            // * SetupDiGetDeviceRegistryProperty
            // * CM_Get_Device_ID
            // * SetupDiGetCustomDeviceProperty
            // * SetupDiGetDeviceProperty
            byte[] propBuffer = new byte[1024];

            // List all connected USB devices
            IntPtr pDevInfo = SetupApi.SetupDiGetClassDevs(0, "USB", deviceEnumeratorInfo.Hwnd, deviceEnumeratorInfo.DICFGFlags);

            if (pDevInfo == IntPtr.Zero || pDevInfo == new IntPtr(-1))
            {
                InfWizardStatus.Log(CategoryType.EnumerateDevices, StatusType.Win32Error, "SetupDiGetClassDevs Failed!");
                return(-1);
            }

            for (devIndex = 0;; devIndex++)
            {
                if (!SetupApi.SetupDiEnumDeviceInfo(pDevInfo, devIndex, ref devInfoData))
                {
                    // Reached the end of the eviceInfo list.
                    InfWizardStatus.Log(CategoryType.EnumerateDevices, StatusType.Info, "device enumeration complete.");
                    break;
                }
                DeviceItem deviceItem = new DeviceItem();

                // SPDRP_DRIVER seems to do a better job at detecting driverless devices than
                // SPDRP_INSTALL_STATE
                RegistryValueKind propertyType;
                int requiredSize;
                if (SetupApi.SetupDiGetDeviceRegistryProperty(pDevInfo,
                                                              ref devInfoData,
                                                              SetupApi.SPDRP.DRIVER,
                                                              out propertyType,
                                                              propBuffer,
                                                              propBuffer.Length,
                                                              out requiredSize))
                {
                    deviceItem.mDriverless = false;

                    // Read all string values from the registry driver key
                    IntPtr hKey = SetupApi.SetupDiOpenDevRegKey(pDevInfo,
                                                                ref devInfoData,
                                                                1,
                                                                0,
                                                                DevKeyType.DRV,
                                                                (int)RegistryKeyPermissionCheck.ReadSubTree);
                    if (hKey != IntPtr.Zero && hKey != new IntPtr(-1))
                    {
                        int index      = 0;
                        int nameLength = 255;
                        int dataLength = 1023;

                        deviceItem.mDriverRegistryList = new Dictionary <string, object>();

                        StringBuilder     sbName  = new StringBuilder(nameLength + 1);
                        StringBuilder     sbValue = new StringBuilder(dataLength + 1);
                        RegistryValueKind regValueType;

                        while (SetupApi.RegEnumValue(hKey, index, sbName, ref nameLength, IntPtr.Zero, out regValueType, sbValue, ref dataLength) == 0)
                        {
                            if (regValueType == RegistryValueKind.String)
                            {
                                deviceItem.mDriverRegistryList.Add(sbName.ToString(), sbValue.ToString());
                            }

                            // Get next key/value index
                            index++;

                            // Reset max lengths
                            nameLength = 255;
                            dataLength = 1023;
                        }
                        SetupApi.RegCloseKey(hKey);
                    }
                }
                else
                {
                    deviceItem.mDriverless = true;
                }

                // [trobinson] patch
                uint status;
                uint pbmNumber;
                deviceItem.mIsConnected = (SetupApi.CM_Get_DevNode_Status(out status, out pbmNumber, devInfoData.DevInst, 0) != SetupApi.CR.NO_SUCH_DEVNODE);
                if (deviceItem.mIsConnected)
                {
                    deviceItem.mIsConnected = ((pbmNumber & CM_PROB_PHANTOM) != CM_PROB_PHANTOM);
                }

                //if (deviceItem.mDriverless && !deviceItem.mIsConnected)
                //    deviceItem.mDriverless = false;

                // Find only the ones that are driverless
                if (deviceEnumeratorInfo.DriverlessOnly && !deviceItem.mDriverless)
                {
                    InfWizardStatus.Log(CategoryType.EnumerateDevices, StatusType.Info, "skipping non driverless device.");
                    continue;
                }

                // Driverless devices will return an error
                InfWizardStatus.Log(CategoryType.EnumerateDevices, StatusType.Info, "driverless device found.");


                // Eliminate USB hubs by checking the driver string
                if (!SetupApi.SetupDiGetDeviceRegistryProperty(pDevInfo,
                                                               ref devInfoData,
                                                               SetupApi.SPDRP.SERVICE,
                                                               out propertyType,
                                                               propBuffer,
                                                               propBuffer.Length,
                                                               out requiredSize))
                {
                    InfWizardStatus.Log(CategoryType.EnumerateDevices, StatusType.Warning, "failed getting SPDRP.SERVICE");
                    deviceItem.mServiceName = String.Empty;
                }
                else
                {
                    deviceItem.mServiceName = GetAsAutoString(propBuffer);
                }

                bool bContinue = true;
                foreach (string skipServiceName in mSkipServiceNames)
                {
                    if (deviceItem.mServiceName.Trim().ToLower() == skipServiceName)
                    {
                        bContinue = false;
                        break;
                    }
                }
                if (!bContinue && deviceEnumeratorInfo.SkipWindowsServices)
                {
                    continue;
                }
                //if (!bContinue)
                //    continue;

                deviceItem.mIsSkipServiceName = !bContinue;

                string[] saHardwareIDs;

                // Retrieve the hardware ID
                if (!SetupApi.SetupDiGetDeviceRegistryProperty(out saHardwareIDs,
                                                               pDevInfo,
                                                               ref devInfoData,
                                                               SetupApi.SPDRP.HARDWAREID))
                {
                    InfWizardStatus.Log(CategoryType.EnumerateDevices, StatusType.Win32Error, "failed getting SPDRP.HARDWAREID");
                    continue;
                }

                if (saHardwareIDs.Length == 0)
                {
                    InfWizardStatus.Log(CategoryType.EnumerateDevices, StatusType.Error, "device does not have any hardware ids");
                    continue;
                }

                for (int hwid = 0; hwid < saHardwareIDs.Length; hwid++)
                {
                    InfWizardStatus.Log(CategoryType.EnumerateDevices, StatusType.Info, "found hardware ID ({0}/{1}): {2}", hwid + 1, saHardwareIDs.Length, saHardwareIDs[hwid]);
                }

                // Get Device ID
                SetupApi.CR r = SetupApi.CM_Get_Device_ID(devInfoData.DevInst, propBuffer, propBuffer.Length, 0);
                if (r != SetupApi.CR.SUCCESS)
                {
                    InfWizardStatus.Log(CategoryType.EnumerateDevices,
                                        StatusType.Error,
                                        "CM_Get_Device_ID:Failed retrieving simple path for device index: {0} HWID:{1} CR error {2}",
                                        devIndex,
                                        saHardwareIDs[0],
                                        r);
                    continue;
                }
                deviceItem.mDeviceId = GetAsAutoString(propBuffer);

                InfWizardStatus.Log(CategoryType.EnumerateDevices,
                                    StatusType.Info,
                                    "{0} USB device {1}: {2}",
                                    deviceItem.mDriverless ? "Driverless" : deviceItem.mServiceName,
                                    devIndex,
                                    deviceItem.mDeviceId);


                string sDeviceDescription;
                if (SetupApi.WindowsVersion < WindowsVersionType.WINDOWS_7)
                {
                    // On Vista and earlier, we can use SPDRP_DEVICEDESC
                    bContinue = SetupApi.SetupDiGetDeviceRegistryProperty(out sDeviceDescription,
                                                                          pDevInfo,
                                                                          ref devInfoData,
                                                                          SetupApi.SPDRP.DEVICEDESC);

                    if (!bContinue)
                    {
                        sDeviceDescription = string.Empty;
                    }
                }
                else
                {
                    // On Windows 7, the information we want ("Bus reported device description") is
                    // accessed through DEVPKEY_Device_BusReportedDeviceDesc
                    try
                    {
                        bContinue = SetupApi.SetupDiGetDeviceProperty(pDevInfo,
                                                                      ref devInfoData,
                                                                      SetupApi.DEVPKEY_Device_BusReportedDeviceDesc,
                                                                      out propertyType,
                                                                      propBuffer,
                                                                      propBuffer.Length,
                                                                      out requiredSize,
                                                                      0);
                    }
                    catch (DllNotFoundException)
                    {
                        //if (SetupDiGetDeviceProperty == NULL)
                        InfWizardStatus.Log(CategoryType.EnumerateDevices,
                                            StatusType.Warning,
                                            "Failed to locate SetupDiGetDeviceProperty() is Setupapi.dll");
                        bContinue = false;
                    }
                    if (bContinue)
                    {
                        sDeviceDescription = GetAsAutoString(propBuffer);
                    }
                    else
                    {
                        // fallback to SPDRP_DEVICEDESC (USB husb still use it)
                        bContinue = SetupApi.SetupDiGetDeviceRegistryProperty(out sDeviceDescription,
                                                                              pDevInfo,
                                                                              ref devInfoData,
                                                                              SetupApi.SPDRP.DEVICEDESC);
                        if (!bContinue)
                        {
                            sDeviceDescription = string.Empty;
                        }
                    }
                }
                if (!bContinue)
                {
                    InfWizardStatus.Log(CategoryType.EnumerateDevices,
                                        StatusType.Warning | StatusType.Win32Error,
                                        "Failed reading read device description for {0}: {1}",
                                        devIndex,
                                        deviceItem.mDeviceId);
                }

                deviceItem.DeviceDescription = sDeviceDescription;
                deviceItem.BaseFilename      = sDeviceDescription;
                MatchCollection matches = regHardwareID.Matches(saHardwareIDs[0]);
                foreach (Match match in matches)
                {
                    foreach (NamedGroup namedGroup in RegHardwareID.NAMED_GROUPS)
                    {
                        RegHardwareID.ENamedGroups groupEnum = (RegHardwareID.ENamedGroups)namedGroup.GroupNumber;
                        Group group = match.Groups[(int)groupEnum];
                        if (!group.Success)
                        {
                            continue;
                        }

                        switch (groupEnum)
                        {
                        case RegHardwareID.ENamedGroups.Vid:
                            deviceItem.VendorID = group.Value;
                            break;

                        case RegHardwareID.ENamedGroups.Pid:
                            deviceItem.ProductID = group.Value;
                            break;

                        case RegHardwareID.ENamedGroups.Rev:
                            //deviceItem.Rev = group.Value;
                            break;

                        case RegHardwareID.ENamedGroups.MI:
                            deviceItem.MI = group.Value;
                            if (deviceItem.MI != string.Empty)
                            {
                                deviceItem.DeviceDescription += String.Format(" (Interface #{0})", deviceItem.mMI);
                            }
                            break;

                        default:
                            throw new ArgumentOutOfRangeException();
                        }
                    }
                }
                if (deviceItem.mVid == 0 && deviceItem.mPid == 0)
                {
                    continue;
                }

                string sManufacturer;
                if (!SetupApi.SetupDiGetDeviceRegistryProperty(out sManufacturer,
                                                               pDevInfo,
                                                               ref devInfoData,
                                                               SetupApi.SPDRP.MFG))
                {
                    sManufacturer = string.Empty;
                }
                deviceItem.Manufacturer = sManufacturer;

                string[] deviceInterfaceGuids = new string[0];
                if (SetupApi.SetupDiGetCustomDeviceProperty(pDevInfo,
                                                            ref devInfoData,
                                                            "DeviceInterfaceGUIDs",
                                                            SetupApi.DICUSTOMDEVPROP.NONE,
                                                            out propertyType,
                                                            propBuffer,
                                                            propBuffer.Length,
                                                            out requiredSize))
                {
                    deviceInterfaceGuids = GetAsAutoStringArray(propBuffer, requiredSize);
                }

                if (deviceInterfaceGuids.Length > 0)
                {
                    deviceItem.DeviceInterfaceGuid = deviceInterfaceGuids[0];
                }

                if (!deviceEnumeratorInfo.DeviceFound(deviceItem, pDevInfo, ref devInfoData))
                {
                    break;
                }

                InfWizardStatus.Log(CategoryType.EnumerateDevices, StatusType.Info, "device description: {0}", deviceItem.DeviceDescription);
            }

            SetupApi.SetupDiDestroyDeviceInfoList(pDevInfo);
            return(devIndex);
        }
Beispiel #4
0
        protected bool WriteStringFiles()
        {
            bool bRtn = true;

            string readmeHtmFile = Path.Combine(DriverDirectory, "README_" + mDeviceItem.BaseFilename + ".htm");

            try
            {
                // All strings without an extention are considered tokens.
                // They are added to the tagger instead of writing to file.
                foreach (KeyValuePair <string, string> pair in mDriverResource.Strings)
                {
                    if (!pair.Key.Contains("."))
                    {
                        Tagger[pair.Key] = pair.Value;
                    }
                }

                foreach (KeyValuePair <string, string> pair in mDriverResource.Strings)
                {
                    // Skip the token strings
                    if (!pair.Key.Contains("."))
                    {
                        continue;
                    }

                    string key      = Tagger.TagString(pair.Key);
                    string filePath = Path.Combine(DriverDirectory, key);

                    if (filePath.ToLower() == readmeHtmFile.ToLower())
                    {
                        continue;
                    }

                    InfWizardStatus.Log(CategoryType.InfWriter, StatusType.Info, "writing driver file {0}", key);

                    // The resource key is a path relative to the driver directory.
                    string fileDir = Path.GetDirectoryName(filePath);
                    if (!Directory.Exists(fileDir))
                    {
                        Directory.CreateDirectory(fileDir);
                    }

                    if (File.Exists(filePath))
                    {
                        File.Delete(filePath);
                    }


                    // Tag the string and write the file.
                    string fileData = Tagger.TagString(pair.Value);
                    File.WriteAllText(filePath, fileData, Encoding.ASCII);
                }

                // Write the libusbdotnet InfWizard readme htm file.
                File.WriteAllText(readmeHtmFile, Tagger.TagString(resInfWizard.SETUP_PACKAGE_README_HTM));
            }
            catch (Exception ex)
            {
                InfWizardStatus.Log(CategoryType.InfWriter, StatusType.Error, "failed writing string driver resource {0}", ex);
                bRtn = false;
            }

            return(bRtn);
        }