分享

在PB中实现wince流式接口驱动【转】

 lhzstudio 2012-05-09

转自:http://blog.21ic.com/user1/1015/archives/2008/50616.html

1.在PB中建立一个平台,自己使用的是X86仿真环境裁减的一个系统.

2.在PROJECT中新建一个WIN32 DLL项目.

3.在C/C++原文件中增加函数.

4.编写DLL的导出函数定义文件.DEF

5.选择编译.到了此处,我才发现我建的这个DLL项目有问题,一直说缺少ceddk.h这个头文件.最后的解决方

法是我把自己裁减的这个系统目录下面的WINCE500目录下的有关的头文件都考到DLL项目的目录下面

了.另外,还需要设置DLL项目的属性.右键点击DLL项目,选择SETTING,然后在这几个选项中进行目录设

置.

6.编译结束,OK之后,查看DLL是否正确的导出了这些函数.在PB 中选择Build菜单中的Open Build

Release Directory打开命令提示符,输入   dumpbin/exports Motor.dll

看到.DEF文件中的函数了,就证明是已经正确导出了.

7.为驱动程序配置注册表.下载BUILTIN下.

8.写程序,进行验证.

9.查看运行状况.当wince系统启动完毕之后,在PB的tool菜单下,选择Remote Process Viewer,定位到

Device.exe,可以看到Motor.Dll已经被系统加载了.

以下是一些代码,供自己留个纪念.

//以下是MOTOR源程序

#i nclude <stdafx.h>
#i nclude <windows.h>
#i nclude <ceddk.h>

#pragma comment(lib,"ceddk.lib")

DWORD MOT_Init(LPCTSTR pContext,LPCVOID lpvBusContext);
BOOL MOT_Deinit(DWORD hDeviceContext);
DWORD MOT_Open(DWORD hDeviceContext,DWORD AccessCode,DWORD ShareMode);
BOOL MOT_Close(DWORD hOpenContext);
BOOL MOT_IOControl(DWORD hOpenContext,DWORD dwCode,PBYTE pBufIn,DWORD dwLenIn,PBYTE pBufOut,DWORD dwLenOut,PDWORD pdwActualOut);
void MOT_PowerUp(DWORD hDeviceContext);
void MOT_PowerDown(DWORD hDeviceContext);
DWORD MOT_Read(DWORD hOpenContext,LPVOID pBuffer,DWORD Count);
DWORD MOT_Write(DWORD hOpenContext,LPVOID pBuffer,DWORD Count);
DWORD MOT_Seek(DWORD hOpenContext,long Amount,WORD Type);


BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD ul_reason_for_call,
                       LPVOID lpReserved
      )
{
    switch(ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:OutputDebugString(L"MYDRIVER - DLL_PROCESS_ATTCH\r\n");break;
case DLL_PROCESS_DETACH:OutputDebugString(L"MYDRIVER - DLL_PROCESS_DETACH\r\n");break;
    case DLL_THREAD_ATTACH:OutputDebugString(L"MYDRIVER - DLL_THREAD_ATTCH\r\n");break;
case DLL_THREAD_DETACH:OutputDebugString(L"MYDRIVER - DLL_THREAD_DETACH\r\n");break;
}
    return 1;
}

DWORD MOT_Init(LPCTSTR pContext,LPCVOID lpvBusContext)
{
OutputDebugString(L"Electronic Motor Driver Init\r\n");
return 1;
}

BOOL MOT_Deinit(DWORD hDeviceContext)
{
OutputDebugString(L"Electronic Motor Driver DeInit\r\n");
return 1;
}

DWORD MOT_Open(DWORD hDeviceContext,DWORD AccessCode,DWORD ShareMode)
{
OutputDebugString(L"Electronic Motor Driver Open\r\n");
return 1;
}

BOOL MOT_Close(DWORD hOpenContext)
{
OutputDebugString(L"Electronic Motor Driver Close\r\n");
return 1;
}

DWORD MOT_Write(DWORD hOpenContext,LPVOID pBuffer,DWORD Count)
{
OutputDebugString(L"Electronic Motor Driver Write\r\n");
return 1;
}

BOOL MOT_IOControl(DWORD hOpenContext,DWORD dwCode,PBYTE pBufIn,DWORD dwLenIn,PBYTE pBufOut,DWORD dwLenOut,PDWORD pdwActualOut)
{
OutputDebugString(L"Electronic Motor Driver IO Control\r\n");
return 1;
}

void MOT_PowerUp(DWORD hDeviceContext)
{
OutputDebugString(L"Electronic Motor Driver Power Up\r\n");
}

void MOT_PowerDown(DWORD hDeviceContext)
{
OutputDebugString(L"Electronic Motor Driver Power Down\r\n");
}

DWORD MOT_Read(DWORD hOpenContext,LPVOID pBuffer,DWORD Count)
{
OutputDebugString(L"Electronic Motor Driver Read\r\n");
return 1;
}

DWORD MOT_Seek(DWORD hOpenContext,long Amount,WORD Type)
{
OutputDebugString(L"Electronic Motor Driver Seek\r\n");
return 1;
}

//bib文件定义

MODULES
Motor.dll $(_FLATRELEASEDIR)\Motor.dll               NK

//注册表定义

[HKEY_LOCAL_MACHINE\drivers\BuiltIn\Motor]
   "Dll" = "Motor.dll"
   "Prefix" = "MOT"
   "Index" = dword:1
   "Order" = dword:0
   "FriendlyName" = "Motor device"
   "Icotl" = dword:0

//def文件定义

LIBRATY MotorDriver
EXPORTS
MOT_Init
MOT_Deinit
MOT_Open
MOT_Close
MOT_IOControl
MOT_PowerUp
MOT_PowerDown
MOT_Read
MOT_Write
MOT_Seek

//测试文件

#i nclude "stdafx.h"

int WINAPI WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR     lpCmdLine,
                     int       nCmdShow)
{

HANDLE hDrv = CreateFile(L"MOT1:",GENERIC_WRITE,0,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);
if(INVALID_HANDLE_VALUE == hDrv)
{
   OutputDebugString(L"Failed to open driver..\r\n");
   return -1;
}
int count;
DWORD dwWritten;
for(int i = 0;i < 100;i++)
{
   count = 0x07;
   WriteFile(hDrv,&count,1,&dwWritten,NULL);
   Sleep(3000);
}
CloseHandle(hDrv);
return 0;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多