一个简单的流驱动模板

来源:本站
导读:目前正在解读《一个简单的流驱动模板》的相关信息,《一个简单的流驱动模板》是由用户自行发布的知识型内容!下面请观看由(电工技术网 - www.9ddd.net)用户发布《一个简单的流驱动模板》的详细说明。
简介:鉴于网上有人向我要一个简单的流驱动的例子,其实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_ATTACHn")));

return TRUE;

case DLL_THREAD_ATTACH:

RETAILMSG(1,(TEXT("STRINGS: DLL_THREAD_ATTACHn")));

break;

case DLL_THREAD_DETACH:

RETAILMSG(1,(TEXT("STRINGS: DLL_THREAD_DETACHn")));

break;

case DLL_PROCESS_DETACH:

RETAILMSG(1,(TEXT("STRINGS: DLL_PROCESS_DETACHn")));

break;

#ifdef UNDER_CE

case DLL_PROCESS_EXITING:

RETAILMSG(1,(TEXT("STRINGS: DLL_PROCESS_EXITINGn")));

break;

case DLL_SYSTEM_STARTED:

RETAILMSG(1,(TEXT("STRINGS: DLL_SYSTEM_STARTEDn")));

break;

#endif

}

return TRUE;

}

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

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

BOOL STR_Close(DWORD hOpenContext)

{

BOOL bRet = TRUE;

RETAILMSG(1,(TEXT("STRINGS: STR_Closen")));

return bRet;

}

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

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

BOOL STR_Deinit(DWORD hDeviceContext)

{

BOOL bRet = TRUE;

RETAILMSG(1,(TEXT("STRINGS: STR_Deinitn")));

return bRet;

}

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

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

DWORD STR_Init(DWORD dwContext)

{

DWORD dwRet = 0;

RETAILMSG(1,(TEXT("STRINGS: STR_Initn")));

// Initialize buffer to zero.

memset (achBuffer, 0, BUFSIZE * sizeof(WCHAR));

// Set return value to non-zero.

dwRet = 1;

return dwRet;

}

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

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

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_IOControln")));

return bRet;

}

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

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

DWORD STR_Open(DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode)

{

DWORD dwRet = 0;

RETAILMSG(1,(TEXT("STRINGS: STR_Openn")));

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

return dwRet;

}

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

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

void STR_PowerDown(DWORD hDeviceContext)

{

RETAILMSG(1,(TEXT("STRINGS: STR_PowerDownn")));

}

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

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

void STR_PowerUp(DWORD hDeviceContext)

{

RETAILMSG(1,(TEXT("STRINGS: STR_PowerUpn")));

}

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

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

DWORD STR_Read(DWORD hOpenContext, LPVOID pBuffer, DWORD Count)

{

DWORD dwRet = 0;

RETAILMSG(1,(TEXT("STRINGS: STR_Readn")));

// 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_Seekn")));

return dwRet;

}

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

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

DWORD STR_Write(DWORD hOpenContext, LPCVOID pSourceBytes, DWORD NumberOfBytes)

{

DWORD dwRet = 0;

RETAILMSG(1,(TEXT("STRINGS: STR_Writen")));

// 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_MACHINEDriversBuiltInSTRINGS]

"Index"=dword:1

"Prefix"="STR"

"Dll"="String.dll"

"Order"=dword:0

调用这个驱动程序

#include <windows.h>

#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, '', 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;

}

提醒:《一个简单的流驱动模板》最后刷新时间 2024-03-14 01:02:43,本站为公益型个人网站,仅供个人学习和记录信息,不进行任何商业性质的盈利。如果内容、图片资源失效或内容涉及侵权,请反馈至,我们会及时处理。本站只保证内容的可读性,无法保证真实性,《一个简单的流驱动模板》该内容的真实性请自行鉴别。