简体   繁体   中英

C# managed dll call or unmanaged dll call?

I was asking to do two dll calls from our application. These two dlls are from other group and other company. Have read a little about managed and unmanaged. I would prefer to do managed call. But whether use managed or unmanaged is the decision of the caller only or it also depends on the callee? All dlls can be called with managed code? If callee is also a factor, how can I know this dll can be called with managed code?

If the DLL is written in managed code it'll be a managed call. Otherwise you'll have to do an unmanaged call. Both parts have to be managed for it to be a managed call.

I just means a .Net dll or a non .Net call.

Managed code will remove a level of risk when interoperating with an uncontrolled codebase as you have all the same .Net datatypes and call semantics are the same.

Depending what language your DLL was written in if it isn't .Net you may have issues with the order of arrays and how to push object references onto the stack. Plus you may be forced to pin objects in memory to call a DLL safely if it performs mutations on a given object/primitive value directly.

You definately need more information about the DLLs you are trying to use.

If the DLL is unmanaged, you are going to need additional information about the exported functions so that you can write the appropriate P/Invoke information to make the calls. See this MSDN Article or http://pinvoke.net for examples.

If the DLLs are COM based or .NET, then you can add a reference in your project through Visual Studio and access them directly.

I would suggest writing your DLL in .NET to handle your managed code calls. If you set your .net assembly as COM visible and use the regasm tool you can register and call the same object with unmanaged code.

[ComVisible(true)]

http://edn.embarcadero.com/article/32754

http://msdn.microsoft.com/en-us/library/tzat5yw6(VS.71).aspx

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