c ++ 11 lambda作为ReadFileEx的回调 [英] c++11 lambda as callback of ReadFileEx

查看:213
本文介绍了c ++ 11 lambda作为ReadFileEx的回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码。

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

问题是我不知道如何解决intellisense错误。 p>

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


$ b b

我可以创建一个lambda函数,并在这种情况下使用它作为CALLBACK函数?

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

推荐答案

在Mingw 4.8.1和VS2013 ..:

Works fine in Mingw 4.8.1 and VS2013..:

创建了一个名为file.txt的文件。添加了Hello World并保存。然后我运行以下,它打印> Hello World。

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

在VS2010中,以下编译至少:

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

SleepEx参数TRUE导致它抛出..我没有线索为什么..

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

即使这种情况下,当它击中SleepEx,但上述和以下都有Hello World存储在重叠缓冲区..

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

这篇关于c ++ 11 lambda作为ReadFileEx的回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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