Windows API 之 CreateFile

Creates or opens a file or I/O device. The most commonly used I/O devices are as follows: file, file stream, directory, physical disk, volume, console buffer, tape drive, communications resource, mailslot, and pipe. The function returns a handle that can be used to access the file or device for various types of I/O depending on the file or device and the flags and attributes specified.

函数原型如下:

HANDLE WINAPI CreateFile(
  _In_     LPCTSTR               lpFileName,
  _In_     DWORD                 dwDesiredAccess,
  _In_     DWORD                 dwShareMode,
  _In_opt_ LPSECURITY_ATTRIBUTES lpSecurityAttributes,
  _In_     DWORD                 dwCreationDisposition,
  _In_     DWORD                 dwFlagsAndAttributes,
  _In_opt_ HANDLE                hTemplateFile
);

Return value

If the function succeeds, the return value is an open handle to the specified file, device, named pipe, or mail slot.

If the function fails, the return value is INVALID_HANDLE_VALUE. To get extended error information, call GetLastError.

Remarks

CreateFile was originally developed specifically for file interaction but has since been expanded and enhanced to include most other types of I/O devices and mechanisms available to Windows developers. This section attempts to cover the varied issues developers may experience when using CreateFile in different contexts and with different I/O types. The text attempts to use the word file only when referring specifically to data stored in an actual file on a file system. However, some uses of file may be referring more generally to an I/O object that supports file-like mechanisms. This liberal use of the term file is particularly prevalent in constant names and parameter names because of the previously mentioned historical reasons.

When an application is finished using the object handle returned by CreateFile, use the CloseHandle function to close the handle. This not only frees up system resources, but can have wider influence on things like sharing the file or device and committing data to disk. Specifics are noted within this topic as appropriate.

CreateFile是创建一个File的内核对象,而不是创建物理磁盘上的“文件”。

这里提出一个问题,为什么要使用CloseHandle关闭句柄

句柄是一种内核对象,系统维护着每一个内核对象,当每个内核对象引用记数为0时,系统就从内存中释放该对象,CloseHandle就是将该线程对象的引 用记数减1。所有的内核对象(包括线程Handle)都是系统资源,用了要还的,也就是说用完后一定要closehandle关闭之,如果不这么做,你系 统的句柄资源很快就用光了。CreateFile创建了一个内核对象,你如果不调用CloseHandle去关闭句柄,那它是无法自己释放的,所以在调用了CreateFile函数后,一定要记得调用CloseHandle

进一步在问,何谓内核对象

内核对象的数据结构只能被内核访问,因此应用程序无法在内存中找到这些数据结构并直接改变它们的内容当调用一个用于创建内核对象的函数时,该函数就返回一个用于标识该对象的句柄。如果将该句柄值传递给另一个进程中的一个线程,那么在另一个进程使用你的进程的句柄值所作的调用就会失败。如果想在多个进程中共享内核对象,要通过一定的机制。如对象句柄的继承性,命名对象,复制对象句柄。若要确定一个对象是否属于内核对象,最容易的方法是观察创建该对象所用的函数。
所以:

作为一个Windows软件开发人员,你经常需要创建、打开和操作各种内核对象。系统要创建和操作若干类型的内核对象,比如存取符号对象、事件对象、文件对象、文件映射对象、I/O完成端口对象、作业对象、信箱对象、互斥对象、管道对象、进程对象、信标对象、线程对象和等待计时器对象等。这些对象都是通过调用函数来创建的。内核对象在应用程序中只能通过Windows的一组函数Create*/Open*/CloseHandle以及相应的内核对象句柄(*Handle)来访问,内核对象句柄(32Windows进程中句柄为32位,64位进程中,句柄也变成64位)是一个不透明值,其具体意义也可能与Windows不同版本的具体实现相关。例如,CreateFileMapping函数可使系统能够创建一个文件映射对象。每个内核对象只是内核分配的一个内存块,并且只能由该内核访问。该内存块是一种数据结构,它的成员负责维护该对象的各种信息。有些数据成员(如安全性描述符、使用计数等)在所有对象类型中是相同的,但大多数数据成员属于特定的对象类型。例如,进程对象有一个进程ID、一个基本优先级和一个退出代码,而文件对象则拥有一个字节位移、一个共享模式和一个打开模式。

使用计数

内核对象的所有者是内核,而非进程,而且进程操作内核对象的唯一手段就是用句柄作为参数来调用相应的Windows APICreate*/Open*/CloseHandle)。Windows操作系统可由此来实现对内核对象的使用计数,就是说同一个内核对象,Windows操作系统知道有多少进程在引用它。只有当引用计数变为0的时候,内核对象才被销毁,因此,一个内核对象的生命周期有可能比创建它的进程生命周期要长。

进程的内核对象句柄

进程的内核对象句柄都包含了进程句柄表的索引值,而进程的句柄表是在进程初始化的时候系统为其分配的;实际上将句柄值除以4(或者右移2位,以忽略Windows内部使用的最后两位)就能得到实际的句柄表索引值。

每个句柄包含一个索引值,能够只想该表中的一条记录,而该句柄表中每一条记录都是一个数据结构,这个数据结构包含了:

 

参考:

MSDN:https://msdn.microsoft.com/en-us/library/aa363858%28VS.85%29.aspx

http://www.cnblogs.com/wind-net/archive/2012/11/09/2762667.html

内核对象:http://www.cnblogs.com/Code-life/archive/2012/06/10/2543872.html

http://www.cnblogs.com/dlbrant/archive/2013/05/27/3102824.html

 

posted @ 2015-10-22 17:57  _No.47  阅读(2621)  评论(0编辑  收藏  举报