驱动程序和应用程序之间通信(ForWinCE)(转载)

WinCE上驱动程序的模型相对比较简单,实现一个驱动程序不是什么难事。尽管如此,本文还是实现了一个比较通用一点的通讯接口。

应用程序发给信息驱动程序有两种方式,同步调用和异步调用。
同步调用能够让驱动程序立即处理应用程序的请求,并返回驱动程序处理后的结果。
如:

#define MSG_TYPE_TEST_1            0x00000000
#define MSG_TYPE_ICMP_PACKET    0x00000001

#pragma pack(push, 1)
typedef struct __IO_MESSAGE
{
    ULONG    Type;
    ULONG    Length;

    union
    {
        struct
        {
            double x;
            double y;
        }Test_1;

        struct
        {
            UCHAR MiniPacket[128];
        }Icmp_Packet;

        // All other messages add here

    }value;
}IO_MESSAGE, *PIO_MESSAGE;
#pragma pack(pop)

#include <windows.h>
#include "ioexample.h"
#include "ioapp.h"

VOID SendMessageWithResult()
{
    //
    // First of all, open the device
    //
    if(!IO_Open())
        return;


    //
    // Build message
    //
    IO_MESSAGE Msg;

    Msg.Type    = MSG_TYPE_TEST_1;
    Msg.Length    = sizeof(Msg.value.Test_1);
    Msg.value.Test_1.x = 3.0;
    Msg.value.Test_1.y = 4.0;

    //
    // Call API to send, IO_SendEx run sync and then return a result.
    //
    UCHAR lpOutBuf[8];
    DWORD BytesReturned;
    BOOL bRet = IO_SendEx((PTLV)&Msg, TLV_LENGTH(&Msg), lpOutBuf, 8, &BytesReturned);
    if(!bRet) // If return FALSE, BytesReturned is error code.
    {
        switch(BytesReturned)
        {
        case IO_ERROR_INVALID_LENGTH:
            break;
        case IO_ERROR_UNKNOWN:
            break;
        }
    }

    //
    // Then we get the result
    //
    double z = *(double*)lpOutBuf;

    //
    // When we quit application, don't forget close device.
    //
    IO_Close();
}

异步调用不需要结果返回,调用立即返回。对驱动程序的请求会随后执行。
如:

VOID SendMsg()
{
    //
    // First of all, open the device
    //
    if(!IO_Open())
        return;


    //
    // Build message
    //
    IO_MESSAGE Msg;

    Msg.Type    = MSG_TYPE_TEST_1;
    Msg.Length    = sizeof(Msg.value.Test_1);
    Msg.value.Test_1.x = 3.0;
    Msg.value.Test_1.y = 4.0;

    //
    // Call API to send, IO_SendEx run async.
    //
    BOOL bRet = IO_Send((PTLV)&Msg, TLV_LENGTH(&Msg)); // if we give sizeof(Msg) is also OK.
    if(!bRet) // If return FALSE, something error.
    {
    }

    //
    // When we quit application, don't forget close device.
    //
    IO_Close();
}

驱动程序也可以通知应用程序,应用程序检测到通知事件后,可以取回驱动程序发送给应用程序的请求。

如:

VOID ThreadForReceive()
{
    //
    // First of all, open the device
    //
    if(!IO_Open())
        return;

    //
    // setup a receive event for driver
    //
    HANDLE hEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
    if(hEvent == NULL)
        return;

    IO_InitializeReceiveEvent(hEvent);

    //
    // We can wait for this event and then receive one or more times
    //
    while(1)
    {
        DWORD dRet = WaitForSingleObject(hEvent, INFINITE);
        if(dRet == WAIT_OBJECT_0)
        {
            DWORD NumbersRemained;
            IO_MESSAGE OutMessage;
            BOOL bRet = IO_Receive((PTLV)&OutMessage, sizeof(IO_MESSAGE), &NumbersRemained);
            if(bRet == FALSE) // if return false, numbersremained is error code
            {
            }
            else
            {
                
            }
            
            for(int i=0; i<NumbersRemained; i++)
            {
                DWORD temp;
                bRet = IO_Receive((PTLV)&OutMessage, sizeof(IO_MESSAGE), &temp);
            }
        }

        // We can test here whether main process let us quit
        //if(WAIT_OBJECT_0 == WaitForSingalObject(hQuitEvent, 0))
        //   break;

        // As a test, any way we should break here
        break;
    }


    // Free them
    IO_RemoveReceiveEvent();
    CloseHandle(hEvent);

    //
    // When we quit application, don't forget close device.
    //
    IO_Close();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值