-
May 23rd, 2009, 08:27 PM
#1
IoCreateDevice returns success, but I can't seem to connect to it
I've got code like
Code:
NTSTATUS ntStatus = STATUS_SUCCESS;
UNICODE_STRING deviceNameUnicodeString;
int i;
RtlInitUnicodeString(&deviceNameUnicodeString, L"\\Device\\MyDevice");
ntStatus = IoCreateDevice ( theDriverObject,
0, // For driver extension
&deviceNameUnicodeString,
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN,
FALSE,
&g_MyDevice
);
DbgPrint("I loaded:::%i:::\n", ntStatus);
KdPrint(("%wZ\n",&deviceNameUnicodeString));
Which shows up in DebugView like:
Code:
I loaded:::0:::
\Device\MyDevice
However, when I try to write a user-land program to connect to it by name:
Code:
#include <windows.h>
#include <stdio.h>
#include <stdlib.h>
void __cdecl main()
{
HANDLE hDevice;
DWORD errLevel;
hDevice = CreateFile( "\\\\Device\\MyDevice",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
errLevel = GetLastError();
printf("%i ::: %i\n", hDevice, errLevel);
}
I get the following output:
From looking at my winerror.h, I see that code 53 is ERROR_BAD_NETPATH "The network path was not found."
So: Am I successfully creating a Device? If so, how do I connect to it from a user-land program? If not, what am I doing wrong?
-
May 24th, 2009, 05:20 AM
#2
Re: IoCreateDevice returns success, but I can't seem to connect to it
Hello,
Try using the following in the CreateFile of the application:
"\\\\.\\MyDevice"
Windows replaces the " \\\\.\\ "with the "\??" which is an internal reference to the Object Manager that contains and controls various windows objects, it automatically looks in the device folder for device drivers.
Hope this works
Peter
Last edited by PeterBritton; May 24th, 2009 at 06:13 AM.
-
May 24th, 2009, 08:53 AM
#3
Re: IoCreateDevice returns success, but I can't seem to connect to it
Originally Posted by PeterBritton
Hello,
Try using the following in the CreateFile of the application:
"\\\\.\\MyDevice"
Trying this results in the output -1 ::: 2, which is apparently the error FILE_NOT_FOUND.
I've tried opening \\\\.\\C and \\\\.\\D this way, and get the same output. I've also tried changing CreateFile to creating a new file c:\myfile.txt, which failed with -1 ::: 2. I then tried it on c:\log.txt (which already exists), which gives output 2024 ::: 0.
-
May 24th, 2009, 10:41 AM
#4
Re: IoCreateDevice returns success, but I can't seem to connect to it
Hello,
In the driver code, there needs to be another unicode string set to:
L"\\DosDevices\\MyDevice"
and then use IoCreateSymbolicLink with the 2 unicode strings.
something along the lines of:
Code:
NTSTATUS ntStatus = STATUS_SUCCESS;
UNICODE_STRING deviceNameUnicodeString;
UNICODE_STRING deviceLINKNameUnicodeString;
int i;
RtlInitUnicodeString(&deviceNameUnicodeString, L"\\Device\\MyDevice");
RtlInitUnicodeString(&deviceNameUnicodeString, L"\\DosDevices\\MyDevice");
ntStatus = IoCreateDevice ( theDriverObject,
0, // For driver extension
&deviceNameUnicodeString,
FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN,
FALSE,
&g_MyDevice
);
IoCreateSymbolicLink (&deviceLINKNameUnicodeString, &deviceNameUnicodeString);
Dont forget to add error checking on the return codes.
The driver is still opened with the "\\\\.\\MyDevice"
Peter
-
May 24th, 2009, 10:51 AM
#5
Re: IoCreateDevice returns success, but I can't seem to connect to it
-
May 24th, 2009, 12:53 PM
#6
Re: IoCreateDevice returns success, but I can't seem to connect to it
Originally Posted by PeterBritton
Hello,
In the driver code, there needs to be another unicode string set to:
L"\\DosDevices\\MyDevice"
and then use IoCreateSymbolicLink with the 2 unicode strings.
That worked. Thanks a bunch. I have just a couple more questions:
- Is the reason it has to be a "DosDevices" is because the program I'm using to connect to it is a UMTYPE=console program?
- What kind of program would it need to be to connect to a plain "Device" rather than "DosDevices"?
-
May 25th, 2009, 05:28 AM
#7
Re: IoCreateDevice returns success, but I can't seem to connect to it
Hello
It's great that its working!
The driver already is a "plain device" - The reference to "DosDevices" is more of a compatibility issue with previous versions of Windows. When the kernel sees that you are making a symbolic link to "DosDevices", it replaces it with either "\??" or "\GLOBAL??" dependant on the context of the thread it was created.
The symbolic link is required for the application to use "CreateFile" to access the driver and not whether it is for a console or GUI type program.
Peter
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|