Calling a C ++ function from C # and sending data back to C #

I have a C # application that calls functions in a DLL written in C ++. Calls work fine and the code is executed using this method in C #:

[DllImport("myDLL.dll")]
public static extern void Function();

static void Main()
{
    Function();
}

Now it works, I need C ++ executable code to send text to C #, where it can be displayed on the panel.

The text "step one ..." executes the code ... then "step two", etc., showing the running processes. Just basic stuff. I'm not sure how to approach this, since I'm not a C ++ guy. My main skills are .NET.

Greetings

+3
source share
1 answer

Try using callbacks in C ++ and sign them in C #

C ++ Part

typedef void (CALLBACK *pfNotifyMessage)(LPTSTR);

extern "C" AFX_API_EXPORT void SetNotifyMessage(pfNotifyMessageType pfNotify);
extern "C" AFX_API_EXPORT void Function();

In C ++, calling a function calls pfNotifyMessage

C # part

public delegate void NotifyMessage(string message);

[DllImport("myDLL.dll")]
public static extern void SetNotifyMessage(NotifyMessage notify);

[DllImport("myDLL.dll")]
public static extern void Function();

#

+2

Source: https://habr.com/ru/post/1792491/


All Articles