有时我们在用回调函数时,经常会碰到void*类型(等于PVOID),转一般的int,double啊之类的类型强制转换就可得到,如:int in = (int)pvArgToCompletionRoutine; 但string类型可不能强制转换的到啊,强制转换直接报错,编译器通过不了。那换种方法,可通过如下方式获得:
#include<iostream>
#include<Windows.h>
#include <thread>
using namespace std;
VOID APIENTRY TimerAPCRoutine(PVOID pvArgToCompletionRoutine, DWORD dwTimerLowValue, DWORD dwTimerHighValue)
{
//char *str = new char[10];
char * str = (char *)pvArgToCompletionRoutine;
string ss=str;
std::cout << "dio:"<< str << std::endl;
}
void timer(unsigned int interval, PVOID de) {
HANDLE hTimer = CreateWaitableTimer(NULL, FALSE, NULL);
LARGE_INTEGER li;
li.QuadPart = 0;
if (!SetWaitableTimer(hTimer, &li, interval, TimerAPCRoutine,de, FALSE))
{
CloseHandle(hTimer);
return;
}
while (1)
{
SleepEx(interval, TRUE);
}
CloseHandle(hTimer);
}
int main()
{
string li = "name";
void* ll = (void*)li.c_str();
std::thread t1(timer, 1000, ll);
t1.join();
return 0;
}