2015-01-06 45 views
0

我希望異步數據寫入使用WriteFileExWINAPI到文件不兼容,但我有回調的一個問題。LPOVERLAPPED_COMPLETION_ROUTINE與功能

我得到如下錯誤: 類型的參數 「無效(*)(DWORD dwErrorCode,DWORD dwNumberOfBytesTransfered,LPOVERLAPPED lpOverlapped的)」 與類型 「LPOVERLAPPED_COMPLETION_ROUTINE」 的參數不兼容

什麼我做錯了嗎?

這裏是代碼:

// Callback 
void onWriteComplete(
     DWORD dwErrorCode, 
     DWORD dwNumberOfBytesTransfered, 
     LPOVERLAPPED lpOverlapped) { 
    return; 
} 

BOOL writeToOutputFile(String ^outFileName, List<String ^> ^fileNames) { 
    HANDLE hFile; 
    char DataBuffer[] = "This is some test data to write to the file."; 
    DWORD dwBytesToWrite = (DWORD) strlen(DataBuffer); 
    DWORD dwBytesWritten = 0; 
    BOOL bErrorFlag = FALSE; 

    pin_ptr<const wchar_t> wFileName = PtrToStringChars(outFileName); 

    hFile = CreateFile(
     wFileName,    // name of the write 
     GENERIC_WRITE,   // open for writing 
     0,      // do not share 
     NULL,     // default security 
     CREATE_NEW,    // create new file only 
     FILE_FLAG_OVERLAPPED, 
     NULL);     // no attr. template 

    if (hFile == INVALID_HANDLE_VALUE) { 
     return FALSE; 
    } 

    OVERLAPPED oOverlap; 
    bErrorFlag = WriteFileEx(
     hFile,   // open file handle 
     DataBuffer,  // start of data to write 
     dwBytesToWrite, // number of bytes to write 
     &oOverlap,  // overlapped structure 
     onWriteComplete), 

    CloseHandle(hFile); 
} 

回答

3

你應該仔細閱讀documentation開始。這部分是特別重要的:

The OVERLAPPED data structure must remain valid for the duration of the write operation. It should not be a variable that can go out of scope while the write operation is pending completion.

你沒有達到那個要求,你需要緊急解決這個問題。

至於編譯器錯誤,這很簡單。您的回撥不符合要求。再次諮詢documentation其中它的簽名是給出如下:

VOID CALLBACK FileIOCompletionRoutine(
    _In_  DWORD dwErrorCode, 
    _In_  DWORD dwNumberOfBytesTransfered, 
    _Inout_ LPOVERLAPPED lpOverlapped 
); 

您省略CALLBACK指定調用約定。

+0

謝謝!現在,它的工作 –

+0

順便說一句,你能再次幫助我嗎?回調在這裏根本不叫。我在等,但沒有任何事發生 –

+0

您需要閱讀文檔。你沒有檢查返回值。或者調用GetLastError。 –