LPOVERLAPPED_COMPLETION_ROUTINE与函数不兼容

时间:2015-01-06 22:54:13

标签: winapi c++-cli writefile

我想使用 winapi 中的 WriteFileEx 异步将数据写入文件,但我遇到了回调问题。

我收到了关注错误:  类型为" void(*)的参数(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);
}

1 个答案:

答案 0 :(得分:4)

您应该首先仔细阅读documentation。这部分特别重要:

  

OVERLAPPED数据结构必须在写入操作期间保持有效。当写操作等待完成时,它不应该是一个超出范围的变量。

你不符合这个要求,你需要紧急解决这个问题。

至于编译错误,这很简单。您的回调不符合要求。再次咨询documentation,其签名为:

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

您省略了CALLBACK,它指定了调用约定。