2009-05-29 19 views
1

セキュリティインテルでWindowsオペレーティングシステムにセキュリティ修正プログラムを自動的に適用するコードを作成しています。 (私はVisual Studio .NETを使用していますので、.NET Framework 1.1、およびC#で開発しています)
最初に、WUApiを使用して既にシステムに適用されている修正プログラムをリストしたいと思います。私は "tlbimped" wuapi.dllへの参照を追加しました。また、元のdllをregsvr32に登録しました.Windows Updateサービスがサービスマネージャで起動しているようですが、コードは動作しません。適用された修正プログラムの数として、修正プログラムを適用した直後でも、 "QueryHistory"関数に到達するとCOMExceptionがスローされます。次のように
コードは次のとおりです。Windows Update API C#コードが更新履歴を取得できません

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; 
     } 
    } 
} 

答えて

1

コードの行の下に変更してください - それは働くことを願っています。

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; 
} 
関連する問題