Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / April 2008

Tip: Looking for answers? Try searching our database.

Driver device IO control by C#

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Kid - 30 Apr 2008 10:10 GMT
hi

Can I CreateFile and DeviceIOControl by C# ?

Does it use the same functions as Visual C , what file should I include and
link ?

Thank you .
Willy Denoyette [MVP] - 30 Apr 2008 10:43 GMT
> hi
>
> Can I CreateFile and DeviceIOControl by C# ?

Yes, by using PInvoke.

> Does it use the same functions as Visual C , what file should I include
> and
> link ?

There is nothing to link or include, you need to declare the function in C#,
the runtime will dynamically load and call the function from the DLL.
As an example, following are the PInvoke declarations (C#) for the
kernel32.dll API's CreateFile, DeviceIoControl and CloseHandle.

       [DllImport("kernel32.dll", CharSet = CharSet.Auto,
SetLastError=true)]
       static extern SafeFileHandle CreateFile(string lpFileName, int
dwDesiredAccess, int dwShareMode,
         IntPtr lpSecurityAttributes, uint dwCreationDisposition, uint
dwFlagsAndAttributes,
         SafeFileHandle hTemplateFile);

       [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError =
true)]
       static extern int DeviceIoControl(SafeFileHandle handle, uint
dwIoControlCode, IntPtr lpInBuffer,
         int nInBufferSize, IntPtr lpOutBuffer, int nOutBufferSize,
         out int lpBytesReturned, IntPtr lpOverlapped);

        [DllImport("kernel32.dll")]
        static extern bool CloseHandle(IntPtr hObject);

note that you also need to declare all of the constants used as argument in
an API call, for instance:

       const int GENERIC_READ = unchecked((int)0x80000000);
       const int GENERIC_WRITE = unchecked((int)0x40000000);
       ..
       const int OPEN_EXISTING = 3;

In short you need to convert the header file contents related to the API you
want to use into C# style declarations.

When you are done with all this, you can call these from C# just like this:

SafeFileHandle h = CreateFile("\\\\.\\PhysicalDrive1",  GENERIC_READ, ...);

You may want to consult the PInvoke site here <http://www.pinvoke.net/>
before you start to declare some API's yourself, but watch out, some API's
are declared incorrectly, some are missing the structs constant or enum
declarations, and a large deal is simply covered by managed API's.

Willy.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.