[so] WriteFile x-(
Mihai Iancu
so@atlantis.cs.pub.ro
Sat, 6 Dec 2003 03:43:43 -0800 (PST)
--0-1010843226-1070711023=:73637
Content-Type: multipart/alternative; boundary="0-807777371-1070711023=:73637"
--0-807777371-1070711023=:73637
Content-Type: text/plain; charset=us-ascii
Nu reusesc sa ma prind cum merge WriteFile cu OVERLAPPED.
In MSDN scrie
<quote>
If hFile was opened with FILE_FLAG_OVERLAPPED and lpOverlapped is not NULL, the write operation starts at the offset specified in the OVERLAPPED structure and WriteFile may return before the write operation has been completed. In this case, WriteFile returns FALSE and the GetLastError function returns ERROR_IO_PENDING. This allows the calling process to continue processing while the write operation is being completed. The event specified in the OVERLAPPED structure is set to the signaled state upon completion of the write operation.
</quote>
Am incercat sa scriu intr-un fisier 10Mb si apoi 30 Mb totul se intampla instant si deci am crezut ca este ok sa intoarca TRUE writefile.
Apoi am scris 100Mb si dureaza cam 3 secunde, dar functia WriteFile nu intoarce FALSE imediat, ci asteapta sa faca scrierea. Ce este ciudat este ca Eventul din OVERLAPPED este semnazat deci nu cred ca am gresit in totalitate apelurile de functii.
codul este atasat
---------------------------------
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
--0-807777371-1070711023=:73637
Content-Type: text/html; charset=us-ascii
<DIV>
<P>Nu reusesc sa ma prind cum merge WriteFile cu OVERLAPPED.</P>
<P>In MSDN scrie </P>
<P><quote></P>
<P>If <I>hFile</I> was opened with FILE_FLAG_OVERLAPPED and <I>lpOverlapped</I> is not NULL, the write operation starts at the offset specified in the <B>OVERLAPPED</B> structure and <B>WriteFile</B> <STRONG><U>may return</U></STRONG> before the write operation has been completed. In this case, <B>WriteFile</B> returns FALSE and the <B>GetLastError</B> function returns ERROR_IO_PENDING. This allows the calling process to continue processing while the write operation is being completed. The event specified in the <B>OVERLAPPED</B> structure is set to the signaled state upon completion of the write operation. </P>
<P></quote></P>
<P> </P>
<P>Am incercat sa scriu intr-un fisier 10Mb si apoi 30 Mb totul se intampla instant si deci am crezut ca este ok sa intoarca TRUE writefile.</P>
<P>Apoi am scris 100Mb si dureaza cam 3 secunde, dar functia WriteFile nu intoarce FALSE imediat, ci asteapta sa faca scrierea. Ce este ciudat este ca Eventul din OVERLAPPED este semnazat deci nu cred ca am gresit in totalitate apelurile de functii.</P>
<P><STRONG>codul este atasat </STRONG></P></DIV><p><hr SIZE=1>
Do you Yahoo!?<br>
<a href="http://antispam.yahoo.com/whatsnewfree">Protect your identity with Yahoo! Mail AddressGuard</a>
--0-807777371-1070711023=:73637--
--0-1010843226-1070711023=:73637
Content-Type: text/plain; name="wfx_test.cpp"
Content-Description: wfx_test.cpp
Content-Disposition: inline; filename="wfx_test.cpp"
#include <stdio.h>
#include <windows.h>
/*
HELPS FOR FINDING THE ERRORS RETURNED BY THE FUNCTIONS
*/
void PostError(const char* msg, DWORD dwErr, bool bTerminate){
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
dwErr,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL);
printf("%s: %s\n", msg, (LPCSTR)lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
if (bTerminate)
ExitProcess(3);
}
/*
MAIN
*/
int main(){
//declare
char *lpBuffer = (char*) HeapAlloc(GetProcessHeap(),0,100*1024*1024);
DWORD dwBytesToWrite=100*1024*1024;
DWORD dwBytesWritten;
BOOL bResult;
OVERLAPPED ovrLpd1;
HANDLE hEvent;
//create event
hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (hEvent == INVALID_HANDLE_VALUE){
printf("error CreateEvent\n");
ExitProcess(2);
}
//create the overlapped structure
ovrLpd1.Offset = 0;
ovrLpd1.OffsetHigh = 0;
ovrLpd1.hEvent = hEvent;
//others
if (lpBuffer == NULL){
printf("error HeapAlloc()\n");
ExitProcess(1);
}
ZeroMemory((PVOID)lpBuffer,100*1024*1024);
//create file
HANDLE hFile = CreateFile(
"junk.file",
GENERIC_WRITE,
0,
NULL,
OPEN_ALWAYS,
FILE_FLAG_OVERLAPPED,
NULL);
if (hFile==INVALID_HANDLE_VALUE){
PostError("CreateFile: ",(int)GetLastError(), FALSE);
ExitProcess(1);
}
printf("called WriteFile\n");
bResult = WriteFile(
hFile,
lpBuffer,
dwBytesToWrite,
NULL,
&ovrLpd1);
printf("called WriteFile end\n");
GetOverlappedResult(hFile, &ovrLpd1, &dwBytesWritten, FALSE);
printf("%d\n", (int)dwBytesWritten);
if (!bResult)
PostError("WriteFile",GetLastError(), FALSE);
else
printf("File written - WriteFile returned TRUE ????\n");
printf("wating to finish async write\n");
WaitForSingleObject(hEvent, INFINITE);
CloseHandle(hFile);
HeapFree(GetProcessHeap(),0,lpBuffer);
return 0;
}
--0-1010843226-1070711023=:73637--