Managed use of DeviceIoControl() and CreateFile()

N

Nate Kohari

Hello all,

I am developing an application in C# with a target platform of Windows
CE 4.2, and I need to make a call to DeviceIoControl() to control the
hardware of our target device. I'm using OpenNETCF (which is a
fantastic library by the way!), but whenever I use the
FileEx.CreateFile() wrapper to get a handle for the device, it throws
an error with the Win32 error code 55 (device not found).

I have an example (written in C++) from the device manufacturer of how
to manipulate the device, and basically I'm trying to port it to
managed code so I don't have to have a separate library. I'm certain
that I'm calling it with the correct device name, and the equivalent
access, share, and disposition parameters as their example shows.

Here is the line that throws the exception:

handle = FileEx.CreateFile(@"TGR1:",
FileAccess.Read | FileAccess.Write,
FileShare.ReadWrite,
FileCreateDisposition.OpenExisting,
0);

I'm by no means an expert when it comes to the Win32 API... does
something jump out at anyone? I can also create a secondary library in
C++ using the information the manufacturer provided me with and
P/Invoke functions in it, but if I can handle all of this in managed
code I'd much prefer to do so.


Thanks,
Nate Kohari
 
P

Paul G. Tobey [eMVP]

The most likely errors seem not to be the case for you, based on that code.
Leaving off the colon at the end of the device name or getting the number
wrong are #1 and #2. You probably don't want to set ReadWrite as the
sharing mode, in this case, either, but I don't think that will cause that
particular error. To make sure that the device name is actually correct,
open the registry with Remote Registry Editor in eVC and look in the key
path below:

HKEY_LOCAL_MACHINE\Drivers\Active

There should be a number of subkeys of that key. Each subkey is one
'device'. The values under those subkeys indicate the device names. Make
sure that there is one called TGR1.

In this unmanaged code example, I don't suppose that they are *starting* a
service and then talking to it, are they? Or loading the device driver? It
seems most likely that you either have the name wrong or the device driver
is actually not active. You might also try dropping the @...

Paul T.
 
N

Nate Kohari

Paul,

Thanks for your prompt reply. There is a key in
HKEY_LOCAL_MACHINE\Drivers\Active\05 with a name of "TGR1:". The
example they provide is basically just a call to CreateFile() to get
the device handle, which is subsequently passed to DeviceIoControl() to
send the request to the device.

The device in question is a hardware trigger with a configurable
software effect that happens when the trigger is pulled. The call to
DeviceIoControl() that I'm trying to execute will modify this trigger
effect. There's a control panel applet that allows this same change,
and I'm basically trying to reproduce it so it can be changed from
within our application.

I was thinking that maybe the problem is that the device is already in
use, but I figured that it would be a different error message. Also, I
thought that maybe there was an ASCII/Unicode issue, so I tried to
P/Invoke CreateFileW() directly and received the same error code from
Marshal.GetLastWin32Error(). I figured OpenNETCF would marshal the
managed strings to Unicode character buffers anyway, but I thought it
was worth a shot.

Any other ideas? I'm grasping at straws right now... like I said, I
don't have a problem with writing it in C++, but I'd like to avoid
adding external dependencies if I can.

Thanks again,
Nate
 
P

Paul G. Tobey [eMVP]

Test-wise, run your managed code application. When it fails, run an
unmanaged code application immediately after exiting the managed code one.
You should get the same error...

Paul T.
 
N

Nate Kohari

The plot thickens. I tried created a DLL in eVC with the following
simple function:

extern "C"
__declspec(dllexport)
HANDLE OpenTriggerHandle()
{
return CreateFile(L"TGR1:",
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
0,
OPEN_EXISTING,
0,
0);
}

Effectively, this function should do the same thing as what I'm trying
to do in C# with FileEx.CreateFile(). When I P/Invoke this function
from managed code, I again get an invalid handle (-1) and an error code
of 55. It definitely seems like the device is already in use... I've
contacted the device manufacturer to ask for further assistance, and
I'll keep tinkering.

Thanks for your help so far. If anyone else has any suggestions, I'd
appreciate them!


Nate
 

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