CreateFile problem with device driver

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>

You don’t have access to the \Device\ portion of the object manager
namespace from Win32. Try opening “\\.\TUS1” instead.

-scott


Scott Noone
Software Engineer
OSR Open Systems Resources, Inc.
http://www.osronline.com

“Randy Hyde” wrote in message news:xxxxx@ntdev…
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>

I would recommend using device interfaces and not fixed names. This way
you don’t have TrosUsbXxxx and have to iterate. Are you clearing
DO_DEVICE_INITIALIZING from DeviceObject->Flags?

D

(Also, you can allocate the symbolic buffer name out paged pool)

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Randy Hyde
Sent: Friday, July 15, 2005 12:51 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] CreateFile problem with device driver

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


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: unknown lmsubst tag argument:
‘’
To unsubscribe send a blank email to xxxxx@lists.osr.com</windows.h></stdio.h>

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Scott Noone
Sent: Friday, July 15, 2005 1:02 PM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] CreateFile problem with device driver

You don’t have access to the \Device\ portion of the object manager
namespace from Win32. Try opening “\\.\TUS1” instead.

-scott

Yup, did that, and about every other variation you can think of
(e.g., \DosDevices\TUS1, which is the name that QueryDosDevice
returns).
Still, no dice.

I know the device is there, I can see it when using the SetupDi***
calls. And, of course, QueryDosDevice sees it. For some reason, I can’t
connect to *any* filename for this guy.
Cheers,
Randy Hyde

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Doron Holan
Sent: Friday, July 15, 2005 1:01 PM
To: Windows System Software Devs Interest List
Subject: RE: [ntdev] CreateFile problem with device driver

I would recommend using device interfaces and not fixed names. This way
you don’t have TrosUsbXxxx and have to iterate.

Yes, ultimately this is what I plan to do. Indeed, I also had the code
in there to register a device interface (which I commented out, thinking
maybe *that* had something to do with my problems; but that didn’t
affect anything).

>>>>>>>>>>>>>>>>>>>>
Are you clearing
DO_DEVICE_INITIALIZING from DeviceObject->Flags?


Yes, it’s the last thing I do before returning:

deviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
debugf(( “TROYUSB/AddDevice: exit, status=%x\n”, status ));
_return( status );

>>>>>>>>>>>>>>>
D

(Also, you can allocate the symbolic buffer name out paged pool)

Good point, thanks for catching that.
Cheers,
Randy Hyde

I’m new to device drivers development. Could you point out how can I use a
created link when I use device interfaces? I.e. how to pass it to a user
mode application?

“Doron Holan” ???/??? ? ???
???: news:xxxxx@ntdev…
I would recommend using device interfaces and not fixed names. This way
you don’t have TrosUsbXxxx and have to iterate. Are you clearing
DO_DEVICE_INITIALIZING from DeviceObject->Flags?

D

(Also, you can allocate the symbolic buffer name out paged pool)

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Randy Hyde
Sent: Friday, July 15, 2005 12:51 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] CreateFile problem with device driver

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


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: unknown lmsubst tag argument:
‘’
To unsubscribe send a blank email to xxxxx@lists.osr.com</windows.h></stdio.h>

If you use device interface, you use SetupDi APIs to enumerate the
interface GUID to get the interface name. See

SetupDiGetClassDevs
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/b
ase/setupdigetclassdevs.asp

SetupDiEnumDeviceInterfaces
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/b
ase/setupdienumdeviceinterfaces.asp

(beware of wrap)

If you are using hardcoded names like the example below, the application
must have explicit knowledge of that name.

d

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Roman Kudinov
Sent: Saturday, July 16, 2005 1:00 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] CreateFile problem with device driver

I’m new to device drivers development. Could you point out how can I use
a
created link when I use device interfaces? I.e. how to pass it to a user
mode application?

“Doron Holan” ???/??? ? ???
???: news:xxxxx@ntdev…
I would recommend using device interfaces and not fixed names. This way
you don’t have TrosUsbXxxx and have to iterate. Are you clearing
DO_DEVICE_INITIALIZING from DeviceObject->Flags?

D

(Also, you can allocate the symbolic buffer name out paged pool)

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Randy Hyde
Sent: Friday, July 15, 2005 12:51 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] CreateFile problem with device driver

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


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: unknown lmsubst tag argument:
‘’
To unsubscribe send a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@windows.microsoft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com</windows.h></stdio.h>

thanks

“Doron Holan” ???/??? ? ???
???: news:xxxxx@ntdev…
If you use device interface, you use SetupDi APIs to enumerate the
interface GUID to get the interface name. See

SetupDiGetClassDevs
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/b
ase/setupdigetclassdevs.asp

SetupDiEnumDeviceInterfaces
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/devio/b
ase/setupdienumdeviceinterfaces.asp

(beware of wrap)

If you are using hardcoded names like the example below, the application
must have explicit knowledge of that name.

d

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Roman Kudinov
Sent: Saturday, July 16, 2005 1:00 AM
To: Windows System Software Devs Interest List
Subject: Re:[ntdev] CreateFile problem with device driver

I’m new to device drivers development. Could you point out how can I use
a
created link when I use device interfaces? I.e. how to pass it to a user
mode application?

“Doron Holan” ???/??? ? ???
???: news:xxxxx@ntdev…
I would recommend using device interfaces and not fixed names. This way
you don’t have TrosUsbXxxx and have to iterate. Are you clearing
DO_DEVICE_INITIALIZING from DeviceObject->Flags?

D

(Also, you can allocate the symbolic buffer name out paged pool)

-----Original Message-----
From: xxxxx@lists.osr.com
[mailto:xxxxx@lists.osr.com] On Behalf Of Randy Hyde
Sent: Friday, July 15, 2005 12:51 PM
To: Windows System Software Devs Interest List
Subject: [ntdev] CreateFile problem with device driver

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


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: unknown lmsubst tag argument:
‘’
To unsubscribe send a blank email to xxxxx@lists.osr.com


Questions? First check the Kernel Driver FAQ at
http://www.osronline.com/article.cfm?id=256

You are currently subscribed to ntdev as: xxxxx@windows.microsoft.com
To unsubscribe send a blank email to xxxxx@lists.osr.com</windows.h></stdio.h>