WinCE6.0下OEMIOControl的实现

    在WinCE5.0中,应用程序和驱动程序可以通过调用KernelIoControl(..)函数来访问WinCE内核,导致调用 OEMIoControl函数,这样应用程序和驱动程序就可以访问到OAL中的资源了。但在WinCE6.0中,提供了更好的安全性,应用程序能够访问 OEMIoControl中的case受到了限制,默认情况下只有下面的这些case是可以让应用程序访问的:
IOCTL_HAL_GET_CACHE_INFO
IOCTL_HAL_GET_DEVICE_INFO
IOCTL_HAL_GET_DEVICEID
IOCTL_HAL_GET_UUID
IOCTL_PROCESSOR_INFORMATION
如果用户在应用程序中试图访问其他的case,肯定会返回失败的。在WinCE6.0中,驱动程序还像以前一样,可以访问OEMIoControl中的任何 case。那么我们如何让应用程序也访问到一些自己添加的case呢?以下以我IOCTL_HAL_GET_MAP_CODE为例,详细讲解如何添加自定义的case给应用程序调用:

1. %(_WINCEROOT)\PUBLIC\COMMON\OAK\INC\pkfuncs.h 添加要设置的IOCTL:

#define IOCTL_SET_KERNEL_COMM_DEV    CTL_CODE(FILE_DEVICE_HAL, 12, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_HAL_GET_UUID                CTL_CODE(FILE_DEVICE_HAL, 13, METHOD_BUFFERED, FILE_ANY_ACCESS)

#define IOCTL_HAL_GET_MAP_CODE     CTL_CODE(FILE_DEVICE_HAL,2075,METHOD_BUFFERED,FILE_ANY_ACCESS)

2. 到OAL 层的oalioctl.cpp 里添加我们定义的IOCTL :

EXTERN_C

BOOL

IOControl(

    DWORD dwIoControlCode,

    PBYTE pInBuf,

    DWORD nInBufSize,

    PBYTE pOutBuf,

    DWORD nOutBufSize,

    PDWORD pBytesReturned

)

{

    BOOL fRet = FALSE;

    //

    // By default the following ioctls are supported for user mode threads.

    // If a new ioctl is being added to this list, make sure the corresponding

    // data associated with that ioctl is marshalled properly to the OAL

    // ioctl implementation. In normal cases, one doesn't need any

    // marshaling as first level user specified buffers are already validated

    // by kernel that:

    // -- the buffers are within the user process space

    // Check out IsValidUsrPtr() function in vmlayout.h for details on kernel

    // validation of user specified buffers. Kernel doesn't validate that the

    // buffers are accessible; it only checks that the buffer start and end

    // addresses are within the user process space.

    //

    switch (dwIoControlCode)

    {

        case IOCTL_HAL_GET_CACHE_INFO:

        case IOCTL_HAL_GET_DEVICE_INFO:

        case IOCTL_HAL_GET_DEVICEID:

        case IOCTL_HAL_GET_UUID:

        case IOCTL_HAL_GET_MAP_CODE: //Andy

        case IOCTL_HAL_REBOOT:

            // request is to service the ioctl - forward the call to OAL code

            // OAL code will set the last error if there is a failure

            fRet = (*g_pfnExtOALIoctl)(dwIoControlCode, pInBuf, nInBufSize, pOutBuf, nOutBufSize, pBytesReturned);

            break;

        default:

            SetLastError(ERROR_NOT_SUPPORTED);

            break;

    }

    return fRet;

}

这些IOCTL 都是应用程序可以调用的;

3.%(_WINCEROOT)\PLATFORM\COMMON\SRC\INC\oal_ioctl_tab.h 添加 IOCTL 对应的函数:

{ IOCTL_HAL_GET_MAP_CODE,                       0,  OALIoCtlHalGetMapCode     }, //Andy

4. %(_WINCEROOT)\PLATFORM\COMMON\SRC\INC\oal_ioctl.h 添加函数声明:

BOOL OALIoCtlHalGetMapCode(UINT32, VOID *, UINT32, VOID *, UINT32, UINT32 *); //Andy

5.  %(_WINCEROOT)\PLATFORM\COMMON\SRC\COMMON\IOCTL\mapcode.c 添加函数定义:

//------------------------------------------------------------------------------

//

//  Function:  OALIoCtlHalGetMapCode

//

//  Implements the IOCTL_HAL_GET_MAP_CODE handler. This function fills in a

//  GUID structure.

//

BOOL OALIoCtlHalGetMapCode(

                            UINT32 code,

                            VOID *pInpBuffer,

                            UINT32 inpSize,

                            VOID *pOutBuffer,

                            UINT32 outSize,

                            UINT32 *pOutSize

                        )

{

     …(具体实现省略)   

    return rc;

}

这样就完成了该功能的添加。

(声明:该文章是在网络相关文章的基础上,修改了错误,更换了示例而成。因原文章被抄来抄去,已经没办法找到原著者,因此,此处没办法表明,请谅解。)

posted @ 2010-01-04 16:23  时永安  阅读(1556)  评论(1编辑  收藏  举报