2010-07-23 14 views
3

我有一個使用COM DLL的.NET應用程序,其中既有32位也有64位版。我編寫了兩個應用程序清單,它們可以在32位或64位上進行並行COM互操作。這裏的32位版本:根據應用程序運行的位數加載32位或64位並行COM DLL

<?xml version="1.0" encoding="utf-8"?> 
<assembly manifestVersion="1.0" xmlns="urn:schemas-microsoft-com:asm.v1"> 
    <assemblyIdentity name="MyApp" version="1.0.0.0" type="win32" /> 
    <dependency> 
    <dependentAssembly> 
     <assemblyIdentity 
      type="win32" 
      name="MyCOMDll_32.dll" 
      version="1.2.3.4" 
      processorArchitecture="x86" 
      publicKeyToken="0000000000000000" 
      language="*" /> 
    </dependentAssembly> 
    </dependency> 
</assembly> 

然而,維護兩個艙單導致便攜性的損失:你需要決定何時安裝該應用程序使用的版本。而64位應用程序不能再以32位模式運行。

是否有可能讓.NET應用程序加載正確的32位或64位DLL,具體取決於它運行的位數? 我嘗試過使用兩個依賴項元素,一個使用<assemblyIdentity processorArchitecture="x86" .../>,另一個使用<assemblyIdentity processorArchitecture="amd64" .../>,但這會導致應用程序配置錯誤。

我會很感激的答案。 問候, 莫里茨

回答

1

我還沒有找到一種方法與應用manifest.Therefore要做到這一點,我放棄了申請贊成使用激活上下文API的編程解決方案的清單。 此解決方案已從http://support.microsoft.com/kb/830033/en-us(其中字段cookie必須是IntPtr而非uint)進行了修改。 我已經將EnsureActivationContextCreated()方法的內部替換爲


if (!contextCreationSucceeded) 
{ 
    string manifestLoc = Environment.Is64BitProcess 
    ? "MyCOMDll_64.dll.manifest" 
    : "MyCOMDll_32.dll.manifest"; 

    myComActivationContext = new NativeMethods.ACTCTX(); 
    myComActivationContext.cbSize = Marshal.SizeOf(typeof(NativeMethods.ACTCTX)); 
    myComActivationContext.lpSource = manifestLoc; 

    // Note this will fail gracefully if file specified 
    // by manifestLoc doesn't exist. 
    hActCtx = NativeMethods.CreateActCtx(ref myComActivationContext); 
    contextCreationSucceeded = hActCtx != Failed; 
} 
相關問題