2008-10-27 85 views
5

我有一個使用.NET組件的.NET服務應用程序的安裝項目,該組件提供了一個COM接口(COM可調用包裝器/ CCW)。 爲了得到在目標計算機上的工作部件,它具有與如何使用regasm從Visual Studio 2008安裝項目註冊.NET CCW

regasm.exe/TLB /代碼庫component.dll登記

的/ TLB切換到生成類型庫是強制性的在這種情況下,否則我不能從該程序集創建對象。

問題是,我如何配置我的Visual Studio 2008安裝項目來註冊這個程序集調用regasm/tlb?

回答

13

你可以失去的人工呼叫是通過使用System.Runtime.InteropServices.RegistrationServices改爲器regasm.exe:

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)] 
public override void Install(IDictionary stateSaver) 
{ 
base.Install(stateSaver); 

RegistrationServices regsrv = new RegistrationServices(); 
if (!regsrv.RegisterAssembly(GetType().Assembly, AssemblyRegistrationFlags.SetCodeBase)) 
{ 
    throw new InstallException("Failed to register for COM Interop."); 
} 

} 

[System.Security.Permissions.SecurityPermission(System.Security.Permissions.SecurityAction.Demand)] 
public override void Uninstall(IDictionary savedState) 
{ 
base.Uninstall(savedState); 

RegistrationServices regsrv = new RegistrationServices(); 
if (!regsrv.UnregisterAssembly(GetType().Assembly)) 
{ 
    throw new InstallException("Failed to unregister for COM Interop."); 
} 
} 

這也會在卸載時取消註冊庫。

+1

此代碼應該添加到什麼位置?我有一個VB.net項目,它需要regasm並且有一些C#依賴項。這兩個方法可以添加到實現哪個超類或接口的類中? – Amala 2012-04-25 16:13:47

1

你的服務應該有一個安裝程序類。 註冊到OnAfterInstall事件並調用RegAsm:路徑應該從Windows目錄計算並綁定到特定的.Net版本。

4
  1. 在您的主項目(包含要註冊的類的項目)中,右鍵單擊項目文件並選擇添加/新項目,然後選擇安裝程序類。取名類似clsRegisterDll.cs
  2. 在出現,點擊「點擊此處切換到代碼視圖」或右鍵單擊Solution Explorer中的clsRegisterDll.cs文件,並選擇查看代碼
  3. 覆蓋安裝,提交設計師和卸載方法添加:

    //獲取regasm的位置 string regasmPath = System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory()+ @「regasm.exe」; //獲取我們DLL的位置 string componentPath = typeof(RegisterAssembly).Assembly.Location; //執行regasm
    System.Diagnostics.Process.Start(regasmPath,「/ codebase/tlb \」「+ componentPath +」\「」);

    在卸載操作中爲/ u交換/ codebase/tlb。

  4. 編譯您的項目
  5. 在您的安裝,請確保您已將DLL到應用程序文件夾,然後用鼠標右鍵單擊安裝項目並選擇查看/自定義操作
  6. 鼠標右鍵單擊安裝,然後單擊添加自定義操作
  7. 雙擊應用程序文件夾,然後在您的DLL
  8. 做同樣的動作提交
  9. 構建和測試您的安裝

與實際的類,您可以嘗試的演練,可以發現:http://leon.mvps.org/DotNet/RegasmInstaller.html

+1

Wolfwyrd的鏈接已更改爲http://leon.mvps.org/DotNet/RegasmInstaller.aspx – dashrb 2013-02-14 22:54:50

1

我最初嘗試從安裝過程中運行regasm(在我看到這篇文章之前)。嘗試運行regasm並處理所有錯誤是有問題的 - 即使沒有嘗試處理Windows 7的提升權限。

使用Runtime.InteropServices.RegistrationServices.RegisterAssembly更清晰並提供了更好的錯誤捕獲。

相關問題