Listening for event call from device driver using WaitForMultipleObjects

B

bb

I have a windows network device driver written in c++ and a user
interface im porting to c#, my problem is i dont seem to be getting
notified of the event calls from the driver to the c# app

im using the following code in c# in the UI to create an event

public static IntPtr OpenGrantedPacketEvent()
{
IntPtr objDriver = Driver.OpenDriver();
IntPtr objEvent = Win32.CreateEvent(IntPtr.Zero, false, false,
Driver.PW_EVENT_GRANTEDPACKET);
uint nBytesReturned = 0;

if(Win32.DeviceIoControl(objDriver, Driver.IOCTL_PW_SET_GRANTEVENT,
IntPtr.Zero, 0, IntPtr.Zero, 0, out nBytesReturned, IntPtr.Zero) == 0)
{
Driver.CloseEvent(objEvent);
Driver.CloseDriver(objDriver);

throw new Exception("Failed to create granted packet event.");
}

Driver.CloseDriver(objDriver);

return objEvent;
}




in the driver in c++ im doing this to create a notification event

case IOCTL_PW_SET_GRANTEVENT:
{

// is this event clean?
if (!Globals.bGrantedEventClean) {
NtStatus = STATUS_INVALID_PARAMETER;
break;
}

Globals.bGrantedEventClean = FALSE;

Globals.grantedEvent =
IoCreateNotificationEvent(&Globals.grantedEventName,
&Globals.hGrantedEvent);
if (!Globals.grantedEvent) {
NtStatus = STATUS_UNSUCCESSFUL;
break;
}


back in the c# UI, im then using waitformultipleobjects to respond to
the event being hit ...


IntPtr[] objHandles = new IntPtr[] { m_objBlockEvent, m_objGrantEvent,
m_objExitEvent };
uint nIndex = 0;

while(m_blnRunning == true)
{
nIndex = Win32.WaitForMultipleObjects(3, objHandles, false,
Win32.INFINITE);

switch(nIndex)
{
case (uint) HANDLE.BLOCKED: //0
System.Diagnostics.Debug.WriteLine("Blocked!");
break;

case (uint) HANDLE.GRANTED: //1
System.Diagnostics.Debug.WriteLine("Granted!");
break;

case (uint) HANDLE.EXITED: //2
System.Diagnostics.Debug.WriteLine("Exited!");
m_blnRunning = false;
break;
}
}


my problem is my switch never gets hit, nothing even returns back from
waitformultipleobjects

i immediately thought that it was the string used for the event name ..
in the driver the string is declared as
#define PROTOWALL_KERNELEVENT_GRANTEDPACKET L"\\BaseNamedObjects\\ProtoWallGrantedPcktEvent"

and in the c# app its declared as
[MarshalAs(UnmanagedType.LPStr)]
public const string PROTOWALL_EVENT_GRANTEDPACKET =
"ProtoWallGrantedPcktEvent";

although ive tried every different possible combination of declaring
the string as different types (i thought .net strings where unicode by
default, and therefore would not require me to do any marshalling) but
nothing seems to work.


i have tried calling the event myself from inside the C# app using this
code ..
Win32.SetEvent(m_objExitEvent);
and it does work - so it seems i can hear when the event is responded
to, just seemingly not when it comes from the driver.


the subtext to this, is that i have a c++ version of the UI which does
work fine, (i.e. it correctly gets notified) so i dont think the driver
is at fault, either my porting of code, or some under the covers magic
about talking between drivers and .net code. (also everything is
running as administrator privs).

any help or things to try on this subject would be most welcome.

bb
 
B

Bennie Haelen

Hi,

After the line:
IntPtr objEvent = Win32.CreateEvent(IntPtr.Zero, false, false,
You might want to call
int error = Marshal.GetLastWin32Error();

Just to make sure that you did not get a Win32 error. It looks like the
code is correct, but you never know..

Also, after your call to WaitForMultipleObjects:
nIndex = Win32.WaitForMultipleObjects(3, objHandles, false,
Win32.INFINITE);

you might want to check for the WAIT_FAILED return value, which
indicates an error. Again, in this case you can use GetLastWin32Error()
to find the WIN32 error.

Good luck,

Bennie Haelen


I have a windows network device driver written in c++ and a user
interface im porting to c#, my problem is i dont seem to be getting
notified of the event calls from the driver to the c# app

im using the following code in c# in the UI to create an event

public static IntPtr OpenGrantedPacketEvent()
{
IntPtr objDriver = Driver.OpenDriver();
IntPtr objEvent = Win32.CreateEvent(IntPtr.Zero, false, false,
Driver.PW_EVENT_GRANTEDPACKET);
uint nBytesReturned = 0;

if(Win32.DeviceIoControl(objDriver, Driver.IOCTL_PW_SET_GRANTEVENT,
IntPtr.Zero, 0, IntPtr.Zero, 0, out nBytesReturned, IntPtr.Zero) == 0)
{
Driver.CloseEvent(objEvent);
Driver.CloseDriver(objDriver);

throw new Exception("Failed to create granted packet event.");
}

Driver.CloseDriver(objDriver);

return objEvent;
}




in the driver in c++ im doing this to create a notification event

case IOCTL_PW_SET_GRANTEVENT:
{

// is this event clean?
if (!Globals.bGrantedEventClean) {
NtStatus = STATUS_INVALID_PARAMETER;
break;
}

Globals.bGrantedEventClean = FALSE;

Globals.grantedEvent =
IoCreateNotificationEvent(&Globals.grantedEventName,
&Globals.hGrantedEvent);
if (!Globals.grantedEvent) {
NtStatus = STATUS_UNSUCCESSFUL;
break;
}


back in the c# UI, im then using waitformultipleobjects to respond to
the event being hit ...


IntPtr[] objHandles = new IntPtr[] { m_objBlockEvent, m_objGrantEvent,
m_objExitEvent };
uint nIndex = 0;

while(m_blnRunning == true)
{
nIndex = Win32.WaitForMultipleObjects(3, objHandles, false,
Win32.INFINITE);

switch(nIndex)
{
case (uint) HANDLE.BLOCKED: //0
System.Diagnostics.Debug.WriteLine("Blocked!");
break;

case (uint) HANDLE.GRANTED: //1
System.Diagnostics.Debug.WriteLine("Granted!");
break;

case (uint) HANDLE.EXITED: //2
System.Diagnostics.Debug.WriteLine("Exited!");
m_blnRunning = false;
break;
}
}


my problem is my switch never gets hit, nothing even returns back from
waitformultipleobjects

i immediately thought that it was the string used for the event name ..
in the driver the string is declared as
#define PROTOWALL_KERNELEVENT_GRANTEDPACKET L"\\BaseNamedObjects\\ProtoWallGrantedPcktEvent"

and in the c# app its declared as
[MarshalAs(UnmanagedType.LPStr)]
public const string PROTOWALL_EVENT_GRANTEDPACKET =
"ProtoWallGrantedPcktEvent";

although ive tried every different possible combination of declaring
the string as different types (i thought .net strings where unicode by
default, and therefore would not require me to do any marshalling) but
nothing seems to work.


i have tried calling the event myself from inside the C# app using this
code ..
Win32.SetEvent(m_objExitEvent);
and it does work - so it seems i can hear when the event is responded
to, just seemingly not when it comes from the driver.


the subtext to this, is that i have a c++ version of the UI which does
work fine, (i.e. it correctly gets notified) so i dont think the driver
is at fault, either my porting of code, or some under the covers magic
about talking between drivers and .net code. (also everything is
running as administrator privs).

any help or things to try on this subject would be most welcome.

bb
 
B

bb

thanks, but no i never get any errors.

im pretty convinced its either to do with the string name used for the
event, or the access to events created at kernel mode being limited to
my app.

still searching for an answer to this!

db
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top