ReadFile异步读设备

#include 
#include 

int main (void)
{
	HANDLE hDevice=CreateFile(TEXT("C:\\1.txt"),
		GENERIC_READ|GENERIC_WRITE,
		0,
		NULL,
		OPEN_EXISTING,
		FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED,  ///此处设置FILE_FLAG_OVERLAPPED
		NULL);
	
	if (hDevice==INVALID_HANDLE_VALUE)
	{
		printf("Open Error\n");
		return 0;
	}

	OVERLAPPED overlap={0};
	overlap.hEvent=CreateEvent(NULL,FALSE,FALSE,NULL);
	UCHAR buffer[100]={0};
	DWORD dwRead=0;
	//读设备
	ReadFile(hDevice,buffer,100,&dwRead,&overlap);

	//做一些其他操作
	OutputDebugString(TEXT("hello"));

	//等待读设备结束
	WaitForSingleObject(overlap.hEvent,INFINITE);

	//关闭句柄
	CloseHandle(hDevice);


	getchar();
	return 0;
}


你可能感兴趣的:(windows编程)