471,339 Members | 1,415 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,339 software developers and data experts.

Hook C# with C++ event

Hello all,

I have created a DLL using C++. I use this dll in a C# project. How can I
make C# listen to an event that is raised inside one of the functions of the
dll?

for exaxmple:
C++:

void MyCPPFunc()
{
HANDLE hEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
SetEvent(hEvent);
}

----------

C#:
//Need to listen to hEvent and do some stuff when it is signaled??

[DllImport("MyDll.dll")]
static extern void MyCPPFunc();

Please help!
Jun 27 '08 #1
3 5510
On Tue, 03 Jun 2008 20:00:03 -0700, Ali <Al*@discussions.microsoft.com>
wrote:
I have created a DLL using C++. I use this dll in a C# project. How can I
make C# listen to an event that is raised inside one of the functions of
the
dll?
Well, the most obvious approach would be to use p/invoke to call
WaitForSingleObject() on the handle. Or, just add a method in your DLL
that does the waiting.

Another option is to use the handle to create a managed SafeWaitHandle
instance, and then assign that to the SafeWaitHandle property of an
existing WaitHandle object.

Pete
Jun 27 '08 #2
Well, the most obvious approach would be to use p/invoke to call
WaitForSingleObject() on the handle.
Thanks for your reply, Pete!

OK, but how can I pass the handle to C#? I means how can I p/invoke it?

Don't I need to do this first before I can call WaitForSingleObject() in C#?

Thank you!

Jun 27 '08 #3
On Wed, 04 Jun 2008 05:47:01 -0700, Ali <Al*@discussions.microsoft.com>
wrote:
>
>Well, the most obvious approach would be to use p/invoke to call
WaitForSingleObject() on the handle.

Thanks for your reply, Pete!

OK, but how can I pass the handle to C#? I means how can I p/invoke it?
Those are two different questions. As far as passing the handle, you can
just return it as an IntPtr object. That's the general "unfiltered,
unmanaged handle" data type in managed code. You'd call the function in
your C++ DLL the same way you'd call any function in that DLL.

As far as "how can I p/invoke it", the same way you'd p/invoke anything
else. If you've got the IntPtr already, it should be as simple as
declaring the import for the WaitForSingleObject() function and then
calling that with the IntPtr you have.

Pete
Jun 27 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Mohammad | last post: by
reply views Thread by Luke | last post: by
6 posts views Thread by Charles Law | last post: by
3 posts views Thread by needin4mation | last post: by
reply views Thread by zeng.hui.stephen | last post: by
22 posts views Thread by schneider | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.