How to handle events raised from a kernel-level driver written in C++ from C# code
I've two parts in my project. One is a kernel mode driver written in C++ and the other is Windows application written in C#. I've raised an event from the C++ kernel mode driver and I need to handle that event from my C# application. If I was coding in C++ I could use the following function

DWORD WINAPI WaitForSingleObject(
  __in  HANDLE hHandle,
  __in  DWORD dwMilliseconds
);

Is there an alternative to this function in C# ?

See All Responses Below...
Author
Message Rating
Posted on: 16/05/2012
Hello,

I think there is no equivalent library in C#.NET and you need to directly call this win32 API in C#.NET using pInvoke calls..

See some interesting discussions below..

http://www.codeproject.com/Messages/1889585/WaitForSingleObject-equivalent-in- Csharp.aspx

http://www.pinvoke.net/default.aspx/kernel32.waitforsingleobject

Also see the thread below..

http://msdn.microsoft.com/en- us/library/ms173179%28v=vs.80%29.aspx

In principle it should do the same function as WaitforSingleObject, but dont know whether we can trap the same event message coming form the Driver as the method which this C#.NET wrapper library is expecting might be a different win32 API..

Tom
 

Posted on: 16/05/2012
Share this page across other social networking sites



Posted on: 20/05/2012
Hi,

I think your solution is to use PInvoke as says Tom, but I think you need to write a DLL to wrap your kernel-mode driver event and expose it. Then you can manage this DLL event through you C# app with PInvoke as managed applications can not directly connect to kernel drivers.
Posted on: 20/05/2012
You also can handle the gap between managed and unmanaged code manually, using your own marshalers and everything you need to handle this, but I also suggested use PInvoke if you can.

Take a look at this great article: Accessing Device Drivers from C#

- Borja
Share this page across other social networking sites



Posted on: 20/05/2012
Adding to Borjas point , you can use C++/CLI to get the best out of Unmanaged as well as Managed code. For example you can use WaitForSingleObject Unmanaged API as well as .NET managed library if you use Managed C++ without much difficulty with marshaller etc ..

Tom

Please Login to Post Your Comments
Name (Max. 100 characters)
Please post your comments here
Select Language
Comments
Attach File(Max. Size: 2 MB)
A few simple rules when posting your Comments,
  1. Please post only answers relevant to the topic of discussion.
  2. Please dont misuse this site or do not be abusive, offensive, inappropriate,harass anyone on the boards or post ads or spam. Doing so will delete your inappropriate messages and will block or delete your account on this site.