#define DEBUGMSG
#include <windows.h>
#include <winioctl.h>
#include <stdio.h>
#define DEVICE_HELLO_INDEX 0x860
#define START_HELLPWORLD CTL_CODE(FILE_DEVICE_UNKNOWN,DEVICE_HELLO_INDEX,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define STOP_HELLPWORLD CTL_CODE(FILE_DEVICE_UNKNOWN,DEVICE_HELLO_INDEX+1,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define WRITE_HELLPWORLD CTL_CODE(FILE_DEVICE_UNKNOWN,DEVICE_HELLO_INDEX+2,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define READ_HELLPWORLD CTL_CODE(FILE_DEVICE_UNKNOWN,DEVICE_HELLO_INDEX+3,METHOD_BUFFERED,FILE_ANY_ACCESS)
#define erron GetLastError()
#define MY_DEVICE_NAME "\\\\.\\hello"
#define MY_DEVICE_START "-start"
#define MY_DEVICE_STOP "-stop"
#define MY_DEVICE_WRITE "-input"
#define MY_DEVICE_READ "-output"
#define MY_INPUT_BUF_SIZE 255
#define MY_OUTPUT_BUF_SIZE 255
BOOL DriverControl (TCHAR *Maik);
void Usage (TCHAR *Paramerter);
int main (int argc,TCHAR *argv[])
{
if (argc!=2)
{
Usage(argv[0]);
return 0;
}
if (strcmpi(argv[1],MY_DEVICE_START)==0 || strcmpi(argv[1],MY_DEVICE_STOP)==0
|| strcmpi(argv[1],MY_DEVICE_WRITE)==0 || strcmpi(argv[1],MY_DEVICE_READ)==0)
DriverControl(argv[1]);
else
{
Usage(argv[0]);
return 0;
}
return 0;
}
BOOL DriverControl (TCHAR *Maik)
{
HANDLE hDevice=NULL; //设备句柄
DWORD RetBytes=0;
char inputBuffer[MY_INPUT_BUF_SIZE]; //输出用户缓冲区
char outputBuffer[MY_OUTPUT_BUF_SIZE];//输入用户缓冲区
//获得设备句柄
hDevice=CreateFile(MY_DEVICE_NAME,GENERIC_READ | GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if (hDevice==INVALID_HANDLE_VALUE)
{
#ifdef DEBUGMSG
printf("CreateFile() GetLastError reports %d\n",erron);
#endif
return FALSE;
}
//启动
if (strcmpi(Maik,MY_DEVICE_START)==0)
{
//传递启动的I/O控制代码
if (!(DeviceIoControl(hDevice,START_HELLPWORLD,NULL,0,NULL,0,&RetBytes,NULL)))
{
#ifdef DEBUGMSG
printf("DeviceIoControl() GetLastError reports %d\n",erron);
#endif
CloseHandle(hDevice);
return FALSE;
}
}
//停止
if (strcmpi(Maik,MY_DEVICE_STOP)==0)
{
//传递停止的I/O控制代码
if (!(DeviceIoControl(hDevice,STOP_HELLPWORLD,NULL,0,NULL,0,&RetBytes,NULL)))
{
#ifdef DEBUGMSG
printf("DeviceIoControl() GetLastError reports %d\n",erron);
#endif
CloseHandle(hDevice);
return FALSE;
}
}
//输入数据
if (strcmpi(Maik,MY_DEVICE_WRITE)==0)
{
//输入数据缓冲区
strcpy(inputBuffer,"Hello,Input Data!");
if (!(DeviceIoControl(hDevice,WRITE_HELLPWORLD,&inputBuffer,MY_INPUT_BUF_SIZE,NULL,0,&RetBytes,NULL)))
{
#ifdef DEBUGMSG
printf("DeviceIoControl() GetLastError reports %d\n",erron);
#endif
CloseHandle(hDevice);
return FALSE;
}
}
//输出数据
if (strcmpi(Maik,MY_DEVICE_READ)==0)
{
if (!(DeviceIoControl(hDevice,READ_HELLPWORLD,NULL,0,&outputBuffer,MY_OUTPUT_BUF_SIZE,&RetBytes,NULL)))
{
#ifdef DEBUGMSG
printf("DeviceIoControl() GetLastError reports %d\n",erron);
#endif
CloseHandle(hDevice);
return FALSE;
}else{
printf("DeviceIoControl() output Data! s=%s\n return Bytes=%d%\n",outputBuffer,RetBytes);
}
}
if (hDevice)
CloseHandle(hDevice); //关闭句柄
return TRUE;
}
void Usage (TCHAR *Paramerter)
{
fprintf(stderr,"============================================================================\n"
" Hello驱动控制例程\n"
"%s -start\t启动\n"
"%s -stop \t停止\n"
"%s -input\t输入\n"
"%s -output\t输入\n"
"本程序只是用做代码交流,如有错误,还请多多包含!\n"
"============================================================================\n"
,Paramerter,Paramerter,Paramerter);
}
|