in Education by
I'm trying to PInvoke into this C++ library function: int Initialize(Callback* callback); struct Callback { //....... void virtual StateChanged(int state) = 0; }; I have tried this naive C# code, but it doesn't work. [DllImport("CPlusPlusLibrary.dll", SetLastError = true, EntryPoint = "?Initialize@CPlusPlusLibrary@@YAHPAUCallback@1@@Z")] public static extern int Initialize(Callback callback); [StructLayout(LayoutKind.Sequential)] public class Callback { //.... public delegate void IntDelegate(int state); public IntDelegate StateChanged(int state); } var callback = new Callback(); var result = Initialize(callback); JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
It is impossible to do it that way as far as I know. Methods are not "in there" as fields would be, beside this, creating a struct with virtual method will create a vtable pointer in your objects, that you are not taking into account in c# mirrored class. What you can do is to PInvoke to method that takes a functionPointer (in C++) and pass a delegate (C#) there. You can then use this function pointer to call it from native code and your delegate will launch. You could then change your stateChange method definition to take a Callback* as a first parameter, so when you call it from native code you can pass an object pointer which is responsible of that change and marshal it back to Callback in c#. //Edit without having source of native dll, building a bridge between c# and c++ is what comes to my mind. This can be done with c++/cli or c++, sticking to native my idea would be something like this: //c++ <--> new c++ dll <--> c# struct CallbackBridge : public Callback { void (*_stateChanged)(int); virtual void stateChanged(int state) { if (_stateChanged) _stateChanged(this, state); } }; void* CreateCallback() { return new CallbackBridge(); } void DeleteCallback(void* callback); { delete callback; } void setStateChanged(void* callback, void (*ptr)(void*, int)) { CallbackBridge* bridge = (CallbackBridge*)callback; bridge->stateChanged = ptr; } ///... other helper methods The idea here is to treat your object as a black box (hence void* everywhere - it can be any pointer, but from c# you will just see this a a SafeHandle / IntPtr and write helper methods that you can PInvoke to to create / delete and modify objects. You can mock those virtual calls by giving your object a delegate through such method. From c# usage could look like this: (IntPtr for simplicity, SafeHandle could be used): IntPtr callback = CreateCallback(); SetStateChanged(callback, myCallback); //somewhere later: DeleteCallback(callback); void MyCallback(IntrPtr callback, int state) { int someData = SomeOtherHelperMethod(callback); ConsoleWrite("SomeData of callback is {0} and it has changed it's state to {1}", someData, state); } I know, it's a bit clumsy for bigger objects, but without c++/cli wrapper I have never found any better way to be able to handle all those tricky cases like virtual calls etc.

Related questions

0 votes
    I'm trying to PInvoke into this C++ library function: int Initialize(Callback* callback); struct Callback { ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 19, 2022 in Education by JackTerrance
0 votes
    I'm translating a library written in C++ to C#, and the keyword 'union' exists once. In a struct ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I'm translating a library written in C++ to C#, and the keyword 'union' exists once. In a struct ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    In my dll there is a method that I want to export. //Works: extern "C" __declspec(dllexport) // ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    In my dll there is a method that I want to export. //Works: extern "C" __declspec(dllexport) // ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    In my dll there is a method that I want to export. //Works: extern "C" __declspec(dllexport) // ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    In my dll there is a method that I want to export. //Works: extern "C" __declspec(dllexport) // ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    How can one get the coordinates of a mouse click inside a panel? For instance I want to be able ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 13, 2022 in Education by JackTerrance
0 votes
    I have an issue with passing a struct definition to a function. Not an instance of a struct, but the definition. We ... (object sender, EventArgs e) { GetHeaders( MineStruct ); //...
asked May 26, 2022 in Education by JackTerrance
0 votes
    When should I use a struct rather than a class in C#?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    is it possible to call c# methods written in managed code (maybe in a class or a library) from a ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 25, 2022 in Education by JackTerrance
0 votes
    What is the difference between dispose() and finalize() methods in C#?...
asked Jul 27, 2021 in Technology by JackTerrance
0 votes
    What is the difference between the dispose and finalize methods in C#?...
asked Mar 31, 2021 in Technology by JackTerrance
0 votes
    What are extension methods in C#?...
asked Mar 31, 2021 in Education by JackTerrance
0 votes
    What is Default interface methods in C#.Net?...
asked Dec 30, 2020 in Technology by JackTerrance
...