驅動程式檔案操作

2021-10-20 15:27:11 字數 3499 閱讀 7471

#ifdef _cplusplus

#endif

// 建立檔案函式 1.檔案路徑字串

ntstatus createfile

(const pcwstr filename)

;// 定義乙個控制代碼

object_attributes attr;

// 通過建立物件和/或返回控制代碼物件例程被應用到物件或物件控制代碼結構指定的屬性。

unicode_string file;

// unicode字串

rtlinitunicodestring

(&file, filename)

;// 初始化unicode字串

// 初始化 object_attributes 結構

initializeobjectattributes

(&attr,

&file,

obj_kernel_handle | obj_case_insensitive,

// 不區分大小寫

null

,// 設定為null

null);

// 設定為null

io_status_block block;

// io狀態塊

ntstatus status =

zwcreatefile

(&hfile,

// 檔案控制代碼

generic_read | generic_write,

// 讀 / 寫

&attr,

// 指定的物件屬性結構

&block,

// io狀態塊

null

,// 初始大小

file_attribute_normal,

// 該檔案未設定其他屬性。僅當單獨使用時,此屬性才有效

file_share_read,

// 檔案讀共享

file_open_if,

// 開啟檔案

file_non_directory_file |

// 該檔案不是目錄

file_random_access |

// 對檔案的訪問可以是隨機的

file_synchronous_io_nonalert,

// 對檔案的操作都是同步的

null

,// 設定為null0)

;// 設定為null

// 判斷檔案是否成功建立if(

!nt_success

(status)

)else

// 如果成功建立則關閉控制代碼

zwclose

(hfile)

;return status;

}// 開啟檔案

ntstatus openfile

(const pcwstr filepath)

wrirefile

(filehandle)

;readfile

(filehandle)

;// 讀取檔案內容

/* if (nt_success(wrirefile(filehandle)))

else

*/zwclose

(filehandle)

;// 關閉檔案

return status;

}// 檔案寫入

ntstatus wrirefile

(handle hfile)

return status_unsuccessful;

}// 檔案讀取

ntstatus readfile

(handle hfile)

return status_success;

}

#ifdef _cplusplus

#endif

// 建立檔案

extern

"c" ntstatus createfile

(const pcwstr filename)

;// 開啟檔案

ntstatus openfile

(const pcwstr filepath)

;// 檔案寫入

ntstatus wrirefile

(handle hfile)

;// 檔案讀取

ntstatus readfile

(handle hfile)

;

#ifdef _cplusplus

#endif

extern

"c" ntstatus createmydevice

(in pdriver_object pdriverobject)

;void

ddk_unload

(in pdriver_object driverobject)

;#define pagedcode code_seg("page")

#pragma pagedcode

//驅動程式函式入口

extern

"c" ntstatus driverentry

(pdriver_object pdriverobject, punicode_string punicodestring)

void

ddk_unload

(in pdriver_object driverobject)

#define initcode code_seg("init")

#pragma initcode

ntstatus createmydevice

(in pdriver_object pdriverobject)

if(status == status_object_name_exists)

if(status == status_object_name_collision)

kdprint((

"建立裝置失敗"))

;return status;

}kdprint((

"建立裝置成功--pndgmcsy"))

;//設定讀寫方式,這裡為緩衝區讀寫方式

pdevobj-

>flags |

= do_buffered_io;

//建立符號鏈結

unicode_string symlinkname;

rtlinitunicodestring

(&symlinkname, l"\\??\\myddk_linkname");

status =

iocreatesymboliclink

(&symlinkname,

&devname)

;//如果建立符號鏈結失敗,則刪除裝置if(

!nt_success

(status)

)kdprint((

"建立符號鏈結成功"))

;return status_success;

}

驅動程式中檔案操作函式之ZwCreateFile

據說windows是用80 的c和20 的彙編寫成的,那麼在驅動程式的ddk提供給程式設計師使用的核心函式就都包含了c的特性,比如乙個函式的使用會包含多個結構體,所以在使用起來沒有在使用者模式下這麼簡單。為了使用zwcreatefile這個核心函式,我們首先需要作一些準備工作,還是先來看看zwcre...

linux 驅動程式 高階字元驅動程式

ioctl方法 驅動程式的原型實現 int ioctl struct inode inode,struct file filp,unsigned int cmd,unsigned long arg ioctl 命令選擇 位段結構 number direction ioc read ioc write...

linux裝置驅動程式 字元裝置驅動程式

先留個 有一起學習驅動程式的加qq295699450 字元裝置驅動 這篇比較惱火。載入成功,但是讀不出來資料,有知道怎麼回事的,留個言,一起討論下 資料結構 struct scull mem struct scull dev dev 整個驅動程式 如下 include include include...