Microsoft Visual C++ 程式的部署

來源:互聯網
上載者:User
Microsoft Visual C++ 程式的部署

由Microsoft Visual C++編譯的程式動態連結到C運行時(/MD 或 /MDd),它必須捆綁C運行DLL的一份拷貝(通常被叫作MSVCRT.DLL 或 MSVCRxx.DLL,其中xx代表Visual C++的版本)。

1. 用Microsoft Visual C++ 6.0編譯的程式,或者發布在Windows 2000/NT/ME/98 系統
單純通過拷貝MSVCRxx.DLL檔案到應用程式目錄或system32目錄即可

2. 用Visual Studio 2005以上編譯的程式,且發布在Windows XP及以上系統
為了減少DLL引發的配置問題(DLL hell),C和C++運行時由並行 (Side-by-Side) 程式集實現,單純通過拷貝MSVCRxx.DLL並不足以在非開發環境正常運行程式,必須通過一個清單(manifest)來載入CRT DLL。如果載入C執行階段程式庫時沒有這個清單,會引發R6034異常。這就是為何CRT DLLs現在位於WinSXS(Windows Side-by-Side)而不在System32目錄的原因。

EXE和DLL檔案都會有一個manifest檔案,裡面說明了依賴關係,用Visual Studio 2005編譯後,會自動產生與可執行檔同名的manifest檔案,如:
app.exe                // 可執行檔
app.exe.manifest    // dll依賴檔案
一般情況下,會把EXE和DLL的manifest檔案嵌入到EXE和DLL檔案中,外置的manifest就可以刪除了。如:
mt.exe /nologo /manifest ".\app.exe.manifest" /outputresource:".\app.exe";1
在EXE檔案中,最後面的值為1,在DLL檔案中,值為2
Microsoft Visual C++運行庫DLL檔案中則沒有嵌入manifest檔案,因此需要外部的manifest檔案,Visual Studio 2005的manifest名字叫Microsoft.VC80.CRT.manifest,Visual Studio 2008的manifest名字叫Microsoft.VC90.CRT.manifest,所以要將Microsoft.VC80.CRT.manifest,MSVCR80.dll,MSVCP80.dll,MSVCM80.dll 這四個檔案拷貝到應用程式目錄。如
C:\Test\app.exe
C:\Test\MSVCR80.dll
C:\Test\MSVCP80.dll
C:\Test\MSVCM80.dll
C:\Test\Microsoft.VC80.CRT.Manifest
或者採用Microsoft官方建議,如:
在WinXP以上
C:\Test\app.exe
C:\Test\Microsoft.VC80.CRT\Microsoft.VC80.CRT.manifest
C:\Test\Microsoft.VC80.CRT\msvcr80.dll
C:\Test\Microsoft.VC80.CRT\msvcp80.dll
C:\Test\Microsoft.VC80.CRT\msvcm80.dll
在Win2K以下
C:\Test\app.exe
C:\Test\msvcr80.dll
C:\Test\msvcp80.dll
C:\Test\msvcm80.dll
如果用以上方法還是不能執行,說明Microsoft Visual C++在系統中有多個版本的DLL,程式用到的和發布的Microsoft Visual C++ DLL不匹配,如:
app.Manifest 檔案,要求Microsoft.VC90.CRT的DLL,並且版本為9.0.21022.8

<?xml version='1.0' encoding='UTF-8' standalone='yes'?>
<assembly xmlns='urn:schemas-microsoft-com:asm.v1' manifestVersion='1.0'>
  <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
    <security>
      <requestedPrivileges>
        <requestedExecutionLevel level='asInvoker' uiAccess='false' />
      </requestedPrivileges>
    </security>
  </trustInfo>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type='win32' name='Microsoft.VC90.CRT' version='9.0.21022.8' processorArchitecture='x86' publicKeyToken='1fc8b3b9a1e18e3b' />
    </dependentAssembly>
  </dependency>
  <dependency>
    <dependentAssembly>
      <assemblyIdentity type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='x86' publicKeyToken='6595b64144ccf1df' language='*' />
    </dependentAssembly>
  </dependency>
</assembly>

Microsoft.VC90.CRT.manifest檔案,指明是Microsoft.VC90.CRT,但版本為9.0.30729.1

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
    <noInheritable></noInheritable>
    <assemblyIdentity type="win32" name="Microsoft.VC90.CRT" version="9.0.30729.1" processorArchitecture="x86" publicKeyToken="1fc8b3b9a1e18e3b"></assemblyIdentity>
    <file name="msvcr90.dll" hashalg="SHA1" hash="9785b1c493deb5b2134dc4aef3719cee207001bc"><asmv2:hash xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><dsig:Transforms><dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity"></dsig:Transform></dsig:Transforms><dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></dsig:DigestMethod><dsig:DigestValue>VF5ECUAHPV7EnUf+/UIXMPizPvs=</dsig:DigestValue></asmv2:hash></file> <file name="msvcp90.dll" hashalg="SHA1" hash="0f6bbf7fe4fb3fca2cb5b542eca1a1cad051f01c"><asmv2:hash xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><dsig:Transforms><dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity"></dsig:Transform></dsig:Transforms><dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></dsig:DigestMethod><dsig:DigestValue>3Wg+StVMq2uhx7POnAkl2w4dDmY=</dsig:DigestValue></asmv2:hash></file> <file name="msvcm90.dll" hashalg="SHA1" hash="7f3290ab2b7444c2b4a9b1fedfdb16466d7a21bb"><asmv2:hash xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" xmlns:dsig="http://www.w3.org/2000/09/xmldsig#"><dsig:Transforms><dsig:Transform Algorithm="urn:schemas-microsoft-com:HashTransforms.Identity"></dsig:Transform></dsig:Transforms><dsig:DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1"></dsig:DigestMethod><dsig:DigestValue>/YfRn7UQENzdMeoMHxTgdRMiObA=</dsig:DigestValue></asmv2:hash></file>
</assembly>

二者版本不一致,導致程式不能運行,解決辦法是發布程式要求的9.0.21022.8版本的Microsoft.VC90.CRT檔案

3. 還有一種簡單辦法是在需要部署的機器上安裝Visual C++ 2008 Redistributable Package(x86)或者(x64)。
Microsoft Visual C++ 2005 SP1 下載:
http://download.microsoft.com/download/7/9/8/798325b7-8993-4ef9-9148-8db9ff4187fc/vcredist_x86.exe

註:
用Dependency Walker(depends.exe)開啟要發布的EXE,從左上方的列表中找出系統中需要依賴的DLL

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.