從八月初開始幹了一系列的事情,
a. 研究 Intel media SDK
b. 研究 ffmpeg/ 使用ffmpeg去做一些decode/encode,blablabla
c. 研究 Display相關的
d. 協助debug別人的程式
反正這一系列事情弄了我大概兩個半月,
等到一切都結束的時候...發現自己已經忘了怎麼寫driver了Q_Q
所以乾脆就從最基礎的先搞起吧
這邊分享兩段程式,
也當作是自己的一個筆記,
1.[ app ]:
使用console app 去觀察 driver在處理create file和close handle會發生甚麼事情.
2.[ driver]:
1. 創建設備
2. 創建符號
3. 符號連接設備
4. 給設備和符號名字
5. 操作他們
6. IRP
7. Create/Close/Clean
註記:
app下closehandle的時候,driver的動作是先 Cleanup->close
APP: APP部分沒啥好注意的, 就是正常寫
//
#include <iostream>
#include <windows.h>
int main()
{
std::cout << "Hello World!\n";
HANDLE hDevice = NULL;
hDevice = CreateFile
(
L"\\\\.\\MyFirstDevice",
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL
);
if (hDevice==INVALID_HANDLE_VALUE)
{
printf("Create File Failed :%d \n", GetLastError());
goto DONE;
}
else
{
printf("Create File Success\n");
}
system("pause");
printf("Do close handle\n");
system("pause");
CloseHandle(hDevice);
DONE:
system("pause");
return 0;
}
Driver: 這裡的話會遇到幾個問題
1. 你會出現緊告錯誤, 這時候別擔心, Project->R-Click->Properties->C/C++->Warning Level = Level3(/W3) & Treat Warnings As Errors = No(/WS-)
※把這個緊告關掉就好,這個錯誤是我們沒有使用到PDriver object, 但這只是個Sample而已, 不要太在意這些.
#include <ntddk.h>
#define DEVICE_NAME L"\\Device\\MyFirstDevice"
#define SYM_NAME L"\\??\\MyFirstDevice"
VOID nothing(HANDLE ppid, HANDLE mypid, BOOLEAN bcreate)
{
DbgPrint("---ppid:%x, mypid:%x, bool:%d \n ", ppid, mypid, bcreate);
}
VOID DrvUnload(PDRIVER_OBJECT pdriver)
{
DbgPrint("Unload\n");
//我們有新增這個設備,所以要刪除這個設備
if (pdriver->DeviceObject)
{
// 刪除設備
IoDeleteDevice(pdriver->DeviceObject);
// 刪除符號連接
UNICODE_STRING symname = { 0 };
RtlInitUnicodeString(&symname, SYM_NAME);
IoDeleteSymbolicLink(&symname);
}
}
NTSTATUS myCreate(PDEVICE_OBJECT pdriver, PIRP pirp)
{
NTSTATUS status = STATUS_SUCCESS;
DbgPrint("My Device has be opened\n");
pirp->IoStatus.Status = status;
pirp->IoStatus.Information = 0;//0字節
IoCompleteRequest(pirp, IO_NO_INCREMENT);
return status;
}
NTSTATUS myClose(PDEVICE_OBJECT pdriver, PIRP pirp)
{
NTSTATUS status = STATUS_SUCCESS;
DbgPrint("My Device has be myClose\n");
pirp->IoStatus.Status = status;
pirp->IoStatus.Information = 0;//0字節
IoCompleteRequest(pirp, IO_NO_INCREMENT);
return status;
}
NTSTATUS myCleanUp(PDEVICE_OBJECT pdriver, PIRP pirp)
{
NTSTATUS status = STATUS_SUCCESS;
DbgPrint("My Device has be myCleanUp\n");
pirp->IoStatus.Status = status;
pirp->IoStatus.Information = 0;//0字節
IoCompleteRequest(pirp, IO_NO_INCREMENT);
return status;
}
NTSTATUS DriverEntry(PDRIVER_OBJECT driver, PUNICODE_STRING reg_path)
{
NTSTATUS status = STATUS_SUCCESS;
UNICODE_STRING devicename = { 0 };
driver->DriverUnload = DrvUnload;
PDEVICE_OBJECT pDevice = NULL;
RtlInitUnicodeString(&devicename, DEVICE_NAME);
status = IoCreateDevice(driver, 0, &devicename, FILE_DEVICE_UNKNOWN, 0, TRUE, &pDevice);
if (!NT_SUCCESS(status))
{
DbgPrint("Create Device Failed:%x\n", status);
return status;
}
// if success , create symbo link
UNICODE_STRING sysname = { 0 };
RtlInitUnicodeString(&sysname, SYM_NAME);
status = IoCreateSymbolicLink(&sysname, &devicename);
if (!NT_SUCCESS(status))
{
DbgPrint("Create IoCreateSymbolicLink failed:%x\n", status);
IoDeleteDevice(pDevice);
return status;
}
DbgPrint("Create SymbolicLink success\n");
driver->MajorFunction[IRP_MJ_CREATE] = myCreate;
driver->MajorFunction[IRP_MJ_CLOSE] = myClose;
driver->MajorFunction[IRP_MJ_CLEANUP] = myCleanUp;
return 0;
}
最好, 我想說的是....
我應該找時間將檔案放到github裡面去了,
不過從過去到現在真的寫了太多程式, 要分類真的是一個大工程,
等後續有一點時間後, 我在開始著手操作吧.
以上! 結束 :D
留言列表