📄 demodriver.cpp
字号:
// MyDriver.cpp : Defines the entry point for the DLL application.
//
#include "stdafx.h"
#define LOG_CALLS
void Log(char *szStr, bool bLF = true)
{
FILE *f = fopen("\\DemoDriverLog.txt", "a");
fprintf(f, "%u", GetTickCount());
fprintf(f, "%s", szStr);
if (bLF)
fprintf(f, "\n");
fflush(f);
fclose(f);
}
DEMODLL_API DWORD COM_Init(LPCTSTR pContext, LPCVOID lpvBusContext);
DEMODLL_API BOOL COM_Deinit( DWORD hDeviceContext );
DEMODLL_API DWORD COM_Open( DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode );
DEMODLL_API BOOL COM_Close( DWORD hOpenContext );
DEMODLL_API BOOL COM_IOControl( DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut );
DEMODLL_API void COM_PowerUp( DWORD hDeviceContext );
DEMODLL_API void COM_PowerDown( DWORD hDeviceContext );
DEMODLL_API DWORD COM_Read( DWORD hOpenContext, LPVOID pBuffer, DWORD Count );
DEMODLL_API DWORD COM_Write( DWORD hOpenContext, LPCVOID pBuffer, DWORD Count );
DEMODLL_API DWORD COM_Seek( DWORD hOpenContext, long Amount, WORD Type );
#define DEVICE_CONTEXT 0x1450
#define OPEN_CONTEXT 0x1451
HANDLE hComm = INVALID_HANDLE_VALUE;
DWORD dwManagePort = 1;
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch ( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
DEMODLL_API DWORD COM_Init( LPCTSTR pContext, LPCVOID lpvBusContext)
{
#ifdef LOG_CALLS
Log("Init");
#endif
DWORD dwPort = 0;
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, pContext, 0, 0, &hKey) == ERROR_SUCCESS)
{
TCHAR szDriverKey[255];
DWORD dwSize = 255;
DWORD dwType;
if (RegQueryValueEx(hKey, L"Key", 0, &dwType, (BYTE *)szDriverKey, &dwSize) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szDriverKey, 0, 0, &hKey) == ERROR_SUCCESS)
{
dwSize=4;
if (RegQueryValueEx(hKey, L"ManagePort", 0, &dwType, (BYTE *)&dwPort, &dwSize) != ERROR_SUCCESS)
dwPort = 0;
RegCloseKey(hKey);
}
}
}
if (dwPort != 0)
dwManagePort = dwPort;
return DEVICE_CONTEXT;
}
DEMODLL_API BOOL COM_Deinit( DWORD hDeviceContext )
{
if (hDeviceContext != DEVICE_CONTEXT)
{
#ifdef LOG_CALLS
Log("False Deinst");
#endif
return FALSE;
}
#ifdef LOG_CALLS
Log("DeInit");
#endif
return TRUE;
}
DEMODLL_API DWORD COM_Open( DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode )
{
if (hDeviceContext != DEVICE_CONTEXT)
{
#ifdef LOG_CALLS
Log("False Open");
#endif
return 0;
}
TCHAR szPort[255];
swprintf(szPort, L"COM%u:", dwManagePort);
hComm = CreateFile(
szPort,
AccessCode,
ShareMode,
NULL,
OPEN_EXISTING,
0,
NULL);
#ifdef LOG_CALLS
char buf[255];
sprintf(buf, "Open port %S for access %u and share %u %s", szPort, AccessCode, ShareMode, hComm ? "OK" : "Failed");
Log(buf);
#endif
if (hComm == INVALID_HANDLE_VALUE)
return 0;
else
return OPEN_CONTEXT;
}
DEMODLL_API BOOL COM_Close( DWORD hOpenContext )
{
if (hOpenContext != OPEN_CONTEXT)
{
#ifdef LOG_CALLS
Log("False Close");
#endif
return 0;
}
#ifdef LOG_CALLS
Log("Close");
#endif
BOOL bRet = CloseHandle(hComm);
hComm = INVALID_HANDLE_VALUE;
return bRet;
}
DEMODLL_API BOOL COM_IOControl( DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut )
{
if (hOpenContext != OPEN_CONTEXT)
{
#ifdef LOG_CALLS
Log("False IOControl");
#endif
return 0;
}
#ifdef LOG_CALLS
char buf[255];
sprintf(buf, "IOControl code=%u, inlen=%u", dwCode, dwLenIn);
Log(buf);
#endif
BOOL bRet=DeviceIoControl(
hComm,
dwCode,
pBufIn,
dwLenIn,
pBufOut,
dwLenOut,
pdwActualOut,
NULL);
#ifdef LOG_CALLS
sprintf(buf, "IOControl return bRet=%d, outlen=%u", bRet, *pdwActualOut);
Log(buf);
#endif
return bRet;
}
DEMODLL_API void COM_PowerUp( DWORD hDeviceContext )
{
}
DEMODLL_API void COM_PowerDown( DWORD hDeviceContext )
{
}
DEMODLL_API DWORD COM_Read( DWORD hOpenContext, LPVOID pBuffer, DWORD Count )
{
if (hOpenContext != OPEN_CONTEXT)
{
#ifdef LOG_CALLS
Log("False Read");
#endif
return 0;
}
#ifdef LOG_CALLS
char buf[255];
sprintf(buf, "Read max=%u", Count);
Log(buf);
#endif
DWORD dwBytes = 0;
ReadFile(hComm, pBuffer, Count, &dwBytes, NULL);
return dwBytes;
}
DEMODLL_API DWORD COM_Write( DWORD hOpenContext, LPCVOID pBuffer, DWORD Count )
{
if (hOpenContext != OPEN_CONTEXT)
{
#ifdef LOG_CALLS
Log("False Write");
#endif
return 0;
}
#ifdef LOG_CALLS
char buf[255];
sprintf(buf, "Write bytes=%u", Count);
Log(buf);
#endif
DWORD dwBytes = 0;
WriteFile(hComm, pBuffer, Count, &dwBytes, NULL);
return dwBytes;
}
DEMODLL_API DWORD COM_Seek( DWORD hOpenContext, long Amount, WORD Type )
{
#ifdef LOG_CALLS
char buf[255];
sprintf(buf, "Seek Amount=%u, Type=%u", Amount, Type);
Log(buf);
#endif
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -