首页 | 新闻 | 新品 | 文库 | 方案 | 视频 | 下载 | 商城 | 开发板 | 数据中心 | 座谈新版 | 培训 | 工具 | 博客 | 论坛 | 百科 | GEC | 活动 | 主题月 | 电子展
返回列表 回复 发帖

一个简单的流驱动模板

一个简单的流驱动模板

    鉴于网上有人向我要一个简单的流驱动的例子,其实PB自己带例子已经很多了!但是对于初学者可能不知道到哪儿找所以发表一个简单的流驱动模板,对于老鸟可能没有用不过对于初学者或许有用!这个驱动的功能是保存一个字符串!希望对初学者有帮助!


驱动程序部分


#define _WIN32_WINNT 0x0400
#include <windows.h>
#include <tchar.h>


HANDLE g_hInstance;


#define BUFSIZE 256
WCHAR achBuffer[BUFSIZE];


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
BOOL WINAPI 
DllEntryPoint(HANDLE hinstDLL,
              DWORD dwReason,
              LPVOID /* lpvReserved */)
{
    switch(dwReason)
    {
    case DLL_PROCESS_ATTACH:
        g_hInstance = hinstDLL;
        RETAILMSG(1,(TEXT("STRINGS: DLL_PROCESS_ATTACH")));
        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;
}



//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
BOOL STR_Close(DWORD hOpenContext)
{
    BOOL bRet = TRUE;
    RETAILMSG(1,(TEXT("STRINGS: STR_Close\n")));
    return bRet;
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
BOOL STR_Deinit(DWORD hDeviceContext)
{
    BOOL bRet = TRUE;
    RETAILMSG(1,(TEXT("STRINGS: STR_Deinit\n")));
    return bRet;
}


//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD STR_Init(DWORD dwContext)
{
    DWORD dwRet = 0;
    RETAILMSG(1,(TEXT("STRINGS: STR_Init\n")));


    // Initialize buffer to zero.
    memset (achBuffer, 0, BUFSIZE * sizeof(WCHAR));


    // Set return value to non-zero.
    dwRet = 1;
    return dwRet;
}


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

51 c8051f(f020,f040) msp430 arm(2410,2510) fpga(xc3s4000) dsp(5116 dm642) keilc vc++ matlab linux protel Ten_layerPCB mpegx h.26x Rscode Turbocode ofdm VideoBroadcasting ldpc_code(now!)
//-----------------------------------------------------------------------------
BOOL STR_IOControl(DWORD hOpenContext,
DWORD dwCode,
PBYTE pBufIn,
DWORD dwLenIn,
PBYTE pBufOut,
DWORD dwLenOut,
PDWORD pdwActualOut)
{
BOOL bRet = TRUE;
RETAILMSG(1,(TEXT("STRINGS: STR_IOControl\n")));
return bRet;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD STR_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)
{
DWORD dwRet = 0;
RETAILMSG(1,(TEXT("STRINGS: STR_Open\n")));

dwRet = 1; // Have to make non-zero for this call to succeed.
return dwRet;
}

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
void STR_PowerDown(DWORD hDeviceContext)
{
RETAILMSG(1,(TEXT("STRINGS: STR_PowerDown\n")));
}

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

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD STR_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)
{
DWORD dwRet = 0;
RETAILMSG(1,(TEXT("STRINGS: STR_Read\n")));

// Copy the smaller of buffer size or string size.
DWORD cbBuffer = wcslen(achBuffer);
dwRet = min(cbBuffer, Count);
wcsncpy((LPWSTR)pBuffer, achBuffer, dwRet);

// Return number of bytes read.
return dwRet;
}

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

//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
DWORD STR_Write(DWORD hOpenContext, LPCVOID pSourceBytes, DWORD NumberOfBytes)
{
DWORD dwRet = 0;
RETAILMSG(1,(TEXT("STRINGS: STR_Write\n")));

// Copy the smaller of buffer size or number of bytes they send us.
dwRet = min(BUFSIZE, NumberOfBytes);
wcsncpy(achBuffer, (LPWSTR)pSourceBytes, dwRet);

// Return number of bytes written.
return dwRet;
}


驱动程序注册表部分

[HKEY_LOCAL_MACHINE\Drivers\BuiltIn\STRINGS]
"Index"=dword:1
"Prefix"="STR"
"Dll"="String.dll"
"Order"=dword:0

调用这个驱动程序

#include
#include "StdAfx.h"

#define BUFFER_SIZE 256 // The buffer size is the same as the driver's buffer size


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

// Open the STRINGS driver with READ and WRITE access
// ---------------------------------------------------------------------
HANDLE hStr = CreateFile(TEXT("STR1:"), GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, 0);
if (INVALID_HANDLE_VALUE == hStr)
{
MessageBox(NULL, _T("Cannot open STR1:"), _T("StringApp"), MB_OK);
return 0;
}


// Write a string to the driver.
// ---------------------------------------------------------------------
DWORD dwWritten = 0;
WCHAR* pString = TEXT("This is a test of the String Driver. This is o-nly a test");
WriteFile(hStr, pString, (_tcslen(pString)+1), &dwWritten, NULL);



// Read string from driver.
// ---------------------------------------------------------------------
WCHAR wch[BUFFER_SIZE];
DWORD dwBytesRead = BUFFER_SIZE;
memset(&wch, '\0', BUFFER_SIZE * sizeof(WCHAR));

ReadFile(hStr, wch, sizeof(wch), &dwBytesRead, NULL);

MessageBox(NULL, wch, TEXT("StringApp"), MB_OK);



// Disconnect from driver.
// ---------------------------------------------------------------------
CloseHandle(hStr);

return 0;
}
51 c8051f(f020,f040) msp430 arm(2410,2510) fpga(xc3s4000) dsp(5116 dm642) keilc vc++ matlab linux protel Ten_layerPCB mpegx h.26x Rscode Turbocode ofdm VideoBroadcasting ldpc_code(now!)
返回列表