Re: Passing struct of virtual functions



On Jul 16, 8:12 pm, ROLST5 <ROL...@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
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?  

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.
.



Relevant Pages

  • Passing struct of virtual functions
    ... I have a native win32 C++ DLL that has exported functions (I can't modify the ... I'm trying to call this exported win32 DLL function from C#. ... The C struct look something like this ... struct ExportFuncs{ ...
    (microsoft.public.dotnet.languages.csharp)
  • [PATCH 3/4 v2] PCI: support SR-IOV capability
    ... +struct virtfn_slot { ... * The major differences between Virtual Function and PCI device are: ... * transaction to Virtual Functions when they resides on a different ... * bus from Physical Function, so there is no explicit bridge device ...
    (Linux-Kernel)
  • Re: why still use C?
    ... over C is really a matter of notational convenience. ... piece of data must come from a struct or class. ... Virtual functions are used but not common. ... but I am especially careful about name clashes. ...
    (comp.lang.c)
  • Re: Data Structure
    ... >> It's an attempt to fake objects with virtual functions in C. ... You have a pointer to the ... >> vtable as part of each object struct. ... > It's not grossly inefficient. ...
    (comp.lang.c)
  • Junk data after Marshal.PtrToStructure
    ... I have a WIN32 DLL which pass some information to a .NET based ... application via the windows messages. ... in the managed code, ... typedef struct _TOTAL_STATUS ...
    (microsoft.public.dotnet.languages.csharp)