Passing struct of virtual functions

R

ROLST5

I have a native win32 C++ DLL that has exported functions (I can't modify the
win32 DLL). One of these exported functions needs a pointer to a structure.
The structure contains virtual functions. I want to call the win32 function
from C# and pass it a ref to a struct of virtual functions - just as I would
from win32 C.

For example, I'm trying to call this exported win32 DLL function from C#.
I can call it from C#, but I can't get the C# mangling correct and it throws
exceptions at in the DLL at the location below.
bool initialize(ExportFuncs *f)
{
ExportFuncs *temp = f;
f->busWrite(... -> exception
}

The C struct look something like this
struct ExportFuncs{
public:
virtual int busWrite(long regNo, long data);
....
}

Possible?
 
P

Pavel Minaev

I have a native win32 C++ DLL that has exported functions (I can't modifythe
win32 DLL).  One of these exported functions needs a pointer to a structure.  
The structure contains virtual functions.  I want to call the win32 function
from C# and pass it a ref to a struct of virtual functions - just as I would
from win32 C.

For example, I'm trying to call this exported win32 DLL function from C#.
I can call it from C#, but I can't get the C# mangling correct and it throws
exceptions at in the DLL at the location below.
bool initialize(ExportFuncs *f)
{
ExportFuncs *temp = f;
f->busWrite(...  -> exception

}

The C struct look something like this
struct ExportFuncs{
public:
virtual int busWrite(long regNo, long data);
...

}

Possible?  

Yes, it is possible, but it is highly dependent on the implementation
of virtual functions in your C++ compiler. Typically, it would be
something like this:

struct ExportFuncs {
struct VTable {
// _virtual_ functions in order declared in C++ header
Func<long, long, int> busWrite;
...
}
VTable* vtable;

// data members in order declared in C++ header
...
}

The above would probably work for Visual C++ unless struct is itself
inherited from another class.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top