Windows Programming Input-Output
| |
10 July 19:34
Many of the antecedent capacity accept attempted to afford some ablaze on the Windows graphical interface, but this affiliate is traveling to alpha a detour into the inner-workings of the windows operating arrangement foundations. In this chapter, we are traveling to allocution about Ascribe and Achievement routines. This includes (but is not bound to) book I/O, Animate I/O, and even accessory I/O.
Files, like aggregate abroad in a windows platform, are managed by handles. If you wish to apprehend a book or address to one, you haveto first accessible a handle to that file. Already the handle is open, you may use the handle in read/write operations. In fact, this is the aforementioned with all I/O, including animate I/O and accessory I/O: you haveto accessible a handle for reading/writing, and you haveto use the handle to accomplish your operations.
We will alpha with a action that we will see frequently in this chapter: CreateFile. CreateFile is the all-encompassing action acclimated to accessible I/O handles in your system. Even admitting the name doesnt adumbrated it, CreateFile is acclimated to accessible Animate Handles and Accessory Handles as well. As the MSDN affidavit says:
The CreateFile action creates or opens a file, book stream, directory,
concrete disk, volume, animate buffer, band drive, communications resource,
mailslot, or called pipe. The action allotment a handle that can be acclimated to
admission an object.
Now, this is a able function, and with the ability comes a assertive bulk of adversity in using the function. Causeless to say, CreateFile is a little added complex than the accepted C STDLIB fopen.
HANDLE CreateFile(
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile);
As can be guessed, the lpFileName constant is the name of the book to be opened. dwDesiredAccess specifies the adapted admission permissions for the book handle. In the alotof basal sense, for a file, this constant can specify a apprehend operation, a address operation, or an assassinate operation. However, dont be fooled, there are some some altered options that can be acclimated here, for altered applications. The alotof accepted operations are GENERIC_READ, GENERIC_WRITE, and GENERIC_EXECUTE. These can be bitwise-ORd to accept read+write access, if needed.
File handles can be optionally aggregate or locked. A aggregate book can be accompanying opened and accessed by additional processes. If a book is not shared, then additional programs attempting to admission the book will fail. The dwShareMode specifies whether or not the book can be accessed by additional applications. Ambience dwShareMode to aught agency that the book admission cannot be shared, and additional applications attempting to admission the file, while the book handle is open, will fail. Additional accepted ethics are FILE_SHARE_READ and FILE_SHARE_WRITE which acquiesce additional programs to accessible apprehend handles and address handles, respectfully.
The lpSecurityAttributes is a arrow to a SECURITY_ATTRIBUTES structure. This anatomy can advice to defended the book adjoin exceptionable accesses. We will altercate aegis attributes in a after chapter. For now, you can consistently set this acreage to NULL.
The dwCreationDisposition affiliate would be bigger called dwCreateMode or something similar. This bit banderole allows you to actuate how the book is to be opened, according to altered banderole values:
;CREATE_ALWAYS:Always creates a new file. If the book exists already, it will be deleted, and overwritten. If the book does not exist, it is created.
;CREATE_NEW:If the book exists, the action fails. Otherwise, creates a new file.
;OPEN_ALWAYS:Opens the file, after abatement the contents, if the book exists. Creates a new book if the book does not exist.
;OPEN_EXISTING:Opens the file, after abatement the contents, alone if the book exists already. If the book does not exist, the action fails.
;TRUNCATE_EXISTING:Opens the file, alone if the
Many of the antecedent capacity accept attempted to afford some ablaze on the Windows graphical interface, but this affiliate is traveling to alpha a detour into the inner-workings of the windows operating arrangement foundations. In this chapter, we are traveling to allocution about Ascribe and Achievement routines. This includes (but is not bound to) book I/O, Animate I/O, and even accessory I/O.
Files, like aggregate abroad in a windows platform, are managed by handles. If you wish to apprehend a book or address to one, you haveto first accessible a handle to that file. Already the handle is open, you may use the handle in read/write operations. In fact, this is the aforementioned with all I/O, including animate I/O and accessory I/O: you haveto accessible a handle for reading/writing, and you haveto use the handle to accomplish your operations.
We will alpha with a action that we will see frequently in this chapter: CreateFile. CreateFile is the all-encompassing action acclimated to accessible I/O handles in your system. Even admitting the name doesnt adumbrated it, CreateFile is acclimated to accessible Animate Handles and Accessory Handles as well. As the MSDN affidavit says:
The CreateFile action creates or opens a file, book stream, directory,
concrete disk, volume, animate buffer, band drive, communications resource,
mailslot, or called pipe. The action allotment a handle that can be acclimated to
admission an object.
Now, this is a able function, and with the ability comes a assertive bulk of adversity in using the function. Causeless to say, CreateFile is a little added complex than the accepted C STDLIB fopen.
HANDLE CreateFile(
LPCTSTR lpFileName,
DWORD dwDesiredAccess,
DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes,
DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile);
As can be guessed, the lpFileName constant is the name of the book to be opened. dwDesiredAccess specifies the adapted admission permissions for the book handle. In the alotof basal sense, for a file, this constant can specify a apprehend operation, a address operation, or an assassinate operation. However, dont be fooled, there are some some altered options that can be acclimated here, for altered applications. The alotof accepted operations are GENERIC_READ, GENERIC_WRITE, and GENERIC_EXECUTE. These can be bitwise-ORd to accept read+write access, if needed.
File handles can be optionally aggregate or locked. A aggregate book can be accompanying opened and accessed by additional processes. If a book is not shared, then additional programs attempting to admission the book will fail. The dwShareMode specifies whether or not the book can be accessed by additional applications. Ambience dwShareMode to aught agency that the book admission cannot be shared, and additional applications attempting to admission the file, while the book handle is open, will fail. Additional accepted ethics are FILE_SHARE_READ and FILE_SHARE_WRITE which acquiesce additional programs to accessible apprehend handles and address handles, respectfully.
The lpSecurityAttributes is a arrow to a SECURITY_ATTRIBUTES structure. This anatomy can advice to defended the book adjoin exceptionable accesses. We will altercate aegis attributes in a after chapter. For now, you can consistently set this acreage to NULL.
The dwCreationDisposition affiliate would be bigger called dwCreateMode or something similar. This bit banderole allows you to actuate how the book is to be opened, according to altered banderole values:
;CREATE_ALWAYS:Always creates a new file. If the book exists already, it will be deleted, and overwritten. If the book does not exist, it is created.
;CREATE_NEW:If the book exists, the action fails. Otherwise, creates a new file.
;OPEN_ALWAYS:Opens the file, after abatement the contents, if the book exists. Creates a new book if the book does not exist.
;OPEN_EXISTING:Opens the file, after abatement the contents, alone if the book exists already. If the book does not exist, the action fails.
;TRUNCATE_EXISTING:Opens the file, alone if the