Sysinternals Homepage
Forum Home Forum Home > Windows Discussions > Development
  New Posts New Posts RSS Feed - CreateFile - unable to open a handle to a device
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

CreateFile - unable to open a handle to a device

 Post Reply Post Reply Page  12>
Author
Message
swurly View Drop Down
Newbie
Newbie


Joined: 17 July 2008
Location: New Zealand
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote swurly Quote  Post ReplyReply Direct Link To This Post Topic: CreateFile - unable to open a handle to a device
    Posted: 17 July 2008 at 9:40pm
Hi,

I am in the process of just doing some simple device driver development, and I wanted to make a simple User mode program to communicate with a simple device. However, I am unable to open a handle to it.

I know that the device is present by using Device Tree.

The error code I am receiving from CreateFile is 53, which is for network path is not found.

Both the device driver code and the user mode code are given below:

#include <ntddk.h>
#include <stdio.h>

const WCHAR deviceNameBuffer[]= L"\\Device\\MyDevice";
PDEVICE_OBJECT g_Device;

VOID OnUnload(IN PDRIVER_OBJECT DriverObject) {
    DbgPrint("OnUnload called\n");
    IoDeleteDevice(g_Device);
}

NTSTATUS DispatchCreate(IN PDEVICE_OBJECT DeviceObject, IN PIRP Irp) {
    DbgPrint("DispatchCreate called\n");
   
    Irp->IoStatus.Status = STATUS_SUCCESS;
    IoCompleteRequest(Irp, IO_NO_INCREMENT);
   
    return STATUS_SUCCESS;
}

NTSTATUS DriverEntry(IN PDRIVER_OBJECT theDriverObject,
        IN PUNICODE_STRING theRegistryPath) {
    
    NTSTATUS ntStatus;
    UNICODE_STRING deviceNameUnicodeString;
   
    DbgPrint("DriverEntry called\n");
    DbgPrint("Test0\n");
   
    theDriverObject->DriverUnload = OnUnload;
    theDriverObject->MajorFunction[IRP_MJ_CREATE] = DispatchCreate;
   
    // Create Device
    RtlInitUnicodeString(&deviceNameUnicodeString, deviceNameBuffer);
    ntStatus = IoCreateDevice(theDriverObject, 0, &deviceNameUnicodeString, FILE_DEVICE_UNKNOWN,
                        FILE_DEVICE_SECURE_OPEN, TRUE, &g_Device);
                       
    if (NT_SUCCESS(ntStatus)) {
        DbgPrint("Device created\n");
    }
   
    return STATUS_SUCCESS;
}



#include <windows.h>
#include <stdio.h>

int main() {
    HANDLE hDevice;
   
    printf("Interaction Program Test 0\n");
    hDevice = CreateFile("\\\\Device\\MyDevice", GENERIC_READ | GENERIC_WRITE,
                    0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   
    if (hDevice == INVALID_HANDLE_VALUE) {
        printf("Device handle failed with error : %d\n", GetLastError());
        return -1;
    }
    else {
       
        printf("Device handle created successfully!");
    }
   
    CloseHandle(hDevice);
   
    return 0;
}
   





Edited by swurly - 17 July 2008 at 9:40pm
Back to Top
molotov View Drop Down
Moderator Group
Moderator Group
Avatar

Joined: 04 October 2006
Status: Offline
Points: 17531
Post Options Post Options   Thanks (0) Thanks(0)   Quote molotov Quote  Post ReplyReply Direct Link To This Post Posted: 18 July 2008 at 3:54am
Hi Richard,
 
Wonder what would happen if you try:
Quote     hDevice = CreateFile("\\\\.\\MyDevice", GENERIC_READ | GENERIC_WRITE,
                    0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
Daily affirmation:
net helpmsg 4006
Back to Top
Diablo View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 July 2008
Location: Western Sahara
Status: Offline
Points: 251
Post Options Post Options   Thanks (0) Thanks(0)   Quote Diablo Quote  Post ReplyReply Direct Link To This Post Posted: 19 July 2008 at 7:28am
Originally posted by molotov molotov wrote:

Wonder what would happen if you try:     hDevice = CreateFile("\\\\.\\MyDevice", GENERIC_READ | GENERIC_WRITE,
                    0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

He doesn't created symbolic link, only device.

To open device he should use NtCreateFile instead of CreateFile.
Back to Top
swurly View Drop Down
Newbie
Newbie


Joined: 17 July 2008
Location: New Zealand
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote swurly Quote  Post ReplyReply Direct Link To This Post Posted: 22 July 2008 at 2:48am
Two sources I have been too say that you can use CreateFile to access a device.

Here the article from codeproject:

http://www.codeproject.com/KB/system/driverdev.aspx

I have reformatted my laptop and installed VS 2008 which our university gives us access too. Originally I was running VS 6 with the very old SDK.

Anyway, when I get home later I will try all this out and post back. Thanks for the feedback guys.
Back to Top
swurly View Drop Down
Newbie
Newbie


Joined: 17 July 2008
Location: New Zealand
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote swurly Quote  Post ReplyReply Direct Link To This Post Posted: 22 July 2008 at 2:54am
I just checked MSDN on CreateFile and ntCreateFile:


CreateFile
Creates or opens a file, file stream, directory, physical disk, volume, console buffer, tape drive, communications resource, mailslot, or named pipe.



NtCreateFile
The NtCreateFile function either creates a new file or directory, or opens an existing file, device, directory, or volume.


Anyway, I will test this out when I get home.

Cheers (sorry for the double post).



Edited by swurly - 22 July 2008 at 2:56am
Back to Top
himanshup View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 July 2008
Location: India
Status: Offline
Points: 127
Post Options Post Options   Thanks (0) Thanks(0)   Quote himanshup Quote  Post ReplyReply Direct Link To This Post Posted: 22 July 2008 at 7:54am
the file name your are writing in createfile is wrong.
\\\\.\\filename. this convention is used when u open device by calling createfile.
Back to Top
Diablo View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 July 2008
Location: Western Sahara
Status: Offline
Points: 251
Post Options Post Options   Thanks (0) Thanks(0)   Quote Diablo Quote  Post ReplyReply Direct Link To This Post Posted: 22 July 2008 at 8:55am
Quote Two sources I have been too say that you can use CreateFile to access a device.

Show me this wonderful sources.

In link on article you posted they use CreateFile, because driver before created symbolic link. Your code doesn't contains any signs of Symbolic links.
Back to Top
swurly View Drop Down
Newbie
Newbie


Joined: 17 July 2008
Location: New Zealand
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote swurly Quote  Post ReplyReply Direct Link To This Post Posted: 22 July 2008 at 9:04am
The symbolic link makes it easier to open, but you DON'T NEED it. Even when I had the symbolic link it didn't work. [EDIT] Made a mistake in original code Angry, still would like to figure out why I can't get it work without it [/EDIT]

The other source is the Rootkits : Subverting the Windows Kernel book. They use CreateFile with and without Symbolic Link. All the symbolic link changed was the path used to reference the device.

Anyway, just as a side question. For NtCreateFile you must specify an OBJECT_ATTRIBUTES structure, however to use the macro InitializeObjectAttributes I have to use ntddk.h. However, under visual studio I am having trouble adding the header files and librarys from the ddk to the Visual Studio IDE. Any advice?

[EDIT]Just out of curiosity, what other ways do you communicate with the device from a usermode program?[/EDIT]



Edited by swurly - 22 July 2008 at 10:04am
Back to Top
Diablo View Drop Down
Senior Member
Senior Member
Avatar

Joined: 16 July 2008
Location: Western Sahara
Status: Offline
Points: 251
Post Options Post Options   Thanks (0) Thanks(0)   Quote Diablo Quote  Post ReplyReply Direct Link To This Post Posted: 22 July 2008 at 9:28am
Quote but you DON'T NEED it.

Yes.
Quote Rootkits : Subverting the Windows Kernel book

I would not point on this "book" as a serious technical book because it contains mostly untested and out-of-date code.

Quote However, under visual studio I am having trouble adding the header files and librarys from the ddk to the Visual Studio IDE. Any advice?

You should set paths to the DDK headers in project options and environment variables. Doing this will let you easy build drivers with any version of MSVC.

Quote Just out of curiosity, what other ways do you communicate with the device from a usermode program?

Yes. One of them WriteFile.
Back to Top
swurly View Drop Down
Newbie
Newbie


Joined: 17 July 2008
Location: New Zealand
Status: Offline
Points: 8
Post Options Post Options   Thanks (0) Thanks(0)   Quote swurly Quote  Post ReplyReply Direct Link To This Post Posted: 22 July 2008 at 9:58am
Alrightly, I redid it creating a Symbolic link and it created the handle successfully. Would really like to know why the CreateFile doesn't work. Thanks for the tip on the Rootkits book. I thought something as simple as the code for communicating with the device would be simple and would also work.

About seting the directories in VS. I have added the DDK\inc\ddk\wxp to the include, and I have also added the DDK\lib\wxp to the libraries, however I now have the error messages such as:

d:\program files\windows ddk\inc\ddk\wxp\ntdef.h(84) : warning C4005: 'MAX_NATURAL_ALIGNMENT' : macro redefinition
        c:\program files\microsoft sdks\windows\v6.0a\include\winnt.h(55) : see previous definition of 'MAX_NATURAL_ALIGNMENT'

Obviously this is due to the conflict definitinos made from the ddk and the sdk, however is it not unusual to want to include declartions and macros from ntddk.h while usuing regular functions from SDK with their declarations from windows.h?
For a simple program such as this:


#include <windows.h>
#include <stdio.h>
#include <ntddk.h>

int main()
{
    MessageBox(NULL, L"Error", L"Error", MB_OK);

    return 0;
}


Thanks for all the help guys. I realise a lot of these questions are tedious and are regular noob-type questions, however I am just really unfamilar with the VS2008 and DDK. The majority of my coding has been done on Linux and with usermode Windows API.

Thanks again.
-Richard


Edited by swurly - 22 July 2008 at 10:12am
Back to Top
 Post Reply Post Reply Page  12>
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 11.06
Copyright ©2001-2016 Web Wiz Ltd.