LPOVERLAPPED_COMPLETION_ROUTINE与功能不兼容 [英] LPOVERLAPPED_COMPLETION_ROUTINE is incompatible with function

查看:125
本文介绍了LPOVERLAPPED_COMPLETION_ROUTINE与功能不兼容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用 winapi 中的 WriteFileEx 将数据异步写入文件,但是我对回调有疑问。

I want to asynchronously write data to file using WriteFileEx from winapi, but I have a problem with callback.

我正在跟踪错误:
类型为 void(*)(DWORD dwErrorCode,DWORD dwNumberOfBytesTransfered,LPOVERLAPPED lpOverlapped)的参数不兼容参数类型为 LPOVERLAPPED_COMPLETION_ROUTINE

I'm getting follow error: an argument of type "void (*) (DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped)" is incompatible with parameter of type "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);
}


推荐答案

您应该先阅读仔细文档。这部分尤其重要:

You should start by reading the documentation carefully. This part is of particular import:


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

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.

您不满足该要求,并且您需要紧急解决该问题。

You are not meeting that requirement, and you need to tackle that issue urgently.

对于编译器错误,这很简单。您的回调不符合要求。再次查阅文档,其签名为:

As for the compiler error, that's simple enough. Your callback does not meet the requirements. Again consult the documentation where its signature is given as:

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

您已省略 CALLBACK 约定。

这篇关于LPOVERLAPPED_COMPLETION_ROUTINE与功能不兼容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆