6-1
/*
* IoByFile.c
*
* Sample code for Multithreading Applications in Win32
* This is from Chapter 6, Listing 6-1
*
* Demonstrates how a file handle becomes signaled
* when an overlapped operation on the handle's file
* is completed. Notice that sometimes the operation
* is completed immediately. The operation is only
* queued, or pended, if ERROR_IO_PENDING is returned.
*/
#define WIN32_LEAN_AND_MEAN
#include <stdio.h>
#include <stdlib.h>
#include <windows.h>
//
// Constants
//
#define READ_SIZE 512
//
// Function prototypes
//
void CheckOsVersion();
int main()
{
BOOL rc;
HANDLE hFile;
DWORD numread;
OVERLAPPED overlap;
char buf[READ_SIZE];
char szPath[MAX_PATH];
CheckOsVersion();
GetWindowsDirectory(szPath, sizeof(szPath));
strcat(szPath, "http://www.cnblogs.com/nanshouyong326/admin/file://WINHLP32.EXE/");
// Open the file for overlapped reads
hFile = CreateFile( szPath,
GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
NULL
);
if (hFile == INVALID_HANDLE_VALUE)
{
printf("Could not open %s\n", szPath);
return -1;
}
// Initialize the OVERLAPPED structure
memset(&overlap, 0, sizeof(overlap));
overlap.Offset = 1500;
// Request the data
rc = ReadFile(
hFile,
buf,
READ_SIZE,
&numread,
&overlap
);
printf("Issued read request\n");
// Was the operation queued?
if (rc)
{
// The data was read successfully
printf("Request was returned immediately\n");
}
else
{
if (GetLastError() == ERROR_IO_PENDING)
{
// We could do something else for awhile here...
printf("Request queued, waiting...\n");
WaitForSingleObject(hFile, INFINITE);
printf("Request completed.\n");
rc = GetOverlappedResult(
hFile,
&overlap,
&numread,
FALSE
);
printf("Result was %d\n", rc);
}
else
{
// We should check for memory and quota
// errors here and retry. See the samples
// IoByEvnt and IoByAPC.
// Something went wrong
printf("Error reading file\n");
}
}
CloseHandle(hFile);
return EXIT_SUCCESS;
}
//
// Make sure we are running under an operating
// system that supports overlapped I/O to files.
//
void CheckOsVersion()
{
OSVERSIONINFO ver;
BOOL bResult;
ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
bResult = GetVersionEx((LPOSVERSIONINFO) &ver);
if ( (!bResult) ||
(ver.dwPlatformId != VER_PLATFORM_WIN32_NT) )
{
fprintf(stderr, "IoByFile must be run under Windows NT.\n");
exit(EXIT_FAILURE);
}
}
od
00401000 /$ 81EC 1C030000 sub esp, 31C
00401006 |. 53 push ebx
00401007 |. 56 push esi
00401008 |. 57 push edi
00401009 |. E8 52010000 call 00401160
0040100E |. 8D4424 24 lea eax, dword ptr [esp+24]
00401012 |. 68 04010000 push 104 ; /BufSize = 104 (260.)
00401017 |. 50 push eax ; |Buffer
00401018 |. FF15 18204000 call dword ptr [<&KERNEL32.GetWindows>; \GetWindowsDirectoryA
0040101E |. BF B4304000 mov edi, 004030B4 ; ASCII "\WINHLP32.EXE"
00401023 |. 83C9 FF or ecx, FFFFFFFF
00401026 |. 33C0 xor eax, eax
00401028 |. 8D5424 24 lea edx, dword ptr [esp+24]
0040102C |. F2:AE repne scas byte ptr es:[edi]
0040102E |. F7D1 not ecx
00401030 |. 2BF9 sub edi, ecx
00401032 |. 50 push eax ; /hTemplateFile => NULL
00401033 |. 8BF7 mov esi, edi ; |
00401035 |. 8BD9 mov ebx, ecx ; |
00401037 |. 8BFA mov edi, edx ; |
00401039 |. 83C9 FF or ecx, FFFFFFFF ; |
0040103C |. F2:AE repne scas byte ptr es:[edi] ; |
0040103E |. 8BCB mov ecx, ebx ; |
00401040 |. 4F dec edi ; |
00401041 |. C1E9 02 shr ecx, 2 ; |
00401044 |. F3:A5 rep movs dword ptr es:[edi], dword p>; |
00401046 |. 68 00000040 push 40000000 ; |Attributes = OVERLAPPED
0040104B |. 8BCB mov ecx, ebx ; |
0040104D |. 6A 03 push 3 ; |Mode = OPEN_EXISTING
0040104F |. 50 push eax ; |pSecurity => NULL
00401050 |. 83E1 03 and ecx, 3 ; |
00401053 |. 6A 03 push 3 ; |ShareMode = FILE_SHARE_READ|FILE_SHARE_WRITE
00401055 |. 8D4424 38 lea eax, dword ptr [esp+38] ; |
00401059 |. 68 00000080 push 80000000 ; |Access = GENERIC_READ
0040105E |. F3:A4 rep movs byte ptr es:[edi], byte ptr>; |
00401060 |. 50 push eax ; |FileName
00401061 |. FF15 14204000 call dword ptr [<&KERNEL32.CreateFile>; \CreateFileA
00401067 |. 8BF0 mov esi, eax
00401069 |. 83FE FF cmp esi, -1
0040106C |. 75 1F jnz short 0040108D
0040106E |. 8D4C24 24 lea ecx, dword ptr [esp+24]
00401072 |. 51 push ecx ; /<%s>
00401073 |. 68 A0304000 push 004030A0 ; |format = "Could not open %s",LF,""
00401078 |. FF15 24204000 call dword ptr [<&MSVCRTD.printf>] ; \printf
0040107E |. 83C4 08 add esp, 8
00401081 |. 0BC6 or eax, esi
00401083 |. 5F pop edi
00401084 |. 5E pop esi
00401085 |. 5B pop ebx
00401086 |. 81C4 1C030000 add esp, 31C
0040108C |. C3 retn
0040108D |> 33D2 xor edx, edx
0040108F |. 8D4424 10 lea eax, dword ptr [esp+10]
00401093 |. 895424 10 mov dword ptr [esp+10], edx
00401097 |. 8D4C24 0C lea ecx, dword ptr [esp+C]
0040109B |. 895424 14 mov dword ptr [esp+14], edx
0040109F |. 50 push eax ; /pOverlapped
004010A0 |. 895424 1C mov dword ptr [esp+1C], edx ; |
004010A4 |. 51 push ecx ; |pBytesRead
004010A5 |. 895424 24 mov dword ptr [esp+24], edx ; |
004010A9 |. 68 00020000 push 200 ; |BytesToRead = 200 (512.)
004010AE |. 895424 2C mov dword ptr [esp+2C], edx ; |
004010B2 |. 8D9424 340100>lea edx, dword ptr [esp+134] ; |
004010B9 |. 52 push edx ; |Buffer
004010BA |. 56 push esi ; |hFile
004010BB |. C74424 2C DC0>mov dword ptr [esp+2C], 5DC ; |
004010C3 |. FF15 10204000 call dword ptr [<&KERNEL32.ReadFile>] ; \ReadFile
004010C9 |. 8B3D 24204000 mov edi, dword ptr [<&MSVCRTD.printf>; MSVCRTD.printf
004010CF |. 68 88304000 push 00403088 ; /format = "Issued read request",LF,""
004010D4 |. 8BD8 mov ebx, eax ; |
004010D6 |. FFD7 call edi ; \printf
004010D8 |. 83C4 04 add esp, 4
004010DB |. 85DB test ebx, ebx
004010DD |. 74 07 je short 004010E6
004010DF |. 68 64304000 push 00403064 ; ASCII "Request was returned immediately",LF
004010E4 |. EB 60 jmp short 00401146
004010E6 |> FF15 0C204000 call dword ptr [<&KERNEL32.GetLastErr>; [GetLastError
004010EC |. 3D E5030000 cmp eax, 3E5
004010F1 |. 75 4E jnz short 00401141
004010F3 |. 68 48304000 push 00403048 ; ASCII "Request queued, waiting...",LF
004010F8 |. FFD7 call edi
004010FA |. 83C4 04 add esp, 4
004010FD |. 6A FF push -1 ; /Timeout = INFINITE
004010FF |. 56 push esi ; |hObject
00401100 |. FF15 08204000 call dword ptr [<&KERNEL32.WaitForSin>; \WaitForSingleObject
00401106 |. 68 34304000 push 00403034 ; ASCII "Request completed.",LF
0040110B |. FFD7 call edi
0040110D |. 83C4 04 add esp, 4
00401110 |. 8D4424 0C lea eax, dword ptr [esp+C]
00401114 |. 8D4C24 10 lea ecx, dword ptr [esp+10]
00401118 |. 6A 00 push 0 ; /Wait = FALSE
0040111A |. 50 push eax ; |pByteCount
0040111B |. 51 push ecx ; |pOverlapped
0040111C |. 56 push esi ; |hFile
0040111D |. FF15 04204000 call dword ptr [<&KERNEL32.GetOverlap>; \GetOverlappedResult
00401123 |. 50 push eax
00401124 |. 68 24304000 push 00403024 ; ASCII "Result was %d",LF
00401129 |. FFD7 call edi
0040112B |. 83C4 08 add esp, 8
0040112E |. 56 push esi ; /hObject
0040112F |. FF15 00204000 call dword ptr [<&KERNEL32.CloseHandl>; \CloseHandle
00401135 |. 5F pop edi
00401136 |. 5E pop esi
00401137 |. 33C0 xor eax, eax
00401139 |. 5B pop ebx
0040113A |. 81C4 1C030000 add esp, 31C
00401140 |. C3 retn
00401141 |> 68 10304000 push 00403010 ; ASCII "Error reading file",LF
00401146 |> FFD7 call edi
00401148 |. 83C4 04 add esp, 4
0040114B |. 56 push esi ; /hObject
0040114C |. FF15 00204000 call dword ptr [<&KERNEL32.CloseHandl>; \CloseHandle
00401152 |. 5F pop edi
00401153 |. 5E pop esi
00401154 |. 33C0 xor eax, eax
00401156 |. 5B pop ebx
00401157 |. 81C4 1C030000 add esp, 31C
0040115D \. C3 retn
ida
.text:00401000
.text:00401000 ; =============== S U B R O U T I N E =======================================
.text:00401000
.text:00401000
.text:00401000 ; int __cdecl main(int argc, const char **argv, const char *envp)
.text:00401000 _main proc near ; CODE XREF: start+FAp
.text:00401000
.text:00401000 NumberOfBytesTransferred= dword ptr -31Ch
.text:00401000 Overlapped = _OVERLAPPED ptr -318h
.text:00401000 FileName = byte ptr -304h
.text:00401000 Buffer = byte ptr -200h
.text:00401000 argc = dword ptr 4
.text:00401000 argv = dword ptr 8
.text:00401000 envp = dword ptr 0Ch
.text:00401000
.text:00401000 sub esp, 31Ch
.text:00401006 push ebx
.text:00401007 push esi
.text:00401008 push edi
.text:00401009 call sub_401160
.text:0040100E lea eax, [esp+328h+FileName]
.text:00401012 push 104h ; uSize
.text:00401017 push eax ; lpBuffer
.text:00401018 call ds:GetWindowsDirectoryA
.text:0040101E mov edi, offset aWinhlp32_exe ; "http://www.cnblogs.com/nanshouyong326/admin/file://WINHLP32.EXE/"
.text:00401023 or ecx, 0FFFFFFFFh
.text:00401026 xor eax, eax
.text:00401028 lea edx, [esp+328h+FileName]
.text:0040102C repne scasb
.text:0040102E not ecx
.text:00401030 sub edi, ecx
.text:00401032 push eax ; hTemplateFile
.text:00401033 mov esi, edi
.text:00401035 mov ebx, ecx
.text:00401037 mov edi, edx
.text:00401039 or ecx, 0FFFFFFFFh
.text:0040103C repne scasb
.text:0040103E mov ecx, ebx
.text:00401040 dec edi
.text:00401041 shr ecx, 2
.text:00401044 rep movsd
.text:00401046 push 40000000h ; dwFlagsAndAttributes
.text:0040104B mov ecx, ebx
.text:0040104D push 3 ; dwCreationDisposition
.text:0040104F push eax ; lpSecurityAttributes
.text:00401050 and ecx, 3
.text:00401053 push 3 ; dwShareMode
.text:00401055 lea eax, [esp+33Ch+FileName]
.text:00401059 push 80000000h ; dwDesiredAccess
.text:0040105E rep movsb
.text:00401060 push eax ; lpFileName
.text:00401061 call ds:CreateFileA
.text:00401067 mov esi, eax
.text:00401069 cmp esi, 0FFFFFFFFh
.text:0040106C jnz short loc_40108D
.text:0040106E lea ecx, [esp+328h+FileName]
.text:00401072 push ecx
.text:00401073 push offset Format ; "Could not open %s\n"
.text:00401078 call ds:printf
.text:0040107E add esp, 8
.text:00401081 or eax, esi
.text:00401083 pop edi
.text:00401084 pop esi
.text:00401085 pop ebx
.text:00401086 add esp, 31Ch
.text:0040108C retn
.text:0040108D ; ---------------------------------------------------------------------------
.text:0040108D
.text:0040108D loc_40108D: ; CODE XREF: _main+6Cj
.text:0040108D xor edx, edx
.text:0040108F lea eax, [esp+328h+Overlapped]
.text:00401093 mov [esp+328h+Overlapped.Internal], edx
.text:00401097 lea ecx, [esp+328h+NumberOfBytesTransferred]
.text:0040109B mov [esp+328h+Overlapped.InternalHigh], edx
.text:0040109F push eax ; lpOverlapped
.text:004010A0 mov dword ptr [esp+32Ch+Overlapped.anonymous_0], edx
.text:004010A4 push ecx ; lpNumberOfBytesRead
.text:004010A5 mov dword ptr [esp+330h+Overlapped.anonymous_0+4], edx
.text:004010A9 push 200h ; nNumberOfBytesToRead
.text:004010AE mov [esp+334h+Overlapped.hEvent], edx
.text:004010B2 lea edx, [esp+334h+Buffer]
.text:004010B9 push edx ; lpBuffer
.text:004010BA push esi ; hFile
.text:004010BB mov dword ptr [esp+33Ch+Overlapped.anonymous_0], 5DCh
.text:004010C3 call ds:ReadFile
.text:004010C9 mov edi, ds:printf
.text:004010CF push offset aIssuedReadRequ ; "Issued read request\n"
.text:004010D4 mov ebx, eax
.text:004010D6 call edi ; printf
.text:004010D8 add esp, 4
.text:004010DB test ebx, ebx
.text:004010DD jz short loc_4010E6
.text:004010DF push offset aRequestWasRetu ; "Request was returned immediately\n"
.text:004010E4 jmp short loc_401146
.text:004010E6 ; ---------------------------------------------------------------------------
.text:004010E6
.text:004010E6 loc_4010E6: ; CODE XREF: _main+DDj
.text:004010E6 call ds:GetLastError
.text:004010EC cmp eax, ERROR_IO_PENDING
.text:004010F1 jnz short loc_401141
.text:004010F3 push offset aRequestQueuedW ; "Request queued, waiting...\n"
.text:004010F8 call edi ; printf
.text:004010FA add esp, 4
.text:004010FD push 0FFFFFFFFh ; dwMilliseconds
.text:004010FF push esi ; hHandle
.text:00401100 call ds:WaitForSingleObject
.text:00401106 push offset aRequestComplet ; "Request completed.\n"
.text:0040110B call edi ; printf
.text:0040110D add esp, 4
.text:00401110 lea eax, [esp+328h+NumberOfBytesTransferred]
.text:00401114 lea ecx, [esp+328h+Overlapped]
.text:00401118 push 0 ; bWait
.text:0040111A push eax ; lpNumberOfBytesTransferred
.text:0040111B push ecx ; lpOverlapped
.text:0040111C push esi ; hFile
.text:0040111D call ds:GetOverlappedResult
.text:00401123 push eax
.text:00401124 push offset aResultWasD ; "Result was %d\n"
.text:00401129 call edi ; printf
.text:0040112B add esp, 8
.text:0040112E push esi ; hObject
.text:0040112F call ds:CloseHandle
.text:00401135 pop edi
.text:00401136 pop esi
.text:00401137 xor eax, eax
.text:00401139 pop ebx
.text:0040113A add esp, 31Ch
.text:00401140 retn
.text:00401141 ; ---------------------------------------------------------------------------
.text:00401141
.text:00401141 loc_401141: ; CODE XREF: _main+F1j
.text:00401141 push offset aErrorReadingFi ; "Error reading file\n"
.text:00401146
.text:00401146 loc_401146: ; CODE XREF: _main+E4j
.text:00401146 call edi ; printf
.text:00401148 add esp, 4
.text:0040114B push esi ; hObject
.text:0040114C call ds:CloseHandle
.text:00401152 pop edi
.text:00401153 pop esi
.text:00401154 xor eax, eax
.text:00401156 pop ebx
.text:00401157 add esp, 31Ch
.text:0040115D retn
.text:0040115D _main endp
.text:0040115D
.text:0040115D ; ---------------------------------------------------------------------------
.text:0040115E align 10h
.text:00401160