Windows Update API C#代码无法获取更新历史记录

发布于 2024-07-22 05:25:06 字数 3244 浏览 5 评论 0原文

我正在编写一段代码,用于使用公司 Intranet 中的安全修补程序自动修补 Windows 操作系统。 (我使用的是 Visual Studio .NET,即 .NET Framework 1.1,并使用 C# 进行开发。)
作为第一步,我想列出已使用 WUApi 应用于系统的修补程序。 我添加了对“tlbimped”wuapi.dll 的引用,我还使用 regsvr32 注册了原始 dll,Windows 更新服务似乎已在服务管理器中启动并运行,但代码拒绝工作:它返回零作为应用的修补程序的数量,即使是在应用修补程序之后,它也会在到达“QueryHistory”函数时抛出 COMException。
代码如下:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Security;
using System.Security.Permissions;
using WUApiInterop;

namespace Hotfix_Scanner {
    public class Form1 : System.Windows.Forms.Form {
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;

        private UpdateSession session;
        private UpdateSearcher searcher;
        private int count;
        private IUpdateHistoryEntryCollection history;

        private System.ComponentModel.Container components = null;

        public Form1() {
            InitializeComponent();
            SecurityPermission sp = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
            sp.Demand();

            PermissionSet fullTrust = new PermissionSet(PermissionState.Unrestricted);
            fullTrust.Demand();

            session = new UpdateSession();
            searcher = session.CreateUpdateSearcher();
            count = searcher.GetTotalHistoryCount();
            history = searcher.QueryHistory(0, (count - 1));
        }

        protected override void Dispose(bool disposing) {
            if(disposing) {
                if (components != null)  {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        private void InitializeComponent() {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();

            this.button1.Location = new System.Drawing.Point(216, 240);
            this.button1.Name = "button1";
            this.button1.TabIndex = 0;
            this.button1.Text = "Scan";
            this.button1.Click += new System.EventHandler(this.button1_Click);

            this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(292, 232);
            this.textBox1.TabIndex = 1;
            this.textBox1.Text = "";

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
        }

        [STAThread]
        static void Main()  {
            Application.Run(new Form1());
        }

        private void button1_Click(object sender, System.EventArgs e) {
            for (int i = 0; i < count; ++i) {
                textBox1.Text += history[i].Title + "\n";
            } // for loop
            return;
        }
    }
}

I'm writing a code to automatically patch Windows Operation Systems with security hotfixes in a corporate intranet. (I'm using Visual Studio .NET, thus .NET framework 1.1, and developing in c#.)
As a first step, I'd like to list the hotfixes already applied to the system using the WUApi. I've added the reference to the "tlbimped" wuapi.dll, I've also registered the original dll with regsvr32, windows update service seems to be up and running in the service manager, yet the code refuses to work: it returns zero as the number of applied hotfixes, even right after applying a hotfix, and it throws a COMException at reaching the "QueryHistory" function.
The code is as follows:

using System;
using System.ComponentModel;
using System.Windows.Forms;
using System.Security;
using System.Security.Permissions;
using WUApiInterop;

namespace Hotfix_Scanner {
    public class Form1 : System.Windows.Forms.Form {
        private System.Windows.Forms.Button button1;
        private System.Windows.Forms.TextBox textBox1;

        private UpdateSession session;
        private UpdateSearcher searcher;
        private int count;
        private IUpdateHistoryEntryCollection history;

        private System.ComponentModel.Container components = null;

        public Form1() {
            InitializeComponent();
            SecurityPermission sp = new SecurityPermission(SecurityPermissionFlag.UnmanagedCode);
            sp.Demand();

            PermissionSet fullTrust = new PermissionSet(PermissionState.Unrestricted);
            fullTrust.Demand();

            session = new UpdateSession();
            searcher = session.CreateUpdateSearcher();
            count = searcher.GetTotalHistoryCount();
            history = searcher.QueryHistory(0, (count - 1));
        }

        protected override void Dispose(bool disposing) {
            if(disposing) {
                if (components != null)  {
                    components.Dispose();
                }
            }
            base.Dispose( disposing );
        }

        private void InitializeComponent() {
            this.button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();

            this.button1.Location = new System.Drawing.Point(216, 240);
            this.button1.Name = "button1";
            this.button1.TabIndex = 0;
            this.button1.Text = "Scan";
            this.button1.Click += new System.EventHandler(this.button1_Click);

            this.textBox1.Dock = System.Windows.Forms.DockStyle.Top;
            this.textBox1.Location = new System.Drawing.Point(0, 0);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(292, 232);
            this.textBox1.TabIndex = 1;
            this.textBox1.Text = "";

            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(292, 266);
            this.Controls.Add(this.textBox1);
            this.Controls.Add(this.button1);
            this.Name = "Form1";
            this.Text = "Form1";
            this.ResumeLayout(false);
        }

        [STAThread]
        static void Main()  {
            Application.Run(new Form1());
        }

        private void button1_Click(object sender, System.EventArgs e) {
            for (int i = 0; i < count; ++i) {
                textBox1.Text += history[i].Title + "\n";
            } // for loop
            return;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一身仙ぐ女味 2024-07-29 05:25:06

请修改以下代码行 - 希望它能起作用。

private void button1_Click(object sender, System.EventArgs e) {
  for (int i = 0; i < count - 1; ++i) {
    textBox1.Text += history[i].Title + "\n";
  } // for loop
  return;
}

Please modify below lines of code - hope it will work.

private void button1_Click(object sender, System.EventArgs e) {
  for (int i = 0; i < count - 1; ++i) {
    textBox1.Text += history[i].Title + "\n";
  } // for loop
  return;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文