2016-05-13 83 views
1

我編寫了一個Windows服務,它使用防火牆COM庫Interop.NetFwTypeLib來管理TCP傳輸的規則。兩臺機器上部署不報的問題,但是我最近在另一臺計算機上安裝並收到異常:無法使用STAThread屬性強制類型爲'System .__ ComObject'的COM對象

Unable to cast COM object of type 'System.__ComObject' to interface type 'NetFwTypeLib.INetFwRule3'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{B21563FF-D696-4222-AB46-4E89B73AB34A}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE))

閱讀本帖後:

我設置了STAThreadAttribute這個代碼來測試,如果這個我解決問題的Main方法,但沒有任何解決辦法:

class Program 
{ 
    [STAThread] 
    static void Main(string[] args) 
    { 
     try{ 
      var type = Type.GetTypeFromProgID("HNetCfg.FWRule"); 
      var instance = (INetFwRule3) Activator.CreateInstance(type); 

      Console.WriteLine("OK"); 
     } 
     catch (Exception exception){ 
      Console.WriteLine(exception); 
     } 
    } 
} 

我很驚訝,我去運行此腳本來找到註冊表中的CLSID,並沒有返回

**OS** 
Windows Server 2012 R2 Standard 

**FirewallAPI.dll file on Windows/system32** 
File version: 6.3.9600.17415 
Product version: 6.3.9600.17415 
Size: 736 kb 
Date modified: 4/28/2015 8:51 PM 

信息從計算機到哪裏:兩臺電腦,在那裏工作和不工作:

reg query HKCR\CLSID | find /i "{B21563FF-D696-4222-AB46-4E89B73AB34A}" 

這些是從計算機到哪裏了服務的工作信息上的任何結果服務失敗:

**OS** 
Windows Server 2011 Service Pack 1 

**FirewallAPI.dll file on Windows/system32** 
File version: 6.1.7600.16385 
Product version: 6.3.7600.16385 
Size: 730 kb 
Date modified: 7/13/2009 8:51 PM 

問題:

  • 它可以是FirewallAPI.dll的版本是什麼原因導致這個問題有什麼區別?
  • 如果是這樣就足以更新DLL,雖然它似乎有點危險的註冊表上可能的不一致?
+1

這是一個簡單的解釋,在Windows 8和Server 2012中添加了INetFwRule3接口。沒有真正的想法是什麼「Windows Server 2011」可以,但它肯定是1。您將需要限制自己INetFwRule2或減。 Fwiw,HKCR \ CLSID鍵是類GUID的家,而不是接口GUID。 –

+0

我會嘗試你的建議,你知道INetFwRule2接口的操作系統的限制嗎? – Joseph

+1

只要RTFM,看看接口的MSDN文章的底部。 –

回答

1

感謝@Hans的評論,我可以寫出這個答案。

閱讀的MSDN文檔後:

我發現每個接口的最低支持客戶端和服務器。

var osVersion = Environment.OSVersion.Version; 

if(osVersion.Major < 6) 
    throw new Exception("INetFwRule is not available for current OS version. Minimun OS version required is Windows Vista or Windows Server 2008."); 

if (osVersion.Major == 6) 
{ 
    switch (osVersion.Minor) 
    { 
     case 0: 
      //INetFwRule is available. Windows Server 2008 or Windows Vista 
      break; 
     case 1: 
      //INetFwRule2 is available. Windows 7 or Windows Server 2008 R2 
      break; 
     default: 
      //INetFwRule3 is available. Windows 8.1, Windows Server 2012 R2, Windows 8 or Windows Server 2012. 
      break; 
     } 
    } 
    else 
    { 
     //INetFwRule3 is available. Windows Server 2016 Technical Preview or Windows 10. 
    } 

您可以降級到INetFwRule您的應用程序,如果你不需要的INetFwRule2INetFwRule3額外的功能。

相關問題