设备驱动安装以编程方式失败

时间:2016-11-16 09:01:05

标签: c#

我需要通过C#程序安装驱动程序文件“atm6124_cdc.inf”。我尝试了以下方法。

[DllImport("setupapi.dll", SetLastError = true)]
        [PrincipalPermission(SecurityAction.Demand, Authenticated = false, Role = @"BUILTIN\Administrators")]
        public static extern bool SetupCopyOEMInf(

            string SourceInfFileName,
            string OEMSourceMediaLocation,
            OemSourceMediaType OEMSourceMediaType,
            OemCopyStyle CopyStyle,
            string DestinationInfFileName,
            int DestinationInfFileNameSize,
            ref int RequiredSize,
            string DestinationInfFileNameComponent

        );
        [DllImport("setupapi.dll", SetLastError = true)]
        [PrincipalPermission(SecurityAction.Demand, Authenticated = false, Role = @"BUILTIN\Administrators")]
        public static extern bool UpdateDriverForPlugAndPlayDevices(
        IntPtr hwndParent,
        [MarshalAs(UnmanagedType.LPWStr)] string HardwareId,
        [MarshalAs(UnmanagedType.LPWStr)] string FullInfPath,
        uint InstallFlags,
        ref bool bRebootRequired);

        /// <summary>
        /// Driver media type
        /// </summary>
        public enum OemSourceMediaType
        {
            SPOST_NONE = 0,
            //Only use the following if you have a pnf file as well
            SPOST_PATH = 1,
            SPOST_URL = 2,
            SPOST_MAX = 3
        }

        public enum OemCopyStyle
        {
            SP_COPY_NEWER = 0x0000004,   // copy only if source newer than or same as target
            SP_COPY_NEWER_ONLY = 0x0010000,   // copy only if source file newer than target
            SP_COPY_OEMINF_CATALOG_ONLY = 0x0040000,   // (SetupCopyOEMInf only) don't copy INF--just catalog
        }

        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);

            //System.Diagnostics.Process.Start("http://www.microsoft.com");
            //MessageBox.Show(System.Reflection.Assembly.GetExecutingAssembly().Location);


            try
            {
                int size = 0;

                bool success = SetupCopyOEMInf(
                   "D:\\Guardian8\\atm6124_cdc.inf",
                    "", OemSourceMediaType.SPOST_NONE,
                    OemCopyStyle.SP_COPY_NEWER,
                    null,
                    0,
                    ref size, null);

                bool bRebootRequired = false;

                if (success)
                {
                    MessageBox.Show("Copy success");
                }
                else
                {
                    MessageBox.Show("Copy fail");
                }



                    if (UpdateDriverForPlugAndPlayDevices(IntPtr.Zero, @"USB\VID_03EB&PID_6119", "D:\\Guardian8\\atm6124_cdc.inf", 0, ref bRebootRequired))
                    {
                        MessageBox.Show("Installation success");

                    }
                    else
                    {
                        MessageBox.Show("Installation failed");
                    }
                    if (UpdateDriverForPlugAndPlayDevices(IntPtr.Zero, @"USB\VID_03EB&PID_6119&REV_0100", "D:\\Guardian8\\atm6124_cdc.inf", 0, ref bRebootRequired))
                    {
                        MessageBox.Show("Installation success2");
                    }
                    else
                    {
                        MessageBox.Show("Installation failed2");
                    }


                }
                catch(Exception ex)
                {
                    throw ex;
                }

此方法“UpdateDriverForPlugAndPlayDevices”在此处始终返回false。请帮忙。驱动程序是32位,应用程序也是32位。

0 个答案:

没有答案