I personally thank sir Dana Epp… this is really helpfull for me to go ahead with the .NET implementation…
Thanks a lot…
Regards
K.Raju
-----Original Message-----
From: Dana Epp [mailto:xxxxx@vulscan.com]
Sent: Wednesday, July 14, 2004 11:44 PM
To: Windows File Systems Devs Interest List
Subject: Re: [ntfsd] Sharing memory in C#.NET
Krishnama,
Remember that one of the powers of C# (well any .NET lang really) is that when its not available in native managed code, you can marshal it! Just marshal DeviceIOControl through P/Invoke, and use DeviceIOControl.
To help you get started, I would first go read up on P/Invoke before mucking with comms to your kernel mode driver. MSDN has loads of info for that.
Once you have done that you need to determine which native functions in kernel32.dll have to be marshalled. You will at the very least need CreateFile, CloseHandle and DeviceIoControl. This should get you going with that:
DllImport(“kernel32.dll”, SetLastError=true, CharSet=CharSet.Auto)]
public static extern int CreateFile(
[MarshalAs(UnmanagedType.LPTStr)]string lpFileName,
[MarshalAs(UnmanagedType.U4)]uint dwDesiredAccess,
[MarshalAs(UnmanagedType.U4)]uint dwShareMode,
[MarshalAs(UnmanagedType.LPStruct)]SecurityAttributes lpSecurityAttributes,
[MarshalAs(UnmanagedType.U4)]uint dwCreationDisposition,
[MarshalAs(UnmanagedType.U4)]uint dwFlagsAndAttributes,
[MarshalAs(UnmanagedType.U4)]uint hTemplateFile );
[DllImport(“kernel32.dll”, EntryPoint=“CloseHandle”)]
public static extern uint CloseHandle(int handle);
[DllImport(“kernel32.dll”, SetLastError=true, CharSet=CharSet.Auto)]
public static extern bool DeviceIoControl(
[MarshalAs(UnmanagedType.U4)]int hDevice,
[MarshalAs(UnmanagedType.U4)]uint dwIoControlCode,
IntPtr lpInBuffer,
[MarshalAs(UnmanagedType.U4)]uint nInBufferSize,
IntPtr lpOutBuffer,
[MarshalAs(UnmanagedType.U4)]uint nOutBufferSize,
[MarshalAs(UnmanagedType.U4)]out int lpBytesReturned,
[MarshalAs(UnmanagedType.U4)]uint lpOverlapped );
From that point on, if you have used DeviceIoControl in VC++, you should be able to use the exact same methods in your C# code.
-
Open the driver with CreateFile
-
Read and write data through DeviceIoControl
-
Close the handle
Now, you will need to set the buffers for DeviceIoControl. As an IntPtr, you can alloc on the fly and do the work you need. I typically do something like:
IntPtr ptr = Marshal.AllocHGlobal( size_I_need );
To use the ptr (ie: inbound buffer in DeviceIoControl) you basically need to marshal the ptr to a application-specific structure you set up in the buffer… ie:
yourObj = (YourObjectStruct)Marshal.PtrToStructure( (IntPtr) ptr, ts );
Here is an example of a struct I use for communicating an action from a usermode app that I want my kernel mode code to know about:
[StructLayout(LayoutKind.Sequential, Size=4, Pack=1), CLSCompliant(false)]
public struct RequestLockProcessBarrier
{
public byte OpCode;
public byte action;
public ushort ID;
}
When done with it, don’t forget to do:
Marshal.FreeHGlobal( ptr );
Thats it. From that point on, read the DDK docs on using DeviceIoControl.
Good luck.
Krishnama Raju wrote:
HI All
How can I share a data structure between a FileSystem Driver and a User mode app, but the user mode is in C#.NET (WIndows Service) .
I’ve done this for VC++ App and FSD by using DeviceIoControl. It works fine. but I wanted to convert the same into C#.NET. How can I do that??
Please advice / can somebody give a sample code?.
Thank you in advance…
Regards
K.Raju
Questions? First check the IFS FAQ at https://www.osronline.com/article.cfm?id=17
You are currently subscribed to ntfsd as: xxxxx@vulscan.com
To unsubscribe send a blank email to xxxxx@lists.osr.com
–
Regards,
Dana Epp
[Blog: http://silverstr.ufies.org/blog/]
Questions? First check the IFS FAQ at https://www.osronline.com/article.cfm?id=17
You are currently subscribed to ntfsd as: xxxxx@inquesttechnologies.com
To unsubscribe send a blank email to xxxxx@lists.osr.com