I’ve gotten a device driver (marginally) working and communicating with
a bus enumerator driver that I also wrote. I’ve written an internal
IOCTL handler for the bus enum and I’ve managed to open the bus enum
from the second driver and make appropriate calls (using zwCreateFile).
So far, so good. Now, I want to create a user-land app that opens the
second driver and makes standard DeviceIoControl calls. I’ve given the
device a name (and it shows up in the object directory) and I’ve created
a symbolic link, which I can enumerate. But when I attempt to open that
device, I get the following error from Windows:
“The system cannot find the path specified.”
Here’s the app I’m using to attempt to open the file:
#include <stdio.h>
#include <windows.h>
#define IOCTL_TROYUSB_SEND_URB 0x2a004c
// BOOL DeviceIoControl(
// HANDLE hDevice,
// DWORD dwIoControlCode,
// LPVOID lpInBuffer,
// DWORD nInBufferSize,
// LPVOID lpOutBuffer,
// DWORD nOutBufferSize,
// LPDWORD lpBytesReturned,
// LPOVERLAPPED lpOverlapped
// );
#define deviceName “TUS1”
int main( int argc, char **argv )
{
BOOL result;
HANDLE handle;
DWORD bytesReturned;
DWORD numDosChars;
char dosName[512];
memset( dosName, 0, 512 );
numDosChars = QueryDosDevice( deviceName, dosName, 256 );
printf( “qdd: %d, ‘%s’\n”, numDosChars, dosName );
handle =
CreateFile
(
dosName, //“\Device\TroyUsb0001”,
FILE_ALL_ACCESS, // dwDesiredAccess
FILE_SHARE_READ, // dwShareMode,
NULL, // lpSecurityAttributes,
OPEN_EXISTING, // dwCreationDisposition,
0, // dwFlagsAndAttributes,
NULL // hTemplateFile
);
if( handle != INVALID_HANDLE_VALUE )
{
printf( “Making DeviceIoControl Call:\n” );
result =
DeviceIoControl
(
handle,
IOCTL_TROYUSB_SEND_URB, // dwIoControlCode,
NULL, // lpInBuffer,
0, // nInBufferSize,
NULL, // lpOutBuffer,
0, // nOutBufferSize,
&bytesReturned,
NULL // lpOverlapped
);
}
else
{
char lpBuffer[1024];
FormatMessage
(
FORMAT_MESSAGE_FROM_SYSTEM,
NULL,
GetLastError(),
0,
lpBuffer,
256,
NULL
);
printf( “Could not open ‘%s’:\n%s\n”, dosName, lpBuffer );
}
return 0;
}
I’ve tried just about every name I can think of for the first parameter
to CreateFile and I generally get the same result (or an error more
specific, such as bad pathname syntax). Any idea what I’m doing wrong?
Here’s the code I used for creating the device name in the driver itself
(pretty much copied straight out of Oney’s and Baker/Lozano’s books):
devIndex = InterlockedIncrement( &lastIndex );
_snwprintf( name, maxNameLen_c, L"\Device\TroyUsb%04d",
devIndex );
RtlInitUnicodeString( &devName, name );
debugf(( “TROYUSB/AddDevice: devName: \Device\TroyUsb%04d\n”,
devIndex ));
// Create the Troy USB stack device:
status =
IoCreateDevice
(
DriverObject, // our driver object
sizeof (FDO_DEVICE_DATA), // device object extension size
&devName, // Normally, FDOs
don’t have names.
FILE_DEVICE_BUS_EXTENDER, // We are a TROYUSB
0, //FILE_DEVICE_SECURE_OPEN, //
TRUE, // our FDO is exclusive
&deviceObject // The device object created
);
.
.
.
_snwprintf( name, maxNameLen_c,
L"\DosDevices\TUS%d", devIndex );
RtlInitUnicodeString( &dosName, name );
deviceData->DosName.MaximumLength =
dosName.MaximumLength;
deviceData->DosName.Buffer =
ExAllocatePoolWithTag
(
NonPagedPool,
dosName.MaximumLength,
‘DosN’
);
_block // Block #3
_if( deviceData->DosName.Buffer == NULL
)
status =
STATUS_INSUFFICIENT_RESOURCES;
_break; // Out of block #3
_endif
RtlCopyUnicodeString(
&deviceData->DosName, &dosName );
IoCreateSymbolicLink( &dosName, &devName
);
debugf
((
“TROYUSB/AddDevice: Dos Name:
\DosDevices\TUS%d\n”,
devIndex
));
Thanks,
Randy Hyde</windows.h></stdio.h>