472,973 Members | 2,321 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,973 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 5577
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Mohammad | last post by:
Can someone explain to me what a hook is? Does it has something to do with a debugger? Thanks
0
by: Luke | last post by:
I am trying to capture an event when the screensaver starts. I have code working on Windows XP etc when the Password/Welcome Screen is enabled . Using the Fast User Switching Notifications. ...
6
by: Robert Werner | last post by:
I want to write one general purpose set of code that will handle the population (where required) of all controls on a form. Though I'm new to C# my instinct is to somehow hook into the Load event...
6
by: Charles Law | last post by:
I have a class, which implements an interface. Let's say, that the interface looks something like Public Interface IEventSinks Sub ValueChanged(sender As Object, e As ValueChangedEventArgs) Sub...
3
by: needin4mation | last post by:
Hi, had a question about event hooks. I don't know that much about it so please forgive me if I don't use all the right words. Is it possible to use a hook that listens or watches a certain...
2
by: walter | last post by:
Hi there , for sure httpmodule can be hooked up with HttpApplication event. But I'm wondering if it's poissible to trigger a httpModule in the page event ,like Page.Init(). Since request handler...
0
by: zeng.hui.stephen | last post by:
I download the demo http://msdn.microsoft.com/msdnmag/issues/02/10/cuttingedge/. I inherite the demo, and write my code. I want to use Hook to monitor C++ Edit change. I use a C# form...
4
by: JS | last post by:
I have an application with many user controls and forms which display measurements in whatever the current unit system is. The current unit system is kept in my 'Units' static class as...
3
by: The Saeed | last post by:
Hello, I try to use hook mouse event for a specific application (thread) in C#, I can use global hook, but i want to now which application sends mouse event, did you now what should I do? I set a...
22
by: schneider | last post by:
I need to hook the system mouse down event. I'm trying to replicate how a context menu hides when the mouse clicks outside of the control. Thanks, Schneider
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
3
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.