I have seen a lot of articles on the Internet that have completed port and network communication. The following is a summary, which is rarely mentioned. I have read the msdn document carefully. please correct me.
- To perform asynchronous file IO operations, you must specify the file_flag_overlapped attribute during file creation;
- Asynchronous readfileex cannot read the file handle bound to the I/O completion port;
- Asynchronous readfileex ignores the hevent of overlapped;
- After Asynchronization, You can initiate a callback. The callback interface must specify the winapi attribute, which is actually _ stdcall. If this parameter is not specified, the default value is _ cdecl. After the callback is complete, the callback will crash;
- Wait.
A piece of asynchronous code is attached here:
#include <process.h>#include <Windows.h>VOID WINAPI rt( __in DWORD dwErrorCode, __in DWORD dwNumberOfBytesTransfered, __inout LPOVERLAPPED lpOverlapped){ printf("rt call back\n");}int _tmain(int argc, _TCHAR* argv[]){ OVERLAPPED *ol = new OVERLAPPED; memset(ol, 0, sizeof *ol); HANDLE file = CreateFile("ReadMe.txt", GENERIC_READ, 0, NULL, OPEN_EXISTING , FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED , NULL); if(file == INVALID_HANDLE_VALUE) { goto mem_free1; } size_t buf_len = 1024; char *buf = new char[buf_len]; if(!buf) { goto mem_free1; } if(!ReadFileEx(file, buf, buf_len, ol, rt)) { goto mem_free; } SleepEx(INFINITE, TRUE);mem_free: delete []buf;mem_free1: delete ol; CloseHandle(file);return 0;}
For readfile, after the main thread triggers an asynchronous operation, another thread is required for auxiliary work. You can use Event Events. Here we mainly introduce how to complete the port, also paste the Code:
# Include <process. h> # include <windows. h> // create an IO completion port handle createnewcompletionport (DWORD dwnumberofconcurrentthreads) {return (createiocompletionport (invalid_handle_value, null, null, dwnumberofconcurrentthreads ));} // manage the device and the completed port. bool associatedevicewithcompletionport (handle hcompletionport, handle hdevice, DWORD dwcompletionkey) {handle H = createiocompletionport (hdevice, hcompletionport, dwcomple Tionkey, 0); Return (H = hcompletionport);} void reader (void * In) {handle Port = (handle) in; DWORD rcv_len = 0; ulong_ptr my_key = 0; overlapped * ol1 = NULL; while (1) {If (! Getqueuedcompletionstatus (port, & rcv_len, & my_key, & ol1, infinite) {int err = getlasterror (); continue;} else {_ endthread ();}}} int _ tmain (INT argc, _ tchar * argv []) {int key = 12345; handle Port = createnewcompletionport (4); If (! Port) {int err = getlasterror (); Return 0;} handle file = createfile ("readme.txt", generic_read, 0, null, open_existing, file_attribute_normal | file_flag_overlapped, null ); if (file = invalid_handle_value) {closehandle (port);} overlapped * OL = new overlapped; memset (OL, 0, sizeof * ol); If (! Associatedevicewithcompletionport (port, file, key) {int err = getlasterror (); closehandle (port); closehandle (File); Delete ol; return 0;} int buf_len = 1000; char * Buf = new char [buf_len]; If (! Buf) {closehandle (port); closehandle (File); Delete ol; return 0;} Handle h_thread = (handle) _ beginthread (reader, 0, Port); If (! Readfile (file, Buf, buf_len, null, Ol) {int err = getlasterror (); If (Err! = Error_io_pending) {closehandle (port); closehandle (File); Delete ol; waitforsingleobject (h_thread, infinite); Return 0 ;}} waitforsingleobject (h_thread, infinite ); closehandle (port); closehandle (File); Delete ol; Delete [] Buf; return 0 ;}