none
CreateFile Fails with Device Path RRS feed

  • Question

  • Hi all,

         I have written one windows 7 driver for my device. Now i want to access this device from my user mode application. For this i put this following code on my driver.

    NTSTATUS status = STATUS_SUCCESS; PDEVICE_OBJECT deviceObject = NULL; PLOCAL_DEVICE_INFO deviceInfo; UNICODE_STRING ntDeviceName; UNICODE_STRING win32DeviceName; PUNICODE_STRING pSymbolicLink; // init our buffers WCHAR wcaDevNameBuff[] = SNAP_DEVICE_NAME; WCHAR wcaDevLinkBuff[] = DOS_DEVICE_NAME; // this code can be paged - ensures that the calling thread is running at a IRQL // low enough to permit paging PAGED_CODE(); DebugPrint(("Entered Add Device:\n")); DebugPrint(("Driver Object Instance: (%x) PhysDeviceObject: (%x)\n", DriverObject, PhysicalDeviceObject )); // gulNoDevices++; DebugPrint(("SnapAddDevice Call No (%x)\n", (ULONG)gdevNum)); // snap NT device names and DOS device names now appended to // include an instance number wcaDevNameBuff[(sizeof(wcaDevNameBuff) / 2) - 2] = L'0' + gdevNum; wcaDevLinkBuff[(sizeof(wcaDevLinkBuff) / 2) - 2] = L'0' + gdevNum; // create our device name string RtlInitUnicodeString(&ntDeviceName, wcaDevNameBuff); // // Create a device object. Note that this routine will have to // be modified for multiple devices (Device name cannot be duplicated) status = IoCreateDevice (DriverObject, sizeof (LOCAL_DEVICE_INFO), &ntDeviceName, FILE_DEVICE_SNAPSHOT, 0, FALSE, &deviceObject); // check return status if (!NT_SUCCESS (status)) { // // Either not enough memory to create a deviceobject or another // deviceobject with the same name exits. This could happen // if you install another instance of this device. DebugPrint(("IoCreateDevice Failed: (%x)\n", status)); return status; } // create our WIN32 device name, and create the matching symbolic link RtlInitUnicodeString(&win32DeviceName, wcaDevLinkBuff); // create the synbolic link status = IoCreateSymbolicLink(&win32DeviceName, &ntDeviceName ); // check the status if (!NT_SUCCESS(status)) { // If we we couldn't create the link then // abort installation. IoDeleteDevice(deviceObject); return status; } // get a pointer to our device extension. This is our custom structure deviceInfo = (PLOCAL_DEVICE_INFO) deviceObject->DeviceExtension; //Adding the interface GUID for this device status = IoRegisterDeviceInterface ( PhysicalDeviceObject, (LPGUID) &SNAPSHOT_INTERFACE_GUID, NULL, &win32DeviceName); DebugPrint(("&win32DeviceName : (%s)\n", &win32DeviceName)); //ends // Set the pointer to the next driver down in WDM layered driver fashon deviceInfo->NextLowerDriver = IoAttachDeviceToDeviceStack( deviceObject, PhysicalDeviceObject); // if there is no lower driver then this driver will not work (device does // not exist, delete the device object and the symbolic link if(NULL == deviceInfo->NextLowerDriver) { IoDeleteSymbolicLink(&win32DeviceName); IoDeleteDevice(deviceObject); return STATUS_NO_SUCH_DEVICE; } // initialize a remove lock for the device, This lock can track any outstanding // I/O requests on a device and determine when to delete its device object in // response to a IRP_MN_REMOVE_DEVICE request. IoInitializeRemoveLock (&deviceInfo->RemoveLock, // lock SNAPSHOTIO_TAG, // tag ID 1, // MaxLockedMinutes 5); // HighWatermark, this parameter is // used only on checked build. // Set the flag if the device is not holding a pagefile // crashdump file or hibernate file. deviceObject->Flags |= DO_POWER_PAGABLE; // initialise our device structure deviceInfo->DeviceObject = deviceObject; deviceInfo->Removed = FALSE; deviceInfo->Started = FALSE; // add our global ID deviceInfo->ulThisID = gdevNum; // increment or global device number gdevNum++; // we're finished initislising deviceObject->Flags &= ~DO_DEVICE_INITIALIZING; DebugPrint(("Clearing LDI Memory...\n")); // clear our memory structure RtlZeroMemory(&deviceInfo->DeviceInterface, sizeof(DSP_INTERFACE)); // These values are based on the hardware design. // Let us assume the following address // base 0 and 1 = memory space, // base 2 = I/O space. deviceInfo->DeviceInterface.BaseMemory[0].uBaseMemoryType = 0; // base 0 is memory mapped deviceInfo->DeviceInterface.BaseMemory[1].uBaseMemoryType = 0; // base 1 is memory mapped deviceInfo->DeviceInterface.BaseMemory[2].uBaseMemoryType = 1; // base 2 is port I/O mapped // debug statment DebugPrint(("AddDevice: %p to %p->%p \n", deviceObject, deviceInfo->NextLowerDriver, PhysicalDeviceObject));

    Now i have called the CreateFile API with Snapshot0 from my application. But it is failing. I tried lot of combinations. but no luck. please give your ideas.


    Tuesday, April 30, 2013 9:35 AM

Answers

  • You don't show the DOS_DEVICE_NAME but it needs to be L"\\DosDevices\\Snapshot0"  and you do give the exact call to CreateFile but it needs to be "\\\\.\\Snapshot0

    Also, I assume you have your PnP support in so the device can be started which is normally desirable before trying to open.

    A couple of questions:  Since you create a device interface, why aren't you using it for the open?  The SetupAPI's have a little more work, but they are cleaner and safer in the end.   Also, why are you wasting your time writing a PnP driver in WDM, this is just foolish, it is easier, safer and in general better to be using KMDF.


    Don Burn Windows Filesystem and Driver Consulting Website: http://www.windrvr.com Blog: http://msmvps.com/blogs/WinDrvr

    Tuesday, April 30, 2013 11:19 AM