试图不需要为x86和x64程序两个单独的解决方案 [英] Trying to not need two separate solutions for x86 and x64 program

查看:174
本文介绍了试图不需要为x86和x64程序两个单独的解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个计划,需要既有x86和x64的环境中工作。它是使用Oracle的ODBC驱动程序。我必须Oracle.DataAccess.DLL的参考。此DLL取决于系统是否是64或86,虽然是不同的。

I have a program which needs to function in both an x86 and an x64 environment. It is using Oracle's ODBC drivers. I have a reference to Oracle.DataAccess.DLL. This DLL is different depending on whether the system is x64 or x86, though.

目前,我有两个分开的溶液和我维持双方的代码。这是残酷的。我想知道的妥善解决是什么呢?

Currently, I have two separate solutions and I am maintaining the code on both. This is atrocious. I was wondering what the proper solution is?

我有我的平台设置为任何CPU。它是我的理解是应该VS编译DLL到中介语言,比如,如果我使用x86或x64版本,它不应该的问题。不过,如果我尝试使用64 DLL我收到错误无法加载文件或程序集Oracle.DataAccess,版本= 2.102.3.2,文化=中性公钥= 89b483f429c47342'或其依赖项之一。试图加载程序格式不正确。

I have my platform set to "Any CPU." and it is my understanding that VS should compile the DLL to an intermediary language such that it should not matter if I use the x86 or x64 version. Yet, if I attempt to use the x64 DLL I receive the error "Could not load file or assembly 'Oracle.DataAccess, Version=2.102.3.2, Culture=neutral, PublicKeyToken=89b483f429c47342' or one of its dependencies. An attempt was made to load a program with an incorrect format."

我正在运行32位计算机上,因此该错误信息是有道理的,但它让我知道我应该如何当它需要在x64上工作,有效地开发这个程序。

I am running on a 32 bit machine, so the error message makes sense, but it leaves me wondering how I am supposed to efficiently develop this program when it needs to work on x64.

感谢。

推荐答案

这是你的问题的工作解决方案:

This is a working solution for your problem:

添加2 DLL的(x86和x64)中的子文件夹您的解决方案。让他们复制,如果新的

Add the 2 DLL's (x86 and x64) to your solution in a subfolder. Make them "Copy if newer"

参考您使用的开发调试从您添加的2 DLL的正确的DLL。使其复制本地=假的。

Reference the correct DLL you use for development for debugging from the 2 DLL's you added. Make it Copy Local=false.

这里做的事情是,当你的应用程序启动DLL不会自动加载。它不会被载入直到你使用从该程序集的类型。一旦这种情况发生的事件将在.net中,询问哪里能找到你的程序集被触发。

What this does is that when you app starts the DLL is not autoloaded. It will not be loaded until you use a Type from that assembly. Once that happens an event will be triggered in .Net that asks where it can find your assembly.

所以有时第一次使用该程序集之前,请务必附上自己。该事件

So sometime before the first use of that assembly make sure you attach yourself to that event.

AppDomain.CurrentDomain.AssemblyResolve += CurrentDomain_AssemblyResolve;

在处理程序的内容,请确保您加载DLL(x86或x64),当它要求吧。

In the content of the handler make sure you load the DLL (x86 or x64) when it asks for it.

    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
        if (args.Name.Equals("MyFullAssemblyName")) {
            var path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
            if (IntPtr.Size > 4) {
                var dll = System.IO.Path.Combine(path, @"MySubDir\MyDLL_x64.dll");
                return System.Reflection.Assembly.LoadFile(dll);
            }
            else {
                var dll = System.IO.Path.Combine(path, @"MySubDir\MyDLL.dll");
                return System.Reflection.Assembly.LoadFile(dll);
            }
        }
        return null;
    }



瞧。您现在可以运行你的应用程序为32位和64位。

Voila. You can now run your app as both 32 bit and 64 bit.

另外在子文件夹添加DLL文件,你可以让他们作为嵌入资源,然后加载它们像这样的:

Alternatively to adding the DLLs in a subfolder, you can make them as Embedded Resources, and then load them like this:

    static System.Reflection.Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args) {
        if (args.Name.Equals("MyFullAssemblyName")) {
            var ass = Assembly.GetExecutingAssembly();

            if (IntPtr.Size > 4) {
                var strm = ass.GetManifestResourceStream("the.resource.name.for.MyDLL_x64.dll");
                var data = new byte[strm.Length];
                strm.Read(data, 0, data.Length);
                return Assembly.Load(data);
            }
            else {
                var strm = ass.GetManifestResourceStream("the.resource.name.for.MyDLL.dll");
                var data = new byte[strm.Length];
                strm.Read(data, 0, data.Length);
                return Assembly.Load(data);
            }
        }
        return null;
    }

这并不适用于所有装配工作。一些混合组件往往会失败,除非它们是从磁盘加载(可通过其写入只是在装货前的磁盘来解决)。

This does not work for all assemblies. Some "hybrid" assemblies tends to fail unless they are loaded from disk (can be solved by writing them to disk just before loading).

这篇关于试图不需要为x86和x64程序两个单独的解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆