驱动程序部分
#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 only 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;
}