📄 mydriver.cpp
字号:
// MyDriver.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
DWORD DEM_Init(LPCTSTR pContext, LPCVOID lpvBusContext);
BOOL DEM_Deinit( DWORD hDeviceContext );
DWORD DEM_Open( DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode );
BOOL DEM_Close( DWORD hOpenContext );
BOOL DEM_IOControl( DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut );
void DEM_PowerUp( DWORD hDeviceContext );
void DEM_PowerDown( DWORD hDeviceContext );
DWORD DEM_Read( DWORD hOpenContext, LPVOID pBuffer, DWORD Count );
DWORD DEM_Write( DWORD hOpenContext, LPCVOID pBuffer, DWORD Count );
DWORD DEM_Seek( DWORD hOpenContext, long Amount, WORD Type );
#define IOCTL_DRIVER_DEMO 42
// Not exposed by the Device Driver
void DBGOut(DWORD dwValue);
HANDLE hMem=NULL;
DWORD dwCount;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch ( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
OutputDebugString(L"MyDriver - DLL_PROCESS_ATTACH\n");
break;
case DLL_PROCESS_DETACH:
OutputDebugString(L"MyDriver - DLL_PROCESS_DETACH\n");
break;
case DLL_THREAD_ATTACH:
OutputDebugString(L"MyDriver - DLL_THREAD_ATTACH\n");
break;
case DLL_THREAD_DETACH:
OutputDebugString(L"MyDriver - DLL_THREAD_DETACH\n");
break;
}
return TRUE;
}
DWORD DEM_Init( LPCTSTR pContext, LPCVOID lpvBusContext)
{
OutputDebugString(L"MyDriver - DEM_Init - Context: ");
OutputDebugString(pContext);
OutputDebugString(L"\n");
OutputDebugString(L"MyDriver - ~ DEM_Init\n");
return 0x1234;
}
BOOL DEM_Deinit( DWORD hDeviceContext )
{
OutputDebugString(L"MyDriver - DEM_Deinit\n");
OutputDebugString(L"MyDriver - ~ DEM_Deinit\n");
return TRUE;
}
DWORD DEM_Open( DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode )
{
OutputDebugString(L"MyDriver - DEM_Open\n");
OutputDebugString(L"hDeviceContext - ");
DBGOut(hDeviceContext);
OutputDebugString(L"\n");
OutputDebugString(L"MyDriver - ~ DEM_Open\n");
return 0x5678;
}
BOOL DEM_Close( DWORD hOpenContext )
{
OutputDebugString(L"MyDriver - DEM_Close\n");
OutputDebugString(L"hOpenContext - ");
DBGOut(hOpenContext);
OutputDebugString(L"\n");
OutputDebugString(L"MyDriver - ~ DEM_Close\n");
return TRUE;
}
BOOL DEM_IOControl( DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut )
{
OutputDebugString(L"MyDriver - DEM_IOControl\n");
OutputDebugString(L"hOpenContext - ");
DBGOut(hOpenContext);
OutputDebugString(L"\n");
switch (dwCode) {
case IOCTL_DRIVER_DEMO:
{
OutputDebugString(L"DRIVER DEMO IOCTL...\n");
// reverse the string...
HANDLE hTemp=LocalAlloc(LPTR,dwLenIn+1);
memset(hTemp,0x00,dwLenIn+1);
TCHAR *tcOut=(TCHAR*)hTemp;
TCHAR *tcIn=(TCHAR*)pBufIn;
DWORD dwChars=dwLenIn/2;
for (DWORD x=0;x < dwChars;x++) {
tcOut[x]=tcIn[dwChars-x-1];
}
memcpy(pBufOut,hTemp,dwLenIn);
LocalFree(hTemp);
*pdwActualOut=dwLenIn;
}
break;
default:
OutputDebugString(L"Unknown IOCTL\n");
break;
}
OutputDebugString(L"MyDriver - ~ DEM_IOControl\n");
return TRUE;
}
void DEM_PowerUp( DWORD hDeviceContext )
{
OutputDebugString(L"MyDriver - DEM_PowerUp\n");
OutputDebugString(L"hDeviceContext - ");
DBGOut(hDeviceContext);
OutputDebugString(L"\n");
OutputDebugString(L"MyDriver - ~ DEM_PowerUp\n");
}
void DEM_PowerDown( DWORD hDeviceContext )
{
OutputDebugString(L"MyDriver - DEM_PowerDown\n");
OutputDebugString(L"hDeviceContext - ");
DBGOut(hDeviceContext);
OutputDebugString(L"\n");
OutputDebugString(L"MyDriver - ~ DEM_PowerDown\n");
}
DWORD DEM_Read( DWORD hOpenContext, LPVOID pBuffer, DWORD Count )
{
DWORD dwRetCount=0xffff; // default to error
OutputDebugString(L"MyDriver - DEM_Read\n");
OutputDebugString(L"hOpenContext - ");
DBGOut(hOpenContext);
OutputDebugString(L"\n");
if (NULL != hMem) {
dwRetCount=dwCount;
memcpy(pBuffer,hMem,dwCount);
}
OutputDebugString(L"MyDriver - ~ DEM_Read\n");
return dwRetCount;
}
DWORD DEM_Write( DWORD hOpenContext, LPCVOID pBuffer, DWORD Count )
{
OutputDebugString(L"MyDriver - DEM_Write\n");
OutputDebugString(L"hOpenContext - ");
DBGOut(hOpenContext);
OutputDebugString(L"\n");
if (NULL != hMem) {
LocalFree(hMem);
}
hMem=LocalAlloc(LPTR,Count);
memcpy(hMem,pBuffer,Count);
dwCount=Count;
OutputDebugString(L"MyDriver - ~ DEM_Write\n");
return Count;
}
DWORD DEM_Seek( DWORD hOpenContext, long Amount, WORD Type )
{
OutputDebugString(L"MyDriver - DEM_Seek\n");
OutputDebugString(L"hOpenContext - ");
DBGOut(hOpenContext);
OutputDebugString(L"\n");
OutputDebugString(L"MyDriver - ~ DEM_Seek\n");
return 0;
}
void DBGOut(DWORD dwValue)
{
TCHAR tcTemp[10];
wsprintf(tcTemp,L"%ld",dwValue);
OutputDebugString(tcTemp);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -