Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Triggering event in C# from C++ DLL

I have an umanaged C++ DLL which is communicating with a Cisco Server (UCCX).

It sends and receives messages to and from this server via TCP/IP. Now there are some types of messages it receives which contains some parameters which it needs to send to a C# GUI which will display these parameters on screen.

Please tell me an efficient method to trigger an event in C# from this DLL.

like image 614
Achal Gupta Avatar asked Sep 25 '12 05:09

Achal Gupta


People also ask

What is a triggering event?

What Is a Triggering Event? A triggering event is a tangible or intangible barrier or occurrence which, once breached or met, causes another event to occur. Triggering events include job loss, retirement, or death, and are typical for many types of contracts.

What is trigger in C language?

The TRIGGER command raises the specified AT -condition in Debug Tool, or it raises the specified programming language condition in your program.

What is an event trigger in programming?

An event trigger is an association between a predefined event and the script that is to be executed when that event occurs. These scripts fall into two categories based on when they occur and if they modify entries in the database.

Which are types of events in trigger?

There are two types of trigger events: database trigger events and page trigger events.


2 Answers

http://blogs.msdn.com/b/davidnotario/archive/2006/01/13/512436.aspx seems to answer your question. You use a delegate on the C# side and a standard callback on the C++ side.

C++ side:

typedef void (__stdcall *PFN_MYCALLBACK)();
int __stdcall MyUnmanagedApi(PFN_ MYCALLBACK callback);

C# side

public delegate void MyCallback();
[DllImport("MYDLL.DLL")] public static extern void MyUnmanagedApi(MyCallback callback);

public static void Main()
{
  MyUnmanagedApi(
    delegate()
    {
      Console.WriteLine("Called back by unmanaged side");
    }
   );
}
like image 153
CrazyCasta Avatar answered Nov 02 '22 04:11

CrazyCasta


Note the C++ side should be

typedef void (__stdcall *PFN_MYCALLBACK)();
extern "C" __declspec(dllexport) void __stdcall MyUnmanagedApi(PFN_ MYCALLBACK callback);

An alternate option is to drop the __stdcall on the C++ side

typedef void (*PFN_MYCALLBACK)();
extern "C" __declspec(dllexport) void MyUnmanagedApi(PFN_ MYCALLBACK callback);

And on the C# side:

public delegate void MyCallback();
[DllImport("MYDLL.DLL", CallingConvention = CallingConvention.Cdecl))] 
public static extern void MyUnmanagedApi(MyCallback callback);

as above ...
like image 27
Ashley Duncan Avatar answered Nov 02 '22 06:11

Ashley Duncan