2014-02-27 75 views
0

以下是代碼。C++ 11作爲ReadFileEx回調的lambda

#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錯誤。

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 

我能否創建一個lambda函數並在此情況下將它用作CALLBACK函數?

+0

什麼版本你正在使用Visual Studio?您的代碼將在VS2010中不起作用(https://connect.microsoft.com/VisualStudio/feedback/details/572138)。 – Praetorian

+0

代碼是否實際編譯?智能感知通常會標記出這樣的東西可以正常工作。 –

+0

否代碼不能編譯。編譯器崩潰。 「錯誤1:錯誤C1001:編譯器中發生內部錯誤。」 – user35025

回答

0

工作正常MinGW的4.8.1和VS2013 ..:

創建了一個名爲 「file.txt的」 文件。添加「Hello World」並保存。然後我運行以下內容並打印「> 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,下面編譯至少:

#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導致它扔。我不知道爲什麼..

即使這個時候拋出它碰到了SleepEx,但是上面和下面都有存儲在重疊緩衝區中的「Hello World」。

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