Run a function from an interface from C# dll

I want to run a function from a dll written in C#. I wasn't able to run the function because it is embedded in a series of interfaces. Not a single class in the chain.
It is my understanding that you can't run a function within an interface. Does someone a way around this?
Likely not possible. I'm more familiar with raw COM than I am with .NET, but I do believe some of the same principals apply.

For example, interfaces are 'stateless'. Everything an interface member function does will refer back to an instantiated object. The hidden 'this' pointer is passed along with every method call, and 'this' will point back to the memory where the object's instantiation 'lives', i.e., all its private data members will be there as well as VTable pointers to the various interfaces.

On the other hand, with COM anyway, if you have the address of an instantiated object and you have definitions of the interfaces, its possible to calculate with simple arithmetic the addresses of the member functions within the VTables of the interfaces, and to call those functions independantly of the object itself through function pointer notation. I do this frequently myself. Now that's with COM. I don't know how that would apply to .NET classes.

Personally, I'd be curious to know though myself. My problem, like I said, is I don't know .NET very well. My understanding though is that it is somewhat based on COM.
Last edited on
Just to check that I understand you:

You want to call a .Net interface method in a C# DLL from a C++ application?

If so, and you're using Visual C++, then you might be able to use #pragma managed to create a region of managed C++ (that is, C++/CLI) which can be used to call the .Net DLL (or assembly)

Whether it's possible or not will depend on the types of parameter the interface methods take. In some case, you will have to marshal from native to managed types. If you're lucky, all or most of the types will be blittable, which means they can passed directly from unmanaged to managed code.

See the following thread for a basic example ("Hello, .Net world")

Visual C++ 2010 - Unmanged C calling managed c++ dll
http://www.cplusplus.com/forum/windows/85559/

(Note that while this thread refers to a C++/CLI DLL, the prob. is identical. The whole point of .Net is that the assembly created is language independent (from outside). The same approach can be used to call any .Net assembly (the DLL) from native C++, whether the .Net assembly was coded in C#, C++/CLI, VB.Net, etc)

An alternative strategy is to create a COM interop DLL for the managed assembly and then call the C# DLL from the C++ using COM.

Exposing .NET Framework Components to COM
http://msdn.microsoft.com/en-us/library/zsfww439.aspx

The #pragma managed would be the most efficent if possible.

Andy

PS Blittable and Non-Blittable Types
http://msdn.microsoft.com/en-us/library/75dwhxf7.aspx
Last edited on
Topic archived. No new replies allowed.