[so] WriteFile x-(

Mihai Iancu so@atlantis.cs.pub.ro
Sat, 6 Dec 2003 05:11:53 -0800 (PST)


--0-1374431375-1070716313=:76435
Content-Type: text/plain; charset=us-ascii

Raspuns:
 
WriteFile intoarece true cand termina de scris sau daca este OVERLAPPED cand
termina de facut pending, asa ca daca face pending intoarce true chiar daca operatia
nu s-a terminat.
 
deci programul dat merge bine (pana la proba contrarie)

 Iancu <mail2mihai@yahoo.com> wrote:

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#include 
#include 
/*

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;
}


---------------------------------
Do you Yahoo!?
Protect your identity with Yahoo! Mail AddressGuard
--0-1374431375-1070716313=:76435
Content-Type: text/html; charset=us-ascii

<DIV>Raspuns:</DIV>
<DIV>&nbsp;</DIV>
<DIV>WriteFile intoarece true cand termina de scris sau daca este OVERLAPPED cand</DIV>
<DIV>termina de facut pending, asa ca daca face pending intoarce true chiar daca operatia</DIV>
<DIV>nu s-a terminat.</DIV>
<DIV>&nbsp;</DIV>
<DIV>deci programul dat merge bine (pana la proba contrarie)<BR><BR><B><I> Iancu &lt;mail2mihai@yahoo.com&gt;</I></B> wrote:</DIV>
<BLOCKQUOTE class=replbq style="PADDING-LEFT: 5px; MARGIN-LEFT: 5px; BORDER-LEFT: #1010ff 2px solid">
<DIV>
<P>Nu reusesc sa ma prind cum merge WriteFile cu OVERLAPPED.</P>
<P>In MSDN scrie </P>
<P>&lt;quote&gt;</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>&lt;/quote&gt;</P>
<P>&nbsp;</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>#include <STDIO.H><BR>#include <WINDOWS.H><BR>/*<BR><BR>HELPS FOR FINDING THE ERRORS RETURNED BY THE FUNCTIONS<BR><BR>*/<BR>void PostError(const char* msg, DWORD dwErr, bool bTerminate){<BR>LPVOID lpMsgBuf;<BR><BR>FormatMessage(<BR>FORMAT_MESSAGE_ALLOCATE_BUFFER |<BR>FORMAT_MESSAGE_FROM_SYSTEM |<BR>FORMAT_MESSAGE_IGNORE_INSERTS,<BR>NULL,<BR>dwErr,<BR>MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language<BR>(LPTSTR) &amp;lpMsgBuf,<BR>0,<BR>NULL);<BR>printf("%s: %s\n", msg, (LPCSTR)lpMsgBuf);<BR>// Free the buffer.<BR>LocalFree( lpMsgBuf );<BR>if (bTerminate)<BR>ExitProcess(3);<BR>}<BR>/*<BR><BR>MAIN<BR><BR>*/<BR><BR>int main(){<BR>//declare<BR>char *lpBuffer = (char*) HeapAlloc(GetProcessHeap(),0,100*1024*1024);<BR>DWORD dwBytesToWrite=100*1024*1024;<BR>DWORD dwBytesWritten;<BR>BOOL bResult;<BR>OVERLAPPED ovrLpd1;<BR>HANDLE hEvent;<BR>//create
 event<BR>hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);<BR>if (hEvent == INVALID_HANDLE_VALUE){<BR>printf("error CreateEvent\n");<BR>ExitProcess(2);<BR>}<BR>//create the overlapped structure<BR>ovrLpd1.Offset = 0;<BR>ovrLpd1.OffsetHigh = 0;<BR>ovrLpd1.hEvent = hEvent;<BR>//others<BR>if (lpBuffer == NULL){<BR>printf("error HeapAlloc()\n");<BR>ExitProcess(1);<BR>}<BR>ZeroMemory((PVOID)lpBuffer,100*1024*1024);<BR>//create file<BR>HANDLE hFile = CreateFile(<BR>"junk.file",<BR>GENERIC_WRITE,<BR>0,<BR>NULL,<BR>OPEN_ALWAYS,<BR>FILE_FLAG_OVERLAPPED,<BR>NULL);<BR>if (hFile==INVALID_HANDLE_VALUE){<BR>PostError("CreateFile: ",(int)GetLastError(), FALSE);<BR>ExitProcess(1);<BR>}<BR>printf("called WriteFile\n");<BR>bResult = WriteFile(<BR>hFile,<BR>lpBuffer,<BR>dwBytesToWrite,<BR>NULL,<BR>&amp;ovrLpd1);<BR>printf("called WriteFile end\n");<BR>GetOverlappedResult(hFile, &amp;ovrLpd1, &amp;dwBytesWritten, FALSE);<BR>printf("%d\n", (int)dwBytesWritten);<BR>if
 (!bResult)<BR>PostError("WriteFile",GetLastError(), FALSE);<BR>else<BR>printf("File written - WriteFile returned TRUE ????\n");<BR>printf("wating to finish async write\n");<BR>WaitForSingleObject(hEvent, INFINITE);<BR><BR>CloseHandle(hFile);<BR>HeapFree(GetProcessHeap(),0,lpBuffer);<BR>return 0;<BR>}<BR></BLOCKQUOTE><p><hr SIZE=1>
Do you Yahoo!?<br>
<a href="http://antispam.yahoo.com/whatsnewfree">Protect your identity with Yahoo! Mail AddressGuard</a>
--0-1374431375-1070716313=:76435--