Calling an x64 assembly via COM from a 32 bit App

2019-02-22 15:38发布

Short question : is it possible (on an x64 OS of course) ? If not, why exactly ?

I have developed a c# plugin dll for excel 32.

When compiled in x86 it works fine.

When compiled in x64 the COM call fails.

Do I need a 64 bit version of excel ?

I thought COM was agnostic of compiling architecture and made communication possible between dlls developed in different technologies and having different architectures, but I guess the latter is wrong.

I guess an x64 bit dll can obviously not be called via COM (or else) from a 32-bit app.

1条回答
Summer. ? 凉城
2楼-- · 2019-02-22 16:14

COM supports two kind of servers, in-process and out-of-process. Office extensions are in-process components, a DLL that gets loaded into the process. A hard rule for 32-bit processes is that they cannot load 64-bit DLLs. And the other way around. This is enforced by the registry itself, a 32-bit process cannot directly access the registration information for 64-bit COM servers. They are redirected to the HKLM/Software/Wow6432Node keys. Or in other words, they cannot even see components of the wrong bitness.

Out-of-process components don't have that restriction, they run in their own process. COM marshals the calls between the two processes using RPC and papers over the bitness difference. This is also a way to get an in-process 64-bit server to work with a 32-bit host, you can run the component in a surrogate process. This is tricky to get going and almost never worth the hassle, out of process calls are much more expensive than in-process calls due to the required marshaling and context switching. Not just a little more expensive either, it is about 10,000 times slower, mostly because an in-process function call is so very fast. It is only ever used to keep a legacy 32-bit server working with a 64-bit program. Look at COM+ hosting if you want to try this, I don't know much about it.

查看更多
登录 后发表回答