开发者

AnyCPU C# DLL in 64bit process loads 32bit DLL

开发者 https://www.devze.com 2023-02-28 22:53 出处:网络
I have a 64bit C++ process that loads an AnyCPU C# DLL. 开发者_高级运维Can this AnyCPU C# DLL can load 32bit C++ DLLs, or can it load only 64bit DLLs?

I have a 64bit C++ process that loads an AnyCPU C# DLL. 开发者_高级运维Can this AnyCPU C# DLL can load 32bit C++ DLLs, or can it load only 64bit DLLs?

Thanks! :-)


An AnyCPU DLL runs in the context of the calling process, which is x64 according to your question. If this AnyCPU DLL loads another DLL, this also runs in the context of the x64 calling process that loaded the AnyCPU DLL. This is the problem with running code across different platforms. If you want to run a 32 bit DLL by the x64process that calls the AnyCPU DLL, then you need to have create something called as the SURROGATE process - this is basically a x86(32 bit) process that can run and load the 32 bit DLL you want to access from the calling x64 process. Once you have this x86 process running, this x64 process can communicate with the x86 process (that has loaded x86 DLL) using Inter Process Communication, and the indirect calling of x86 DLL functions from x64 process can be achieved.


You can only load dlls with the same bit-ness as the process. The Any CPU dll itself can be loaded in both 64 and 32 bit processes, but that doesn't mean in can load native 32bit dlls into a 64 bit process.

The only workaround I know is having a 32 bit helper process that loads your 32 bit dll and then communicate with that process.


No, you can't load 32-bit DLLs into a 64-bit process.

AnyCPU means that the C# DLL can be loaded into either 32-bit or 64-bit processes, not that it can load DLLs of either bitness.


A 32 bit DLL cannot be loaded in a 64 bit process.

I see a problem in your question: it is not possible is your AnyCPU assembly loads a 32bit C++ dll, since it makes the assembly specific to this architecture.

Making it AnyCPU means that it will dynamically load the DLL specific to the architecture on which it's running. This way it runs on Any CPU.

So in short, the assembly is wrongly marked as being AnyCPU.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号