I am working on a small open-source project that uses a third party COM ActiveX Dll that I can't distribute (and therefore can't reference). Is there an easy way to use this DLL in my project without referencing it (System.Reflection Assembly doesn't seem to work with COM/ActiveX) ? Or should I treat it as unmanaged dll with [DllImport] ?
Thanks
Is there an easy way to use this DLL in my project without referencing it (System.Reflection Assembly doesn't seem to work with COM/ActiveX) ?
Nope.
Or should I treat it as unmanaged dll with [DllImport] ?
Typically you use DllImport
for exported functiuons like Windows API, but not for COM servers or ActiveX components.
Platform invoke is a service that enables managed code to call unmanaged functions implemented in dynamic link libraries (DLLs), such as those in the Windows API. It locates and invokes an exported function and marshals its arguments (integers, strings, arrays, structures, and so on) across the interoperation boundary as needed.
Interoperability enables you to preserve and take advantage of existing investments in unmanaged code. Code that runs under the control of the common language runtime (CLR) is called managed code, and code that runs outside the CLR is called unmanaged code. COM, COM+, C++ components, ActiveX components, and Microsoft Windows API are examples of unmanaged code.
The .NET Framework enables interoperability with unmanaged code through platform invoke services, the System.Runtime.InteropServices namespace, C++ interoperability, and COM interoperability (COM interop).
Platform invoke relies on metadata to locate exported functions and marshal their arguments at run time. The following illustration shows this process.
Beginning with the .NET Framework 4, the common language runtime supports embedding type information for COM types directly into managed assemblies, instead of requiring the managed assemblies to obtain type information for COM types from interop assemblies. Because the embedded type information includes only the types and members that are actually used by a managed assembly, two managed assemblies might have very different views of the same COM type. Each managed assembly has a different Type object to represent its view of the COM type. The common language runtime supports type equivalence between these different views for interfaces, structures, enumerations, and delegates.
Type equivalence means that a COM object that is passed from one managed assembly to another can be cast to the appropriate managed type in the receiving assembly.
Read more about that in the Interoperability (C# Programming Guide) section in MSDN.