The frequency with which I am coming across the situation where I have to call native 32-bit code from a managed 64-bit process is increasing as 64-bit machines and applications become prevalent. I don't want to mark my applciation as 32-bit and I cannot obtain 64-bit versions of of the code that is being calling.
The solution that I currently use is to create C++ COM shims that are loaded out of process to make the 32-bit calls from the 64-bit process.
This COM shim solution works well and the cross process calls are handled behind the scenes by COM, which minimises the overhead of this approach.
I would however like to keep all the new development that we undertake using C# and wondered if there are any frameworks that minimise the overhead of doing this. I have looked at IPCChannel but I feel that this approach is not as neat as the COM shim solution.
thanks, Ed
CLR helps in running the code and also provides a variety of services to make the development process easy. All the codes that are controlled by the CLR are called the Managed Code, while the parts of the program which are written using the unsafe keyword are called Unmanaged Code.
This code is directly executed by CLR with help of managed code execution. Any language that is written in . NET Framework is managed code. Managed code uses CLR which in turns looks after your applications by managing memory, handling security, allowing cross - language debugging, and so on.
Managed Code is converted to IL, Intermiddiate Language also termed as CIL of MSIL. Unmanaged Code is converted to native language code. Programmer has no low level access using Managed Code. Programmer can write low level access code using unmanaged code.
So, by that definition all code compiled by traditional C/C++ compilers is 'unmanaged code'. Also, since it compiles to machine code and not an intermediate language it is non-portable.
I had the same problem and my solution was to use remoting. Basically the project consisted of:
CalculatorRemote.dll
library with
CalculatorNative
internal static class with x32 P/Invoke methodsRemoteCalculator
class derived from MarshalByRefObject
which used native methods from CalculatorNative
;Calculator.dll
), referencing CalculatorRemote.dll
, with Calculator
class which was privately using singleton of the RemoteCalculator
class to invoke x32 functions where needed;RemoteCalculator
from CalculatorRemote.dll
to consume by Calculator.dll
via IpcChannel
.So if the main application started in x64 mode it was spawning a RemoteCalculator
host application and used remoted RemoteCalculator
instance. (When in x32 it just used a local instance of RemoteCalculator
.) The tricky part was telling calculator-host application to shut down.
I think this it better than using COM because:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With