WINDOWS CE/MOBILE 流驱动编写

那天在网上还看到有人继承讨论 什么流驱动要实现的函数是不是一定要叫做INIT,OPEN什么的,前辍到底是哪来的,等等,于是干脆就翻出来自己当时入门时的一个程序SHARE一下:)
其实你要实现的函数可以看下面的注释,前辍吗是在注册表当中写好的,呵呵。应该是"Prefix"="STR",然后你就要实现XXX_INIT.
以这个最小的驱动为例子,注释在后面了,
// StringDriver.cpp : Defines the entry point for the DLL application.
//

#include "stdafx.h"

#include <tchar.h>
#define BUFSIZE 256
WCHAR achBuffer[BUFSIZE];

BOOL APIENTRY DllMain( HANDLE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch(ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
        RETAILMSG(1,(TEXT("Strings:DLL_PROCESS_ATTACH/n")));
        return TRUE;
    case DLL_THREAD_ATTACH:
        RETAILMSG(1,(TEXT("Strings:DLL_THREAD_ATTACH/n")));
        break;
    case DLL_THREAD_DETACH:
        RETAILMSG(1,(TEXT("Strings:DLL_THREAD_DETACH/n")));
        break;
    case DLL_PROCESS_DETACH:
        RETAILMSG(1,(TEXT("Strings:DLL_PROCESS_DETACH/n")));
        break;

#ifdef UNDER_CE
    case DLL_PROCESS_EXITING:
        RETAILMSG(1,(TEXT("Strings:DLL_PROCESS_EXITING/n")));
        break;
    case DLL_SYSTEM_STARTED:
        RETAILMSG(1,(TEXT("Strings:DLL_SYSTEM_STARTED/n")));
        break;
#endif
    }
    return TRUE;
}
//此上的代码没什么大用.
//导出各符号
STRINGDRIVERDLL_API DWORD STR_Init(DWORD dwContext);
STRINGDRIVERDLL_API BOOL STR_Deinit(DWORD hDeviceConext);
STRINGDRIVERDLL_API DWORD STR_Open(DWORD hDeviceContext,DWORD AccessCode,DWORD ShareMode);
STRINGDRIVERDLL_API BOOL STR_Close(DWORD hOpenContext);
STRINGDRIVERDLL_API BOOL STR_IOControl(DWORD hOpenContext,DWORD swCode,PBYTE pBufIn,DWORD dwLenIn,PBYTE pBufOut,DWORD dwLenOut,PDWORD pdwActualOut);
STRINGDRIVERDLL_API void STR_PowerDown(DWORD hDeviceContext);
STRINGDRIVERDLL_API void STR_PowerUp(DWORD hDeviceContext);
STRINGDRIVERDLL_API DWORD STR_Read(DWORD hOpenContext,LPVOID pBuffer,DWORD Count);
STRINGDRIVERDLL_API DWORD STR_Seek(DWORD hOpenContext,long Amount,DWORD Type);
STRINGDRIVERDLL_API DWORD STR_Write(DWORD hOpenContext,LPVOID pSourceBytes,DWORD NumberOfBytes);

//以下为驱动各个派遣例程函数

STRINGDRIVERDLL_API DWORD STR_Init(DWORD dwContext)
{
    DWORD dwRet=0;
    RETAILMSG(1,(TEXT("Strings:STR_Init/n")));

    //初始化驱动程序
    memset(achBuffer,0,BUFSIZE*sizeof(WCHAR));
    //返回一个不为0的数
    dwRet=1;
    return dwRet;
}

//---------------------------------------------------------
STRINGDRIVERDLL_API BOOL STR_Deinit(DWORD hDeviceConext)
{
   与INIT相对泻载函数       
    BOOL bRet=TRUE;
    RETAILMSG(1,(TEXT("Strings:STR_Deinit/n")));
    return bRet;
}

//---------------------------------------------------------
STRINGDRIVERDLL_API DWORD STR_Open(DWORD hDeviceContext,DWORD AccessCode,DWORD ShareMode)
{
    DWORD dwRet=0;
    RETAILMSG(1,(TEXT("Strings:STR_Open/n")));
    //一个要返回一个非空的噢,非常谁调他谁就ACEESS DENY了:)
    dwRet=1;
    return dwRet;
}

//---------------------------------------------------------
STRINGDRIVERDLL_API BOOL STR_Close(DWORD hOpenContext)
{
    BOOL bRet=TRUE;
//这个可不是缺载啊,是关闭!
    RETAILMSG(1,(TEXT("Strings:STR_Close/n")));
    return bRet;
}

//---------------------------------------------------------
STRINGDRIVERDLL_API BOOL STR_IOControl(DWORD hOpenContext,DWORD swCode,PBYTE pBufIn,DWORD dwLenIn,PBYTE pBufOut,DWORD dwLenOut,PDWORD pdwActualOut)
{
    BOOL bRet=TRUE;
    RETAILMSG(1,(TEXT("Strings:STR_IOControl/n")));
    return bRet;
}

//---------------------------------------------------------
STRINGDRIVERDLL_API void STR_PowerDown(DWORD hDeviceContext)
{
    RETAILMSG(1,(TEXT("Strings:STR_PowerDown/n")));
   // 当这个设备断电了,系统会通知   
}

//---------------------------------------------------------
STRINGDRIVERDLL_API void STR_PowerUp(DWORD hDeviceContext)
{
    RETAILMSG(1,(TEXT("Strings:STR_PowerUp/n")));
}

//---------------------------------------------------------
STRINGDRIVERDLL_API DWORD STR_Read(DWORD hOpenContext,LPVOID pBuffer,DWORD Count)
{
    DWORD dwRet=0;
    RETAILMSG(1,(TEXT("Strings:STR_Read/n")));
    //确定实际要读取的字节的大小并读取数据
    DWORD cbBuffer=wcslen(achBuffer);
    dwRet=min(cbBuffer,Count);
    wcsncpy((LPWSTR)pBuffer,achBuffer,dwRet);
    //返回实际读取的字节数
    return dwRet;
}

//---------------------------------------------------------
STRINGDRIVERDLL_API DWORD STR_Seek(DWORD hOpenContext,long Amount,DWORD Type)
{
    DWORD dwRet=0;
    RETAILMSG(1,(TEXT("Strings:STR_Seek/n")));
    return dwRet;
}

//---------------------------------------------------------
STRINGDRIVERDLL_API DWORD STR_Write(DWORD hOpenContext,LPVOID pSourceBytes,DWORD NumberOfBytes)
{
    DWORD dw
Ret=0;
    RETAILMSG(1,(TEXT("Strings:STR_Write/n")));

    //确定实际要写入字节的大小,写入数据
    dwRet=min(BUFSIZE,NumberOfBytes);
    wcsncpy(achBuffer,(LPWSTR)pSourceBytes,dwRet);

    //返回实际写入的字节数
    return dwRet;
}

posted @ 2008-11-12 14:50  javawebsoa  Views(167)  Comments(0Edit  收藏  举报