![](/img/trans.png)
[英]'Access is denied' error when calling CreateFile from a UMDF driver (C++)
[英]Calling WDF driver from c++
我一直在嘗試給示例驅動程序打電話。 我已經編寫了DriverEntry方法,在其中初始化了驅動程序名稱和指向該驅動程序的符號ling。
// UNICODE_STRING DriverName, SymbolName; // Driver registry paths
...
// Driver Entrypoint
NTSTATUS
DriverEntry(PDRIVER_OBJECT pDriverObject, PUNICODE_STRING pRegistryPath) {
Q_UNUSED(pRegistryPath);
DbgPrintEx(0, 0, "Driver Loaded\n");
// The PsSetLoadImageNotifyRoutine routine registers a driver-supplied
// callback that is subsequently notified whenever
// an image is loaded (or mapped into memory).
PsSetLoadImageNotifyRoutine(ImageLoadCallback);
// initialize driver name
RtlInitUnicodeString(&DriverName, L"\\Device\\Explorer");
// initialize symbolic link
RtlInitUnicodeString(&SymbolName, L"\\DosDevices\\Explorer");
IoCreateDevice(pDriverObject, 0, &SymbolName, FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN, FALSE, &pDeviceObject);
IoCreateSymbolicLink(&DriverName, &SymbolName);
pDriverObject->MajorFunction[IRP_MJ_CREATE] = CreateCall;
pDriverObject->MajorFunction[IRP_MJ_CLOSE] = CloseCall;
pDriverObject->MajorFunction[IRP_MJ_DEVICE_CONTROL] = IoControl;
pDriverObject->DriverUnload = UnloadDriver;
pDeviceObject->Flags |= DO_DIRECT_IO;
pDeviceObject->Flags &= ~DO_DEVICE_INITIALIZING;
return STATUS_SUCCESS;
}
當我加載驅動程序時(使用OSR Driver Loader,也可以使用cmd,通過將驅動程序注冊為新服務來完成),我在DebugView中得到了預期的輸出(sysinternals工具允許查看內核調試日志)
現在,我需要確保Windows對象目錄中同時存在設備和符號鏈接。 為此,我使用WinObj(sysinternals的另一個工具),這是輸出
讓我感到困惑的是,符號鏈接位於Device文件夾中,而不是GLOBAL? 。 設備中的符號鏈接
GLOBAL中的設備?
現在,最后,調用驅動程序本身。 我為此使用c ++,這是我的代碼,
class Test
{
public:
HANDLE hDriver; // Handle to driver
// Initializer
Test::Test(LPCSTR RegistryPath)
{
LPCSTR path = "\\\\.\\Explorer";
hDriver = CreateFileA(path, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);
if (hDriver == INVALID_HANDLE_VALUE)
{
// Handle the error.
char result = GetLastError();
bool zadek = false;
}
}
問題是我無法獲得驅動程序的有效句柄。 無論我使用的路徑如何,hDriver的值始終為0x00000000000000a0或0xffffffff。 我使用createFileA是因為我想訪問系統內存。
我犯了一些公然的錯誤嗎?
我應該說,自上次編寫設備驅動程序以來已經有8-9年的時間了,但是我想到的是:
hDriver
得到了0xa0
,這是一個有效的句柄值。 IRP_MJ_DEVICE_CONTROL
回調。 L"\\\\??\\\\Explorer"
或L"\\\\GLOBAL??\\\\Explorer"
進行符號鏈接。 IoCreateDevice
使用DriverName
。 IoCreateSymbolicLink
。 因此您的代碼應如下所示:
...
// initialize driver name
RtlInitUnicodeString(&DriverName, L"\\Device\\Explorer");
// initialize symbolic link
RtlInitUnicodeString(&SymbolName, L"\\??\\Explorer");
IoCreateDevice(pDriverObject, 0, &DriverName, FILE_DEVICE_UNKNOWN,
FILE_DEVICE_SECURE_OPEN, FALSE, &pDeviceObject);
IoCreateSymbolicLink(&SymbolName, &DriverName);
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.