简体   繁体   中英

AnyCpu reference x64 valid?

Is it a valid scenario to reference an x64-dll from an AnyCpu dll when the system it will run on is for sure a 64bit System?

I'm asking, because I had issues here, getting an exception as follows:

"Could not load file or assembly 'XY' or one of its dependencies.An attempt was made to load a program with an incorrect format"

I had this problem several times with different solutions in the last couple of years. Sometimes it seems to work and sometimes not.

It is completely valid for an AnyCPU assembly to target an x64 DLL. But, it is up to you to ensure that your AnyCPU assembly is actually executing as an x64 process.

Note that later versions of Visual Studio have added the "Prefer 32-bit" option to assemblies. So you need to make sure this is turned off, and that you aren't using any settings on the machine that would override that setting.

Also note that if your AnyCPU assembly references other assemblies that may be available only as x86 assemblies on the target machine, it will be run as an x86 process.

Frankly, if you know that you want the process to only ever run as x64, IMHO it makes the most sense to compile it as x64 . At least that way, if you do wind up running it in an incompatible environment, you'll get a better error message (ie the event log will contain the information about the dependency that was incompatible with your process, instead of some other dependency that is actually as expected).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM