473,788 Members | 2,855 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Driver device IO control by C#

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 .
Jun 27 '08 #1
1 2342
"Kid" <Ki*@discussion s.microsoft.com wrote in message
news:37******** *************** ***********@mic rosoft.com...
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("kern el32.dll", CharSet = CharSet.Auto,
SetLastError=tr ue)]
static extern SafeFileHandle CreateFile(stri ng lpFileName, int
dwDesiredAccess , int dwShareMode,
IntPtr lpSecurityAttri butes, uint dwCreationDispo sition, uint
dwFlagsAndAttri butes,
SafeFileHandle hTemplateFile);

[DllImport("kern el32.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("kern el32.dll")]
static extern bool CloseHandle(Int Ptr 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("\\\ \.\\PhysicalDri ve1", 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.


Jun 27 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1221
by: CS Loh | last post by:
Hi, I install a filter driver through INF file to a 3rd party audio device (with driver). The filter driver works well on the 3rd party driver. However upon uninstallation of the filter driver, it will eventually cause the audio driver get uninstalled as well. Here are the steps to uninstall the filter driver: 1. Disable the target audio device. 2. Delete the filter name from "Upperfilters" key of the audio device in
2
6981
by: nbhalala | last post by:
Hello Friends, Hi Myself Naresh (B.E. Comp. Eng) from Mumbai... I'm a Linux Device Driver Writer... I would like to learn writing Driver of "USB Devices"...BUT before that I'm presently working on "PCI ETHERNET NETWORK ADAPTER (RTL8139)" for that - reading it's Datasheet and trying to write simple backbone Driver BUT for that friend I need your Help... Resources I have/using -
12
13774
by: Steve | last post by:
I wrote a simple virtual device driver int15.sys, Is C# support load the device driver from AP?
3
5951
by: bb | last post by:
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,
7
4451
by: Ritu | last post by:
Hi All, Can any body please tell me how i can write a device driver using CSharp. Thanks, Ritu
0
1546
by: am | last post by:
Hi, I have an USB hardware device that came with some (poor) software. I would like to write my own software to use the hardware in dotnet. There are no COM dll's to reference from the original software. I have read a lot in newsgroups and everywhere it is stated that "you need a device-driver" - well I got the device-driver (.sys file in windows/system32 folder)
6
2009
by: Fla | last post by:
Hy! I'm newbie to VB 2005 and I'm trying to read value from a device driver, as I used in VB6 with CreateFile and DeveiceIoControl. I get a correct driver handle with CreateFile but I can't read values with ReadFile, as reported in this URL http://support.microsoft.com/default.aspx?scid=kb;en-us;823179&Product=vb6 I tried with ReadFile with the following code lines: Try
0
1161
by: Fla | last post by:
Hy! I'm newbie to VB 2005 and I'm trying to read value from a device driver, as I used in VB6 with CreateFile and DeveiceIoControl. I get a correct driver handle with CreateFile but I can't read values with ReadFile, as reported in this URL http://support.microsoft.com/default.aspx?scid=kb;en-us;823179&Product=vb6 I tried with ReadFile with the following code lines: Try
3
2516
by: shakthi | last post by:
Hi all. Am a beginner in device driver programming. Please tell me how to get handle for a pseudo driver..., a driver with no device connected i suppose.Am using CreateFile() win32 API. Since it is a pseudo driver, what would the first parameter ?i.e the device name.How to get it? Am new to device drivers. Plz help me in this aspect. Thanks a lot in advance!! Regards, skt.
1
2100
by: dupe576 | last post by:
Hey, My boss put me in charge of getting this pin pad device to work with our software. We have it plugging into the usb port with a converter cable that converts it to a rs232 port. This converter cable uses the pl2303 driver to aid with this. I am new to drivers, particularly on the windows OS. My question is how do I use this driver to communicate with the device? I'm trying to read into it but am being overloaded by...
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9498
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10370
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9969
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8995
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5538
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4074
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3677
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2896
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.