Saya belajar tentang Asynchronous/Overlapped IO di windows. Saya telah menulis kode berikut, tetapi tidak dapat dikompilasi. Di mana kesalahan saya? Saya tidak tahu mengapa kita perlu memanggil sesuatu sebagai FileIoCompletionRoutine dan bagaimana saya harus mendefinisikannya?

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

Kompiler kesalahan:

kesalahan LNK2001: simbol eksternal yang tidak terselesaikan "void __stdcall FileIOCompletionRoutine(unsigned long,unsigned long,struct _OVERLAPPED *)" (?FileIOCompletionRoutine@@YGXKKPAU_OVERLAPPED@@@Z)

0
Cayoot 4 April 2021, 15:28

1 menjawab

Jawaban Terbaik

Anda baru saja mendeklarasikan fungsi FileIOCompletionRoutine(), yang memenuhi compiler, tetapi Anda belum benar-benar mengimplementasikan fungsi tersebut, sehingga linker gagal menemukannya.

#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...
}
1
Remy Lebeau 4 April 2021, 17:15