Hello Members,
I am new to this list, please help me i have a problem doing a native call to kernel mode driver from c#.
I want to connect to a kernel mode file system filter driver via c#.
First I wrote in native c++ and everyting worked fine, this was just for testing issue during the driver development, because the user mode application should run under .NET so I switched to
c#
The call of FilterConnectCommunicationPort works fine from c# with the follwing code:
[DllImport(“fltlib”, SetLastError = true)]
public static extern int FilterConnectCommunicationPort([MarshalAs(UnmanagedType.LPWStr)]
string portName,
uint options,
IntPtr context,
uint sizeOfContext,
IntPtr securityAttributes,
IntPtr portPtr);
IntPtr portPtr = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
string portName = “\FilterPort”;
int status = FilterConnectCommunicationPort(
portName,
0,
IntPtr.Zero,
0,
IntPtr.Zero,
portPtr);
As I could watch in the debug output of the driver the connectNotfiyCallback Routine is called and the status
on the client side is 0 which means, that the operation was successful.
so everything was fine until here, but
when I call FilterGetMessage from the clientside it results in a status of 80070006 by the following code:
[DllImport(“fltlib”, CallingConvention = CallingConvention.Winapi, CharSet = CharSet.Auto, SetLastError = true)]
public static extern unsafe uint FilterGetMessage(
SafeFileHandle hPort,
IntPtr messageBuffer,
int msgSize,
IntPtr Overlapped);
IntPtr message = Marshal.AllocHGlobal(Marshal.SizeOf(typeof(IntPtr)));
SafeFileHandle sf = new SafeFileHandle(portPtr, true);
status = FilterGetMessage(
sf,
message,
Marshal.SizeOf(typeof(IntPtr)),
IntPtr.Zero);
So I guess, that there is anything wrong with the Arguments in FilterGetMessage.
I tried also to define structs for the message because in the native world, the message Argument must be
a type of FILTER_MESSAGE_HEADER like this but it did not work either:
[StructLayout(LayoutKind.Explicit)]
public struct FILTER_MESSAGE_HEADER
{
[FieldOffset(0)]
public UInt32 ReplyLength;
[FieldOffset(4)]
public UInt64 MessageId;
}
[StructLayout(LayoutKind.Explicit)]
public struct RBAC_SYSTEM_MESSAGE
{
[FieldOffset(0)]
public UInt32 BytesToScan;
[FieldOffset(4)]
public UInt32 Reserved;
[FieldOffset(8)]
public char Contents;
}
[StructLayout(LayoutKind.Explicit)]
public struct RBAC_MESSAGE
{
[FieldOffset(0)]
public FILTER_MESSAGE_HEADER MessageHeader;
[FieldOffset(12)]
public RBAC_SYSTEM_MESSAGE MessageContent;
}
RBAC_MESSAGE message = new RBAC_MESSAGE();
message.MessageContent = new RBAC_SYSTEM_MESSAGE();
message.MessageHeader = new FILTER_MESSAGE_HEADER();
SafeFileHandle sf = new SafeFileHandle(portPtr, true);
status = NativeConnector.FilterGetMessage(
sf,
ref message.MessageHeader,
(uint)Marshal.SizeOf(message),
IntPtr.Zero);
Please could anyone help me sending example code for receiving messages and replying messages to kernel mode with c# or giving me a hint, thank you,
elias
Jetzt kostenlos herunterladen: Internet Explorer 8 und Mozilla Firefox 3.5 -
sicherer, schneller und einfacher! http://portal.gmx.net/de/go/chbrowser