更新:2007 年 11 月
假設我們有 MyApplication.exe 和 MyLibrary.DLL,且這兩者都是以 MFC 建置的。MyApplication.exe 相依於 MyLibray.DLL,而這兩者都會在共用的 DLL 中使用 MFC,並且可以是原生或混合的 (/clr) 二進位碼檔案。在最簡單的情況下,精靈會以預設的設定產生 MyApplication.exe 和 MyLibrary.DLL。本章節中的範例描述如何將這個應用程式部署至另一台未安裝 Visual Studio 的電腦。本章節主要涵蓋部署應用程式的發行版本 (Release Version),但也會說明部署應用程式偵錯版本所需的變更。
注意事項: |
|---|
本使用者授權合約不允許轉散發偵錯 Visual C++ 程式。但是,您可以為進行偵錯作業而暫時先部署。請參閱 Visual Studio 2008 軟體授權合約 EULA。 |
初始安裝
本案例中會用到三台電腦。
一台要建置應用程式的開發電腦,它安裝了 Visual Studio 2005 (STD、PRO 或 TS)。
兩台未安裝 Visual Studio 2005 的部署目標電腦。部署目標 1 是一部電腦,其中執行的作業系統可支援應用程式與其相依性之間的資訊清單架構繫結 (Windows XP Home Edition、Windows XP Professional、Windows Server 2003、Windows Vista)。部署目標 2 執行的作業系統則不含前述支援 (Windows 2000)。
案例的目標是,在部署電腦上建置應用程式,並將應用程式部署到兩台目標電腦,然後加以執行。
準備作業
當您已建置即將要在另一部電腦上執行的 Visual C++ 二進位檔時,必須判斷此二進位檔會依賴哪些 DLL。對此,您可以使用 Dependency Walker。在此情況下,您必須考量 Visual C++ DLL,尤其是 CRT 和 MFC。如果您在 Visual Studio 中開啟 MyApplication.exe 的偵錯版本,並在它的資源中瀏覽,則可以看到 RT_MANIFEST 資源。這是內嵌在二進位碼檔案中的資訊清單。如果您將它匯出並開啟為 XML 檔,就會看到:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.VC90.DebugCRT" version="9.0.xxxxx.yy" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.VC90.DebugMFC" version="9.0.xxxxx.yy" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="x86" publicKeyToken="6595b64144ccf1df" language="*"></assemblyIdentity>
</dependentAssembly>
</dependency>
</assembly>
這表示這個應用程式相依於下列組件:
組件 Microsoft.VC90.DebugCRT,版本 9.0.xxxxx.yy for x86
組件 Microsoft.VC90.DebugMFC,版本 9.0.xxxxx.yy for x86
組件 Microsoft.Windows.Common-Controls,版本 6.0.0.0 for x86
在發行的二進位碼檔案中,您會看到:
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.xxxxx.yy" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.VC90.MFC" version="9.0.xxxxx.yy" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
</dependentAssembly>
</dependency>
<dependency>
<dependentAssembly>
<assemblyIdentity type="win32" name="Microsoft.Windows.Common-Controls" version="6.0.0.0" processorArchitecture="x86" publicKeyToken="6595b64144ccf1df" language="*"></assemblyIdentity>
</dependentAssembly>
</dependency>
</assembly>
這表示這個應用程式相依於下列組件:
組件 Microsoft.VC90.CRT,版本 9.0.xxxxx.yy for x86
組件 Microsoft.VC90.MFC,版本 9.0.xxxxx.yy for x86
組件 Microsoft.Windows.Common-Controls,版本 6.0.0.0 for x86
您會在 MyLibrary.dll 中看到偵錯和發行的類似資訊清單。請注意 EXE 的資訊清單 ID 是 1,DLL 的是 2。此外,如果資訊清單未內嵌在二進位碼檔案中,便會儲存為 <binaryname>.<extension>.manifest 並且具有相同的內容。
注意事項: |
|---|
Visual Studio 2005 一定要有資訊清單,並繫結至 Visual C++ 程式庫 (依照使用 %PATH% 的舊方法),才能支援 C++ 應用程式的建置。此外,Visual C++ DLL 可以偵測這個情況,以避免 DLL 載入及報告不支援的情況和必要的變更。請勿使用 /manifest:no 或刪除資訊清單。 |
部署方法
在此範例中,您會將 MyApplication.exe 安裝到資料夾 %TARGET% (客戶可以在安裝期間指定這個資料夾)。MyLibrary.dll 將會安裝在 %TARGET%\MyLibrary 中,並將 \MyLibrary 加入至路徑。
我們將檢視部署 VC++ 應用程式的兩個方法:
使用安裝和部署專案建置安裝套件。
XCopy 部署。
我們將針對這兩個方法探討兩個案例:
將 Visual C++ 程式庫部署為共用組件。
將 Visual C++ 程式庫部署為私用組件。
在案例 1 中,WinSxS 資料夾中只有一個 Visual C++ DLL 複本。在案例 2 中,會有兩個 Visual C++ DLL 複本,這兩個複本安裝在應用程式 EXE 和 DLL 的本機資料夾中。
注意事項: |
|---|
Windows 2000 中不支援案例 2,因為部署在應用程式的本機資料夾中會造成服務方面的問題。 |
注意事項: