简体   繁体   English

了解异步/重叠 IO

[英]Understanding Asynchronous / Overlapped IO

I am learning about Asynchronous / Overlapped IO in windows.我正在学习 windows 中的异步/重叠 IO。 I have written the following code, but it doesn't compile.我已经编写了以下代码,但它没有编译。 Where is my mistake?我的错误在哪里? I don't know why we need to call something as FileIoCompletionRoutine and how should I define it?我不知道为什么我们需要调用 FileIoCompletionRoutine 以及我应该如何定义它?

#include <windows.h>
#include <iostream>

VOID WINAPI FileIOCompletionRoutine(DWORD, DWORD, LPOVERLAPPED);
HANDLE g_HandleEvent;

wchar_t string_data[] = L"Garbage data is absloute thing";

int main(int argc, char* argv[])
{
    g_HandleEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

    auto file_name = L"Cayot.txt";
    auto handle_file = CreateFile(file_name, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_FLAG_OVERLAPPED, 0);

    if (handle_file == INVALID_HANDLE_VALUE)
    {
        std::cout << "File creation is failed." << std::endl;
        return -1;
    }

    OVERLAPPED overlapped_instance = { 0 };
    overlapped_instance.hEvent = g_HandleEvent;

    WriteFileEx(handle_file, string_data, sizeof(string_data), &overlapped_instance, FileIOCompletionRoutine);

    SleepEx(INFINITE, TRUE);

    return 0;
}

Error compiler:错误编译器:

error LNK2001: unresolved external symbol "void __stdcall FileIOCompletionRoutine(unsigned long,unsigned long,struct _OVERLAPPED *)" (?FileIOCompletionRoutine@@YGXKKPAU_OVERLAPPED@@@Z)错误 LNK2001:未解析的外部符号“void __stdcall FileIOCompletionRoutine(unsigned long,unsigned long,struct _OVERLAPPED *)”(?FileIOCompletionRoutine@@YGXKKPAU_OVERLAPPED@@@Z)

You have merely forward-declared the FileIOCompletionRoutine() function, which satisfies the compiler, but you have not actually implemented the function, so the linker fails to find it.您只是前向声明了满足编译器的FileIOCompletionRoutine() function,但您并没有真正实现 function,因此 Z3175B426046787EECE737B8 找不到它。

#include <windows.h>
#include <iostream>

VOID WINAPI FileIOCompletionRoutine(DWORD, DWORD, LPOVERLAPPED);

HANDLE g_HandleEvent;
wchar_t string_data[] = L"Garbage data is absloute thing";

int main(int argc, char* argv[])
{
    g_HandleEvent = CreateEvent(NULL, TRUE, FALSE, NULL);

    auto file_name = L"Cayot.txt";
    auto handle_file = CreateFile(file_name, GENERIC_WRITE, FILE_SHARE_WRITE, 0, CREATE_ALWAYS, FILE_FLAG_OVERLAPPED, 0);

    if (handle_file == INVALID_HANDLE_VALUE)
    {
        std::cout << "File creation is failed." << std::endl;
        return -1;
    }

    OVERLAPPED overlapped_instance = { 0 };
    overlapped_instance.hEvent = g_HandleEvent;

    WriteFileEx(handle_file, string_data, sizeof(string_data), &overlapped_instance, FileIOCompletionRoutine);

    SleepEx(INFINITE, TRUE);

    return 0;
}

VOID WINAPI FileIOCompletionRoutine(DWORD dwErrorCode, DWORD dwNumberOfBytesTransfered, LPOVERLAPPED lpOverlapped)
{
    // Do something here...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM