c++11 lambda as callback of ReadFileEx

2019-09-14 22:03发布

Here is the code.

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
    auto f = CreateFile(L"file.txt", GENERIC_READ, FILE_SHARE_READ, nullptr, 
        OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
    struct overlapped_buffer
    {
        OVERLAPPED o;
        char b[64];
    };

    overlapped_buffer ob = {};
    ReadFileEx(f, ob.b, sizeof(ob.b), &ob.o, [] (DWORD e, DWORD c, OVERLAPPED * o)
    {
        if( ERROR_SUCCESS == e ) printf("Error");
        else {
           auto ob = reinterpret_cast<overlapped_buffer *>(o);
           printf("> %.*s\n", c, ob->b);
        }
    } );

    SleepEx( 1000, TRUE );
    CloseHandle( f );
    printf("read file");
    return 0;
}

The Issue is that I dont know how to resolve the intellisense error.

2   IntelliSense: no suitable conversion function from "lambda []void (DWORD e, DWORD c, OVERLAPPED *o)->void" to "LPOVERLAPPED_COMPLETION_ROUTINE" exists  c:\kombea\portaudiofastplayer\test_lambda\test_lambda.cpp   19  43  test_lambda

Am I able to create a lambda function and use it as a CALLBACK function in this case?

1条回答
别忘想泡老子
2楼-- · 2019-09-14 22:49

Works fine in Mingw 4.8.1 and VS2013..:

Created a file called "file.txt". Added "Hello World" to it and save it. I then ran the following and it prints "> Hello World".

#include <windows.h>
#include <stdio.h>
#include <tchar.h>

int _tmain(int argc, _TCHAR* argv[])
{
    auto f = CreateFile("file.txt", GENERIC_READ, FILE_SHARE_READ, nullptr,
                        OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
    struct overlapped_buffer
    {
        OVERLAPPED o;
        char b[64];
    };

    overlapped_buffer ob = {};
    ReadFileEx(f, ob.b, sizeof(ob.b), &ob.o, [] (DWORD e, DWORD c, OVERLAPPED * o)
    {
        auto ob = reinterpret_cast<overlapped_buffer*>(o);
        printf("> %.*s\n", c, ob->b);
    });

    SleepEx(1000, TRUE);
    CloseHandle(f);
    return 0;
}

In VS2010, the following compiles at least:

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

int main()
{
    auto f = CreateFile(L"C:/Users/Brandon/Desktop/file.txt", GENERIC_READ, FILE_SHARE_READ, nullptr,
                        OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
    struct overlapped_buffer
    {
        OVERLAPPED o;
        char b[64];
    };

    overlapped_buffer ob = {};

    ReadFileEx(f, ob.b, sizeof(ob.b), &ob.o, (LPOVERLAPPED_COMPLETION_ROUTINE) &[&] (DWORD e, DWORD c, OVERLAPPED* o)
    {
        std::cout<<"HERE\n"<<std::endl; //not being called.. already tried std::function..
    });

    //SleepEx(1000, TRUE);
    SleepEx(1000, false);
    CloseHandle(f);
    std::cin.get();
    return 0;
}

The SleepEx with parameter TRUE is causing it to throw.. I have no clue why..

Even this throws when it hits SleepEx, however both the above and the following have "Hello World" stored in the overlapped buffer..

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

int main()
{
    auto f = CreateFile(L"file.txt", GENERIC_READ, FILE_SHARE_READ, nullptr,
                        OPEN_EXISTING, FILE_FLAG_OVERLAPPED, nullptr);
    struct overlapped_buffer
    {
        OVERLAPPED o;
        char b[64];
    };

    overlapped_buffer ob = {};

    struct cb
    {
        static void callback(DWORD e, DWORD c, OVERLAPPED* o)
        {
            auto ptr = reinterpret_cast<overlapped_buffer*>(o);
            std::cout<<ptr->b<<"\n";
        }
    };

    ReadFileEx(f, ob.b, sizeof(ob.b), &ob.o, (LPOVERLAPPED_COMPLETION_ROUTINE) cb::callback);

    SleepEx(1000, TRUE);
    CloseHandle(f);
    std::cin.get();
    return 0;
}
查看更多
登录 后发表回答