阅读:1519回复:7
一个基础的驱动问题,在线等,谢谢!
小弟刚刚开始学习写驱动程序.根据书本写了一个测试程序,希望能监控到串口(com2)收到的数据(win98下).结果什么信息也看不到.下面是代码,请各位帮忙看看,是什么地方错了,谢谢!
vxd的名称是port,下面是代码: // Port - include file for VxD PORT #include <vtoolscp.h> #define DEVICE_CLASS PortDevice #define PORT_DeviceID UNDEFINED_DEVICE_ID #define PORT_Init_Order UNDEFINED_INIT_ORDER #define PORT_Major 1 #define PORT_Minor 0 #define COUNT 65535 struct PortStruct{WORD wPort;WORD wVal;BOOL bInput;}; class MyPort : public VIOPort { public: MyPort(WORD port); ~MyPort(); DWORD handler(VMHANDLE hVM,DWORD port,CLIENT_STRUCT *pRegs,DWORD iotype,DWORD outdata); static int Count; static PortStruct *pBuffer; static int Num; }; class PortDevice : public VDevice { public: PortDevice(); virtual DWORD OnW32DeviceIoControl(PIOCTLPARAMS pDIOCParams); MyPort *p2F8,*p2F9,*p2FA,*p2FB,*p2FC,*p2FD,*p2FE,*p2FF; }; class PortVM : public VVirtualMachine { public: PortVM(VMHANDLE hVM); }; class PortThread : public VThread { public: PortThread(THREADHANDLE hThread); }; // Port - main module for VxD PORT #define DEVICE_MAIN #include "port.h" Declare_Virtual_Device(PORT) #undef DEVICE_MAIN int MyPort::Count; int MyPort::Num; PortStruct *MyPort::pBuffer; MyPort::MyPort(WORD port) : VIOPort(port) { if (Num==0) pBuffer=new PortStruct[COUNT]; Num++; hook(); } MyPort::~MyPort() { Num--; if (Num==0) delete pBuffer; } DWORD MyPort::handler(VMHANDLE hVM,DWORD port,CLIENT_STRUCT *pRegs,DWORD iotype,DWORD outdata) { char data; switch(iotype) { case BYTE_INPUT: pBuffer[Count].wPort=port; data=_inp(port); pBuffer[Count].wVal=data; pBuffer[Count].bInput=true; if (++Count=COUNT) Count=COUNT; return data; case WORD_INPUT: pBuffer[Count].wPort=port; data=_inp(port); pBuffer[Count].wVal=data; pBuffer[Count].bInput=true; if (++Count=COUNT) Count=COUNT; return data; case BYTE_OUTPUT: pBuffer[Count].wPort=port; data=_outp(port,outdata); pBuffer[Count].wVal=outdata; pBuffer[Count].bInput=false; if (++Count>=COUNT) Count=COUNT; return data; case WORD_OUTPUT: pBuffer[Count].wPort=port; data=_outp(port,outdata); pBuffer[Count].wVal=outdata; pBuffer[Count].bInput=false; if (++Count>=COUNT) Count=COUNT; return data; default: break; } return 0; } PortDevice::PortDevice() { p2F8=p2F9=p2FA=p2FB=p2FC=p2FD=p2FE=p2FF=0; } PortVM::PortVM(VMHANDLE hVM) : VVirtualMachine(hVM) {} PortThread::PortThread(THREADHANDLE hThread) : VThread(hThread) {} DWORD PortDevice::OnW32DeviceIoControl(PIOCTLPARAMS pDIOCParams) { int i; switch(pDIOCParams->dioc_IOCtlCode) { case DIOC_OPEN: p2F8=new MyPort(0x2F8); p2F9=new MyPort(0x2F9); p2FA=new MyPort(0x2FA); p2FB=new MyPort(0x2FB); p2FC=new MyPort(0x2FC); p2FD=new MyPort(0x2FD); p2FE=new MyPort(0x2FE); p2FF=new MyPort(0x2FF); break; case DIOC_CLOSEHANDLE: if (p2F8) {delete p2F8;p2F8=0;} if (p2F9) {delete p2F9;p2F9=0;} if (p2FA) {delete p2FA;p2FA=0;} if (p2FB) {delete p2FB;p2FB=0;} if (p2FC) {delete p2FC;p2FC=0;} if (p2FD) {delete p2FD;p2FD=0;} if (p2FE) {delete p2FE;p2FE=0;} if (p2FF) {delete p2FF;p2FF=0;} break; case 111: int size=pDIOCParams->dioc_cbOutBuf; size=MyPort::Count<size?MyPort::Count:size; memcpy(pDIOCParams->dioc_OutBuf,MyPort::pBuffer,size*sizeof(PortStruct)); *pDIOCParams->dioc_bytesret=size; break; } return 0; } 调用port.vxd的是testport,代码如下: //testport.h #define COUNT 65535 struct PortStruct { WORD wPort; WORD wVal; BOOL bInput; }; //testport.cpp #include "conio.h" #include <stdio.h> #include <stdlib.h> #include <windows.h> #include <winioctl.h> #include "testport.h" void main() { PortStruct *pBuffer=new PortStruct[COUNT]; DWORD nBytes; HANDLE hVxD; char ch; _cprintf("l=Load IOPORT VXD, c=Close IOPORT VXD, x=Exit, Others=Do not load IOPORT VXD\n"); ch=getch(); while (ch!='x') { if (ch=='l') { if (hVxD!=NULL) CloseHandle(hVxD); hVxD=CreateFile("\\\\.\\d:\\port.vxd",0,0,0,OPEN_EXISTING,FILE_FLAG_DELETE_ON_CLOSE,0); if (hVxD==INVALID_HANDLE_VALUE) { printf("Open VxD error:%s",GetLastError()); exit(1); } } if (ch=='c') { if (hVxD!=NULL) CloseHandle(hVxD); } if (DeviceIoControl(hVxD,111,NULL,0,(LPVOID) pBuffer,COUNT,&nBytes,0)) { for (DWORD i=0;i<nBytes;i++) printf("%3x : %02x , %s\n",(WORD)pBuffer.wPort,(WORD)pBuffer.wVal,pBuffer.bInput?"Input":"Output"); } _cprintf("l=Load IOPORT VXD, c=Close IOPORT VXD, x=Exit, Others=Do not load IOPORT VXD\n"); ch=getch(); } } 当我按下'l'的时候,我设断点看,nBytes是0. |
|
沙发#
发布于:2005-02-02 16:50
对了,刚才忘记说了,我在com2属性中看到的输入/输出范围是02F8-02FF.
|
|
板凳#
发布于:2005-02-02 17:51
你得选中不使用smileys,否则代码没办法看了。
|
|
|
地板#
发布于:2005-02-02 20:21
不好分析,只有编译时,根据错误提示逐步分析。
在调用时,借助softice分析。 |
|
地下室#
发布于:2005-02-03 08:39
不好分析,只有编译时,根据错误提示逐步分析。 |
|
|
5楼#
发布于:2005-02-03 09:19
我编译port.vxd文件和porttest.exe文件的时候并没有任何error和warning.不过我在debug porttest.exe文件的时候有Loaded 'C:\WINDOWS\SYSTEM\KERNEL32.DLL', no matching symbolic information found.提示.
小弟并不是很明白是什么意思. |
|
6楼#
发布于:2005-02-03 22:29
这个思路就不对,你又没有Hook端口操作,即使Hook,你还要分析COM口操作,太麻烦,Hook串口驱动会更方便些
|
|
|
7楼#
发布于:2005-02-04 15:24
建议楼主使用softice来一步一步跟踪!
|
|