CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2009
    Posts
    3

    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:

    Code:
    -1 ::: 53
    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?

  2. #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.

  3. #3
    Join Date
    May 2009
    Posts
    3

    Re: IoCreateDevice returns success, but I can't seem to connect to it

    Quote Originally Posted by PeterBritton View Post
    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.

  4. #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

  5. #5

    Re: IoCreateDevice returns success, but I can't seem to connect to it

    The MSDN page for this can be found at:

    http://msdn.microsoft.com/en-us/library/ms794720.aspx

  6. #6
    Join Date
    May 2009
    Posts
    3

    Re: IoCreateDevice returns success, but I can't seem to connect to it

    Quote Originally Posted by PeterBritton View Post
    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:

    1. 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?
    2. What kind of program would it need to be to connect to a plain "Device" rather than "DosDevices"?

  7. #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
  •  





Click Here to Expand Forum to Full Width

Featured