1、5驱动程序模型和加载实验实验五驱动程序模型和加载实验1实验目的:1. 熟悉驱动程序的原理和功能;2. 熟悉流式接口驱动程序的结构;3. 掌握实现流式接口驱动的基本步骤;4. 掌握在Platform Builder编写流式接口驱动的方法。2实验任务:1构建一个Wince平台;2. 编写流式接口的驱动程序;3. 编写驱动测试程序;4. 运行驱动和测试程序,查看输出结果。3实验步骤:1. 启动Platform Builder 如图1,单击“开始”菜单,选择“所有程序”-“Microsoft Windows CE 5.0”-“Platform Builder 5.0”,启动Platform Build
2、er。启动界面如图2。图1 Platform Builder5.0位置图2 Platform Builder5.0启动界面2. 创建一个定制的操作系统设计方案 选择“File”-“New Platform”,弹出“New Platform Wizard”向导,选择“Next”; 在Name文本框,输入“DriverModel”作为操作系统设计的名字;在Path文本框,输入或选择你的操作系统设计方案放置的根目录的路径;默认“%_WINCEROOT%PBWorkspaces DriverModel”如图3,然后选择“Next”;图3 工作空间的名字和位置 从可以获得的板级支持包列表(BSP)中,选
3、择EMULATOR: X86,如图4(在界面右侧的信息栏中显示该BSP的相关信息),然后选择“Next”;图4 选择板级支持包 从可以获得的操作系统设计模板中,选择“Enterprise Web Pad”,如图5,然后选择“Next”;图5 选择设计模板 删除“Applications & Media”选项,如图6,然后选择“Next”;图6 删除“Applications & Media”选项 删除“Networking & Communications”选项,如图7,然后选择“Next”;图7 删除“Networking & Communications”选项 默认通告,然后选择“Next
4、”; 点击” Finish”,完成新平台向导,如图8。图8 完成新平台向导3. 编写流式接口的驱动程序 在Platform Builder中选择“File”-“New Project or File”,创建一个“Windows CE Dynamic link library”项目,项目的名称填写“MyDriver”,如图9;图9 “New Project or File”对话框 向导的第一步,默认,然后点击“Next”; 向导的第二步选择“A Simple Windows CE DLL”,如图10,然后点击“Next”。Platform Builder会为我们生成DLL框架代码。 图10 St
5、ep 2 of 3 向导的第三步默认,然后点击“Finish”。 在WorkSpace工作区,打开FileView,找到文件MyDriver.cpp,位置如图11。双击打开后,在编辑区修改Platform Builder生成的DllMain函数,如下:BOOL APIENTRY DllMain( HANDLE hModule, DWORD ul_reason_for_call, LPVOID lpReserved ) switch ( ul_reason_for_call ) case DLL_PROCESS_ATTACH: OutputDebugString(LMyDriver - DLL_
6、PROCESS_ATTACHn); break; case DLL_PROCESS_DETACH: OutputDebugString(LMyDriver - DLL_PROCESS_DETACHn); break; case DLL_THREAD_ATTACH: OutputDebugString(LMyDriver - DLL_THREAD_ATTACHn); break; case DLL_THREAD_DETACH: OutputDebugString(LMyDriver - DLL_THREAD_DETACHn); break; return TRUE;图11 文件MyDriver.
7、cpp位置 添加流式接口驱动的导出函数前置声明,放到文件MyDriver.cpp,添加到DllMain函数之前,如下:DWORD DEM_Init(LPCTSTR pContext, LPCVOID lpvBusContext);BOOL DEM_Deinit( DWORD hDeviceContext );DWORD DEM_Open( DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode );BOOL DEM_Close( DWORD hOpenContext );BOOL DEM_IOControl( DWORD hOpenCon
8、text, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut );void DEM_PowerUp( DWORD hDeviceContext );void DEM_PowerDown( DWORD hDeviceContext );DWORD DEM_Read( DWORD hOpenContext, LPVOID pBuffer, DWORD Count );DWORD DEM_Write( DWORD hOpenContext, LPCVOID pBu
9、ffer, DWORD Count );DWORD DEM_Seek( DWORD hOpenContext, long Amount, WORD Type );#define IOCTL_DRIVER_DEMO 42/ Not exposed by the Device Driver void DBGOut(DWORD dwValue);HANDLE hMem=NULL;DWORD dwCount; 添加导出函数的具体实现,放到文件MyDriver.cpp,代码如下:DWORD DEM_Init( LPCTSTR pContext, LPCVOID lpvBusContext) Output
10、DebugString(LMyDriver - DEM_Init - Context: ); OutputDebugString(pContext); OutputDebugString(Ln); OutputDebugString(LMyDriver - DEM_Initn); return 0x1234;BOOL DEM_Deinit( DWORD hDeviceContext ) OutputDebugString(LMyDriver - DEM_Deinitn); OutputDebugString(LMyDriver - DEM_Deinitn); return TRUE;DWORD
11、 DEM_Open( DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode ) OutputDebugString(LMyDriver - DEM_Openn); OutputDebugString(LhDeviceContext - ); DBGOut(hDeviceContext); OutputDebugString(Ln); OutputDebugString(LMyDriver - DEM_Openn); return 0x5678;BOOL DEM_Close( DWORD hOpenContext ) OutputDebu
12、gString(LMyDriver - DEM_Closen); OutputDebugString(LhOpenContext - ); DBGOut(hOpenContext); OutputDebugString(Ln); OutputDebugString(LMyDriver - DEM_Closen); return TRUE;BOOL DEM_IOControl( DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActual
13、Out ) OutputDebugString(LMyDriver - DEM_IOControln); OutputDebugString(LhOpenContext - ); DBGOut(hOpenContext); OutputDebugString(Ln); switch (dwCode) case IOCTL_DRIVER_DEMO: OutputDebugString(LDRIVER DEMO IOCTL.n); / reverse the string. HANDLE hTemp=LocalAlloc(LPTR,dwLenIn+1); memset(hTemp,0x00,dwL
14、enIn+1); TCHAR *tcOut=(TCHAR*)hTemp; TCHAR *tcIn=(TCHAR*)pBufIn; DWORD dwChars=dwLenIn/2; for (DWORD x=0;x New Project or File - Files -Text File,文件名叫“MyDriver.def”,如图12,弹出的对话框,选择“是”。图12 New Project or FileMyDriver.def内容如下:LIBRARY MyDriverEXPORTS DEM_Init DEM_Deinit DEM_Open DEM_Close DEM_IOControl
15、DEM_PowerUp DEM_PowerDown DEM_Read DEM_Write DEM_Seek本程序只是实现流式驱动的接口,并没有真正同硬件交互。 在WorkSpace的Parameter View中打开project.reg,在文件中添加如下部分:HKEY_LOCAL_MACHINEDriversBuiltInSample Dll = mydriver.Dll Prefix = DEM Index = dword:1 Order = dword:0 FriendlyName = Demo Driver Ioctl = dword:0提示:这段注册表信息告诉系统,在系统启动的时候把
16、mydriver.dll加载到device.exe,驱动的前缀是DEM,下标索引是1,这样,我们就可以使用CreateFile(L”DEM1:”)这样的方式来访问驱动程序了。4. 编写驱动测试程序 在Platform Builder中选择“File”-“New Project or File”,创建名为“MyApp”的“典型Hello World” WCE Application。 打开”MyApp”工程,选择”ResouceView”,在”MyApp resources”点右键-”Insert.”-”Menu”,新建一个菜单项。添加菜单项如下图13所示。菜单项的ID如表1所示。图13 Fil
17、e菜单 表一 菜单项IDCaptionIDExitID_FILE_EXITWriteID_DRIVER_WRITEReadID_DRIVER_READIo ControlID_DRIVER_IOCTL 打开MyApp工程目录下的MyApp.cpp文件,将其代码修改如下: / MyApp.cpp : Defines the entry point for the application./#include stdafx.h#include resource.h#include #pragma comment(lib, commctrl)#define MAX_LOADSTRING 100#def
18、ine IDC_CMDBAR 0x101HWND hWndCmdBar;void WriteToDriver( );void ReadFromDriver( );void HandleIOCTL( );#define IOCTL_DRIVER_DEMO 42/ Global Variables:HINSTANCE hInst; / current instanceTCHAR szTitleMAX_LOADSTRING; / The title bar textTCHAR szWindowClassMAX_LOADSTRING; / The title bar text/ Forward dec
19、larations of functions included in this code module:ATOM MyRegisterClass(HINSTANCE hInstance);BOOL InitInstance(HINSTANCE, int);LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);void WriteToDriver( ) DWORD dwWritten; TCHAR *tcString=LDemo String.; HANDLE hDrv=CreateFile(LDEM1:,GENERIC_WRITE,0,NUL
20、L,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (INVALID_HANDLE_VALUE = hDrv) OutputDebugString(LFailed to open Driver.n); else WriteFile(hDrv,(LPVOID)tcString,lstrlen(tcString)*sizeof(TCHAR),&dwWritten,NULL); CloseHandle(hDrv);void ReadFromDriver( ) DWORD dwRead; TCHAR tcTemp30; HANDLE hDrv=CreateF
21、ile(LDEM1:,GENERIC_READ,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); if (INVALID_HANDLE_VALUE = hDrv) OutputDebugString(LFailed to open Driver.n); else memset(tcTemp,0x00,30*sizeof(TCHAR); ReadFile(hDrv,tcTemp,30,&dwRead,NULL); MessageBox(NULL,tcTemp,LDemo Data,MB_OK); CloseHandle(hDrv);void Ha
22、ndleIOCTL( ) HANDLE hDrv=CreateFile(LDEM1:,GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL); TCHAR tcBuffer10; DWORD dwBytesReturned; lstrcpy(tcBuffer,LHello); BOOL bRet=DeviceIoControl( hDrv, IOCTL_DRIVER_DEMO, tcBuffer, lstrlen(tcBuffer)*sizeof(TCHAR), tcBuffer, lstrlen(tcBuffer)*siz
23、eof(TCHAR), &dwBytesReturned, NULL); MessageBox(NULL,tcBuffer,LIOCTL Test,MB_OK); CloseHandle(hDrv); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) / TODO: Place code here. MSG msg; HACCEL hAccelTable; / Initialize global strings LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING); LoadString(hInstance, IDC_MyApp, szWindowClass, MAX_LOADSTRING); MyRegisterClass(hInstance); / Perform application initialization
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1