mirror: 上游快照导入

This commit is contained in:
gitea committed 2026-11-30 09:52:36 +08:00
commit 7475ccb467
86 files changed
+3213

No files matched your search

+179
View File
@@ -0,0 +1,179 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup Label="ProjectConfigurations">
<ProjectConfiguration Include="Debug|Win32">
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>17.0</VCProjectVersion>
<Keyword>Win32Proj</Keyword>
<ProjectGuid>{9504cd32-5959-4dc7-ba59-6d4debac8688}</ProjectGuid>
<RootNamespace>CSharpLoaderDll</RootNamespace>
<WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>DynamicLibrary</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<PlatformToolset>v143</PlatformToolset>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</ImportGroup>
<ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<TargetName>version</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<TargetName>version</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<TargetName>version</TargetName>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<TargetName>version</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;_DEBUG;CSHARPLOADERDLL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>WIN32;NDEBUG;CSHARPLOADERDLL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>_DEBUG;CSHARPLOADERDLL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<SDLCheck>true</SDLCheck>
<PreprocessorDefinitions>NDEBUG;CSHARPLOADERDLL_EXPORTS;_WINDOWS;_USRDLL;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<ConformanceMode>true</ConformanceMode>
<PrecompiledHeader>Use</PrecompiledHeader>
<PrecompiledHeaderFile>pch.h</PrecompiledHeaderFile>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableUAC>false</EnableUAC>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<ClInclude Include="framework.h" />
<ClInclude Include="hid_dll.hpp" />
<ClInclude Include="memory.h" />
<ClInclude Include="pch.h" />
<ClInclude Include="plugin.hpp" />
<ClInclude Include="version_dll.hpp" />
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp" />
<ClCompile Include="hid_dll.cpp" />
<ClCompile Include="memory.cpp" />
<ClCompile Include="pch.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="plugin.cpp" />
<ClCompile Include="version_dll.cpp" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets">
</ImportGroup>
</Project>
@@ -0,0 +1,57 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<Filter Include="源文件">
<UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier>
<Extensions>cpp;c;cc;cxx;c++;cppm;ixx;def;odl;idl;hpj;bat;asm;asmx</Extensions>
</Filter>
<Filter Include="头文件">
<UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier>
<Extensions>h;hh;hpp;hxx;h++;hm;inl;inc;ipp;xsd</Extensions>
</Filter>
<Filter Include="资源文件">
<UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier>
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter>
</ItemGroup>
<ItemGroup>
<ClInclude Include="framework.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="pch.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="memory.h">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="hid_dll.hpp">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="version_dll.hpp">
<Filter>头文件</Filter>
</ClInclude>
<ClInclude Include="plugin.hpp">
<Filter>头文件</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<ClCompile Include="dllmain.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="pch.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="memory.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="hid_dll.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="version_dll.cpp">
<Filter>源文件</Filter>
</ClCompile>
<ClCompile Include="plugin.cpp">
<Filter>源文件</Filter>
</ClCompile>
</ItemGroup>
</Project>
+231
View File
@@ -0,0 +1,231 @@
// dllmain.cpp : 定义 DLL 应用程序的入口点。
#include "pch.h"
#include <iostream>
#include "memory.h"
#include "hid_dll.hpp"
#include "version_dll.hpp"
#include "plugin.hpp"
enum class DllType {
Unknown,
Version,
Hid,
};
static DllType dllType = DllType::Unknown;
struct MonoAssemblyOpenRequest
{
uint8_t raw[40];
};
typedef enum {
MONO_IMAGE_OK,
MONO_IMAGE_ERROR_ERRNO,
MONO_IMAGE_MISSING_ASSEMBLYREF,
MONO_IMAGE_IMAGE_INVALID
} MonoImageOpenStatus;
// MonoThread * mono_thread_internal_attach (MonoDomain *domain)
typedef void* (*mono_thread_internal_attach_t)(void* domain);
// MonoAssembly * mono_assembly_request_open (const char *filename, const MonoAssemblyOpenRequest *open_req, MonoImageOpenStatus *status)
typedef void* (*mono_assembly_request_open_t)(const char* filename, const MonoAssemblyOpenRequest* open_req, MonoImageOpenStatus* status);
/*
gint32 ves_icall_System_AppDomain_ExecuteAssembly (
MonoAppDomainHandle ad, MonoReflectionAssemblyHandle refass, MonoArrayHandle args, MonoError *error)
struct _MonoAppDomain { // size: 32
MonoMarshalByRefObject mbr; // 0
MonoDomain *data; // 24
};
struct _MonoReflectionAssembly { // size: 32
MonoObject object; // 0
MonoAssembly *assembly; // 16
MonoObject *evidence; // 24
};
*/
struct _MonoAppDomain { // size: 32
uint8_t mbr[24]; // 0
void* data; // 24
};
struct _MonoReflectionAssembly { // size: 32
uint8_t object[16]; // 0
void* assembly; // 16
void* evidence; // 24
};
struct MonoError {
uint8_t raw[104];
};
typedef int (*ves_icall_System_AppDomain_ExecuteAssembly_t)(_MonoAppDomain** ad, _MonoReflectionAssembly** refass, void** args, MonoError* error);
DWORD WINAPI MainThread(LPVOID dwModule)
{
if (dllType == DllType::Version)
{
Sleep(3000);
}
const char* configFile = "./CSharpLoader/b1cs.ini";
UINT enableConsole = GetPrivateProfileIntA("Settings", "Console", 0, configFile);
BOOL enableJit = GetPrivateProfileIntA("Settings", "EnableJit", 1, configFile);
if (enableConsole == 1) {
AllocConsole();
FILE* fDummy;
freopen_s(&fDummy, "CONIN$", "r", stdin);
freopen_s(&fDummy, "CONOUT$", "w", stdout);
freopen_s(&fDummy, "CONOUT$", "w", stderr);
}
loadPluginDlls();
std::cout << "CSharpLoader enableJit: " << enableJit << std::endl;
std::cout << "CSharpLoader wait for init." << std::endl;
// enable jit
if (enableJit) {
uint64_t memory_fuction_ptr = signature("83 3D ? ? ? ? 00 0F 84 ? ? ? ? C7 84 24 ? ? 00 00 01 00 00 00").GetPointer();
if (memory_fuction_ptr == 0) {
std::cout << "memory function signature found." << std::endl;
} else {
DWORD old_protect;
if (VirtualProtect((void*)(memory_fuction_ptr + 7), 2, PAGE_EXECUTE_READWRITE, &old_protect)) {
*(uint16_t*)(memory_fuction_ptr + 7) = 0xE990; // nop; jmp
VirtualProtect((void*)(memory_fuction_ptr + 7), 2, old_protect, &old_protect);
uint64_t mono_mode_ptr = signature("48 8D 0D ? ? ? ? E8 ? ? ? ? 89 44 24 ? 83 7C 24 ? 00").GetPointer();
if (mono_mode_ptr == 0) {
std::cout << "mono_mode signature found." << std::endl;
} else {
if (VirtualProtect((void*)(mono_mode_ptr + 7), 5, PAGE_EXECUTE_READWRITE, &old_protect)) {
*(uint8_t*)(mono_mode_ptr + 7) = 0xB8;
*(uint32_t*)(mono_mode_ptr + 8) = 1;
VirtualProtect((void*)(mono_mode_ptr + 7), 5, old_protect, &old_protect);
}
}
}
}
}
signature domain_s("F0 FF 88 B0 00 00 00 48 8B 05 ? ? ? ? 48 3B D8 49 0F 44 C4");
if (domain_s.GetPointer() == 0) {
std::cout << "domainPtr pattern not found." << std::endl;
return EXIT_FAILURE;
}
void **domainPtr = (void**)domain_s.instruction(10).add(14).GetPointer();
if (domainPtr == nullptr) {
std::cout << "domainPtr not found." << std::endl;
return EXIT_FAILURE;
}
auto mono_thread_internal_attach = (mono_thread_internal_attach_t)signature(
"40 57 48 83 EC 30 8B 15 ? ? ? ? 48 8B F9 65 48 8B 04 25 58 00 00 00 B9 A8 02 00 00 48 8B 04 D0 48 83 3C 01 00").GetPointer();
if (mono_thread_internal_attach == nullptr) {
std::cout << "mono_thread_internal_attach not found." << std::endl;
return EXIT_FAILURE;
}
auto mono_assembly_request_open = (mono_assembly_request_open_t)signature(
"40 55 41 56 41 57 48 8D AC 24 40 FF FF FF 48 81 EC C0 01 00 00 48 8B 05 ? ? ? ? 48 33 C4 48 89 85 90 00 00 00 48 89 4D 90").GetPointer();
if (mono_assembly_request_open == nullptr) {
std::cout << "mono_assembly_request_open not found." << std::endl;
return EXIT_FAILURE;
}
auto ves_icall_System_AppDomain_ExecuteAssembly = (ves_icall_System_AppDomain_ExecuteAssembly_t)signature(
"48 89 5C 24 08 55 56 57 41 56 41 57 48 83 EC 50 48 89 94 24 88 00 00 00").GetPointer();
if (ves_icall_System_AppDomain_ExecuteAssembly == nullptr) {
std::cout << "ves_icall_System_AppDomain_ExecuteAssembly not found." << std::endl;
return EXIT_FAILURE;
}
void* domain = nullptr;
for (size_t i = 0; i < 180; i++) {
domain = *domainPtr;
if (domain != nullptr) {
break;
}
Sleep(1000); // 1s
}
if (domain == nullptr) {
std::cout << "domain is null." << std::endl;
return EXIT_FAILURE;
}
Sleep(2000); // 2s
void* mono_thread = mono_thread_internal_attach(domain);
if (mono_thread == nullptr) {
std::cout << "mono_thread_internal_attach failed." << std::endl;
return EXIT_FAILURE;
}
MonoAssemblyOpenRequest open_request{};
MonoImageOpenStatus status = MonoImageOpenStatus::MONO_IMAGE_OK;
wchar_t fullFilename[MAX_PATH];
GetFullPathName(L"CSharpLoader\\CSharpManager.bin", MAX_PATH, fullFilename, nullptr);
char fullFilenameA[MAX_PATH];
// convert to utf-8 to support Chinese path
WideCharToMultiByte(CP_UTF8, 0, fullFilename, MAX_PATH, fullFilenameA, MAX_PATH, NULL, NULL);
void* assembly = mono_assembly_request_open(fullFilenameA, &open_request, &status);
if (assembly == nullptr) {
std::cout << "mono_assembly_request_open failed." << std::endl;
return EXIT_FAILURE;
}
_MonoAppDomain ad{};
ad.data = domain;
_MonoReflectionAssembly refass{};
refass.assembly = assembly;
MonoError mono_error{};
_MonoAppDomain* p_ad = &ad;
_MonoReflectionAssembly* p_refass = &refass;
void* args = nullptr;
int res = ves_icall_System_AppDomain_ExecuteAssembly(&p_ad, &p_refass, &args, &mono_error);
if (res != 0) {
std::cout << "ves_icall_System_AppDomain_ExecuteAssembly failed: " << res << std::endl;
return EXIT_FAILURE;
}
std::cout << "CSharpLoader init success." << std::endl;
return EXIT_SUCCESS;
}
void init_dll(HMODULE hModule) {
DisableThreadLibraryCalls(hModule);
wchar_t moduleFullpathFilename[MAX_PATH + 1];
GetModuleFileNameW(hModule, moduleFullpathFilename, static_cast<UINT>(std::size(moduleFullpathFilename)));
wchar_t fname[_MAX_FNAME+1];
{
wchar_t drive[_MAX_DRIVE+1];
wchar_t dir[_MAX_DIR+1];
wchar_t ext[_MAX_EXT+1];
_wsplitpath_s(moduleFullpathFilename, drive, dir, fname, ext);
}
if (_wcsicmp(fname, L"version") == 0) {
dllType = DllType::Version;
init_version_dll();
} else if (_wcsicmp(fname, L"hid") == 0) {
dllType = DllType::Hid;
init_hid_dll();
}
CreateThread(nullptr, 0, MainThread, hModule, 0, nullptr);
}
void deinit_dll() {
if (dllType == DllType::Version) {
deinit_version_dll();
} else if (dllType == DllType::Hid) {
deinit_hid_dll();
}
// unloadPluginDlls();
}
BOOL APIENTRY DllMain( HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
static std::once_flag initFlag;
static std::once_flag cleanupFlag;
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
std::call_once(initFlag, [&]() { init_dll(hModule); });
break;
case DLL_PROCESS_DETACH:
std::call_once(cleanupFlag, [&]() { deinit_dll(); });
break;
}
return TRUE;
}
+5
View File
@@ -0,0 +1,5 @@
#pragma once
#define WIN32_LEAN_AND_MEAN // 从 Windows 头文件中排除极少使用的内容
// Windows 头文件
#include <windows.h>
+221
View File
@@ -0,0 +1,221 @@
#include "pch.h"
#include "hid_dll.hpp"
#include <windows.h>
#include <hidsdi.h>
#include <string>
#include <stdio.h>
///////////////////////////////////////////////////////////////
static HMODULE hModuleDll = nullptr;
static const wchar_t dllFname[] = L"hid";
bool init_hid_dll() {
deinit_hid_dll();
wchar_t systemDirectory[MAX_PATH]{0};
GetSystemDirectoryW(systemDirectory, MAX_PATH);
wchar_t fullpathDllName[MAX_PATH]{0};
swprintf_s(fullpathDllName, MAX_PATH, L"%s\\%s.dll", systemDirectory, dllFname);
if ((hModuleDll = LoadLibraryW(fullpathDllName)) == NULL) {
return false;
}
wprintf(L"load %s success\n", dllFname);
return true;
}
bool deinit_hid_dll() {
if(hModuleDll != nullptr) {
FreeLibrary(hModuleDll);
hModuleDll = nullptr;
}
return true;
}
///////////////////////////////////////////////////////////////
template<typename T>
void setup(T*& funcPtr, const char* funcName) {
if(funcPtr != nullptr) {
return;
}
funcPtr = reinterpret_cast<T*>(GetProcAddress(hModuleDll, funcName));
}
#define D(funcname, ...) \
static decltype(funcname)* p; \
setup(p, #funcname); \
return p(__VA_ARGS__); \
__pragma(comment(linker, "/EXPORT:" __FUNCTION__ "=" __FUNCDNAME__))
// https://github.com/reactos/reactos/blob/7033ab18dfa936168a279913035587858dd143e7/dll/win32/hid/hid.c#L376
extern "C" ULONG WINAPI HidD_Hello(PCHAR Buffer, ULONG BufferLength) {
D(HidD_Hello, Buffer, BufferLength);
}
extern "C" BOOLEAN WINAPI HidD_GetAttributes(HANDLE HidDeviceObject, PHIDD_ATTRIBUTES Attributes) {
D(HidD_GetAttributes, HidDeviceObject, Attributes);
}
extern "C" void WINAPI HidD_GetHidGuid(LPGUID HidGuid) {
D(HidD_GetHidGuid, HidGuid);
}
extern "C" BOOLEAN WINAPI HidD_GetPreparsedData(HANDLE HidDeviceObject, PHIDP_PREPARSED_DATA * PreparsedData) {
D(HidD_GetPreparsedData, HidDeviceObject, PreparsedData);
}
extern "C" BOOLEAN WINAPI HidD_FreePreparsedData(PHIDP_PREPARSED_DATA PreparsedData) {
D(HidD_FreePreparsedData, PreparsedData);
}
extern "C" BOOLEAN WINAPI HidD_FlushQueue(HANDLE HidDeviceObject) {
D(HidD_FlushQueue, HidDeviceObject);
}
extern "C" BOOLEAN WINAPI HidD_GetConfiguration(HANDLE HidDeviceObject, PHIDD_CONFIGURATION Configuration, ULONG ConfigurationLength) {
D(HidD_GetConfiguration, HidDeviceObject, Configuration, ConfigurationLength);
}
extern "C" BOOLEAN WINAPI HidD_SetConfiguration(HANDLE HidDeviceObject, PHIDD_CONFIGURATION Configuration, ULONG ConfigurationLength) {
D(HidD_SetConfiguration, HidDeviceObject, Configuration, ConfigurationLength);
}
extern "C" BOOLEAN WINAPI HidD_GetFeature(HANDLE HidDeviceObject, PVOID ReportBuffer, ULONG ReportBufferLength) {
D(HidD_GetFeature, HidDeviceObject, ReportBuffer, ReportBufferLength);
}
extern "C" BOOLEAN WINAPI HidD_SetFeature(HANDLE HidDeviceObject, PVOID ReportBuffer, ULONG ReportBufferLength) {
D(HidD_SetFeature, HidDeviceObject, ReportBuffer, ReportBufferLength);
}
extern "C" BOOLEAN WINAPI HidD_GetInputReport(HANDLE HidDeviceObject, PVOID ReportBuffer, ULONG ReportBufferLength) {
D(HidD_GetInputReport, HidDeviceObject, ReportBuffer, ReportBufferLength);
}
extern "C" BOOLEAN WINAPI HidD_SetOutputReport(HANDLE HidDeviceObject, PVOID ReportBuffer, ULONG ReportBufferLength) {
D(HidD_SetOutputReport, HidDeviceObject, ReportBuffer, ReportBufferLength);
}
extern "C" BOOLEAN WINAPI HidD_GetNumInputBuffers(HANDLE HidDeviceObject, PULONG NumberBuffers) {
D(HidD_GetNumInputBuffers, HidDeviceObject, NumberBuffers);
}
extern "C" BOOLEAN WINAPI HidD_SetNumInputBuffers(HANDLE HidDeviceObject, ULONG NumberBuffers) {
D(HidD_SetNumInputBuffers, HidDeviceObject, NumberBuffers);
}
extern "C" BOOLEAN WINAPI HidD_GetPhysicalDescriptor(HANDLE HidDeviceObject, PVOID Buffer, ULONG BufferLength) {
D(HidD_GetPhysicalDescriptor, HidDeviceObject, Buffer, BufferLength);
}
extern "C" BOOLEAN WINAPI HidD_GetManufacturerString(HANDLE HidDeviceObject, PVOID Buffer, ULONG BufferLength) {
D(HidD_GetManufacturerString, HidDeviceObject, Buffer, BufferLength);
}
extern "C" BOOLEAN WINAPI HidD_GetProductString(HANDLE HidDeviceObject, PVOID Buffer, ULONG BufferLength) {
D(HidD_GetProductString, HidDeviceObject, Buffer, BufferLength);
}
extern "C" BOOLEAN WINAPI HidD_GetIndexedString(HANDLE HidDeviceObject, ULONG StringIndex, PVOID Buffer, ULONG BufferLength) {
D(HidD_GetIndexedString, HidDeviceObject, StringIndex, Buffer, BufferLength);
}
extern "C" BOOLEAN WINAPI HidD_GetSerialNumberString(HANDLE HidDeviceObject, PVOID Buffer, ULONG BufferLength) {
D(HidD_GetSerialNumberString, HidDeviceObject, Buffer, BufferLength);
}
extern "C" BOOLEAN WINAPI HidD_GetMsGenreDescriptor(HANDLE HidDeviceObject, PVOID Buffer, ULONG BufferLength) {
D(HidD_GetMsGenreDescriptor, HidDeviceObject, Buffer, BufferLength);
}
extern "C" NTSTATUS WINAPI HidP_GetCaps(PHIDP_PREPARSED_DATA PreparsedData, PHIDP_CAPS Capabilities) {
D(HidP_GetCaps, PreparsedData, Capabilities);
}
extern "C" NTSTATUS WINAPI HidP_GetLinkCollectionNodes(PHIDP_LINK_COLLECTION_NODE LinkCollectionNodes, PULONG LinkCollectionNodesLength, PHIDP_PREPARSED_DATA PreparsedData) {
D(HidP_GetLinkCollectionNodes, LinkCollectionNodes, LinkCollectionNodesLength, PreparsedData);
}
extern "C" NTSTATUS WINAPI HidP_GetSpecificButtonCaps(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, USHORT LinkCollection, USAGE Usage, PHIDP_BUTTON_CAPS ButtonCaps, PUSHORT ButtonCapsLength, PHIDP_PREPARSED_DATA PreparsedData) {
D(HidP_GetSpecificButtonCaps, ReportType, UsagePage, LinkCollection, Usage, ButtonCaps, ButtonCapsLength, PreparsedData);
}
extern "C" NTSTATUS WINAPI HidP_GetButtonCaps(HIDP_REPORT_TYPE ReportType, PHIDP_BUTTON_CAPS ButtonCaps, PUSHORT ButtonCapsLength, PHIDP_PREPARSED_DATA PreparsedData) {
D(HidP_GetButtonCaps, ReportType, ButtonCaps, ButtonCapsLength, PreparsedData);
}
extern "C" NTSTATUS WINAPI HidP_GetSpecificValueCaps(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, USHORT LinkCollection, USAGE Usage, PHIDP_VALUE_CAPS ValueCaps, PUSHORT ValueCapsLength, PHIDP_PREPARSED_DATA PreparsedData) {
D(HidP_GetSpecificValueCaps, ReportType, UsagePage, LinkCollection, Usage, ValueCaps, ValueCapsLength, PreparsedData);
}
extern "C" NTSTATUS WINAPI HidP_GetValueCaps(HIDP_REPORT_TYPE ReportType, PHIDP_VALUE_CAPS ValueCaps, PUSHORT ValueCapsLength, PHIDP_PREPARSED_DATA PreparsedData) {
D(HidP_GetValueCaps, ReportType, ValueCaps, ValueCapsLength, PreparsedData);
}
extern "C" NTSTATUS WINAPI HidP_GetExtendedAttributes(HIDP_REPORT_TYPE ReportType, USHORT DataIndex, PHIDP_PREPARSED_DATA PreparsedData, PHIDP_EXTENDED_ATTRIBUTES Attributes, PULONG LengthAttributes) {
D(HidP_GetExtendedAttributes, ReportType, DataIndex, PreparsedData, Attributes, LengthAttributes);
}
extern "C" NTSTATUS WINAPI HidP_InitializeReportForID(HIDP_REPORT_TYPE ReportType, UCHAR ReportID, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_InitializeReportForID, ReportType, ReportID, PreparsedData, Report, ReportLength);
}
extern "C" NTSTATUS WINAPI HidP_SetData(HIDP_REPORT_TYPE ReportType, PHIDP_DATA DataList, PULONG DataLength, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_SetData, ReportType, DataList, DataLength, PreparsedData, Report, ReportLength);
}
extern "C" NTSTATUS WINAPI HidP_GetData(HIDP_REPORT_TYPE ReportType, PHIDP_DATA DataList, PULONG DataLength, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_GetData, ReportType, DataList, DataLength, PreparsedData, Report, ReportLength);
}
extern "C" ULONG WINAPI HidP_MaxDataListLength(HIDP_REPORT_TYPE ReportType, PHIDP_PREPARSED_DATA PreparsedData) {
D(HidP_MaxDataListLength, ReportType, PreparsedData);
}
extern "C" NTSTATUS WINAPI HidP_SetUsages(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, USHORT LinkCollection, PUSAGE UsageList, PULONG UsageLength, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength ) {
D(HidP_SetUsages, ReportType, UsagePage, LinkCollection, UsageList, UsageLength, PreparsedData, Report, ReportLength);
}
extern "C" NTSTATUS WINAPI HidP_UnsetUsages(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, USHORT LinkCollection, PUSAGE UsageList, PULONG UsageLength, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_UnsetUsages, ReportType, UsagePage, LinkCollection, UsageList, UsageLength, PreparsedData, Report, ReportLength);
}
extern "C" NTSTATUS WINAPI HidP_GetUsages(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, USHORT LinkCollection, PUSAGE UsageList, PULONG UsageLength, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_GetUsages, ReportType, UsagePage, LinkCollection, UsageList, UsageLength, PreparsedData, Report, ReportLength);
}
extern "C" NTSTATUS WINAPI HidP_GetUsagesEx(HIDP_REPORT_TYPE ReportType, USHORT LinkCollection, PUSAGE_AND_PAGE ButtonList, ULONG * UsageLength, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_GetUsagesEx, ReportType, LinkCollection, ButtonList, UsageLength, PreparsedData, Report, ReportLength);
}
extern "C" ULONG WINAPI HidP_MaxUsageListLength(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, PHIDP_PREPARSED_DATA PreparsedData) {
D(HidP_MaxUsageListLength, ReportType, UsagePage, PreparsedData);
}
extern "C" NTSTATUS WINAPI HidP_SetUsageValue(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, USHORT LinkCollection, USAGE Usage, ULONG UsageValue, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_SetUsageValue, ReportType, UsagePage, LinkCollection, Usage, UsageValue, PreparsedData, Report, ReportLength);
}
extern "C" NTSTATUS WINAPI HidP_SetScaledUsageValue(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, USHORT LinkCollection, USAGE Usage, LONG UsageValue, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_SetScaledUsageValue, ReportType, UsagePage, LinkCollection, Usage, UsageValue, PreparsedData, Report, ReportLength);
}
extern "C" NTSTATUS WINAPI HidP_SetUsageValueArray(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, USHORT LinkCollection, USAGE Usage, PCHAR UsageValue, USHORT UsageValueByteLength, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_SetUsageValueArray, ReportType, UsagePage, LinkCollection, Usage, UsageValue, UsageValueByteLength, PreparsedData, Report, ReportLength);
}
extern "C" NTSTATUS WINAPI HidP_GetUsageValue(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, USHORT LinkCollection, USAGE Usage, PULONG UsageValue, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_GetUsageValue, ReportType, UsagePage, LinkCollection, Usage, UsageValue, PreparsedData, Report, ReportLength);
}
extern "C" NTSTATUS WINAPI HidP_GetScaledUsageValue(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, USHORT LinkCollection, USAGE Usage, PLONG UsageValue, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_GetScaledUsageValue, ReportType, UsagePage, LinkCollection, Usage, UsageValue, PreparsedData, Report, ReportLength);
}
extern "C" NTSTATUS WINAPI HidP_GetUsageValueArray(HIDP_REPORT_TYPE ReportType, USAGE UsagePage, USHORT LinkCollection, USAGE Usage, PCHAR UsageValue, USHORT UsageValueByteLength, PHIDP_PREPARSED_DATA PreparsedData, PCHAR Report, ULONG ReportLength) {
D(HidP_GetUsageValueArray, ReportType, UsagePage, LinkCollection, Usage, UsageValue, UsageValueByteLength, PreparsedData, Report, ReportLength);
}
extern "C" NTSTATUS WINAPI HidP_UsageListDifference(PUSAGE PreviousUsageList, PUSAGE CurrentUsageList, PUSAGE BreakUsageList, PUSAGE MakeUsageList, ULONG UsageListLength) {
D(HidP_UsageListDifference, PreviousUsageList, CurrentUsageList, BreakUsageList, MakeUsageList, UsageListLength);
}
+4
View File
@@ -0,0 +1,4 @@
#pragma once
bool init_hid_dll();
bool deinit_hid_dll();
+277
View File
@@ -0,0 +1,277 @@
#include "pch.h"
#include "memory.h"
memory::allocator memory::mem_allocator_;
memory::allocator::~allocator()
{
this->clear();
}
void memory::allocator::clear()
{
std::lock_guard<std::mutex> _(this->mutex_);
for (auto& data : this->pool_)
{
memory::free(data);
}
this->pool_.clear();
}
void memory::allocator::free(void* data)
{
std::lock_guard<std::mutex> _(this->mutex_);
const auto j = std::find(this->pool_.begin(), this->pool_.end(), data);
if (j != this->pool_.end())
{
memory::free(data);
this->pool_.erase(j);
}
}
void memory::allocator::free(const void* data)
{
this->free(const_cast<void*>(data));
}
void* memory::allocator::allocate(const size_t length)
{
std::lock_guard<std::mutex> _(this->mutex_);
const auto data = memory::allocate(length);
this->pool_.push_back(data);
return data;
}
bool memory::allocator::empty() const
{
return this->pool_.empty();
}
void* memory::allocate(const size_t length)
{
return calloc(length, 1);
}
void memory::free(void* data)
{
if (data)
{
::free(data);
}
}
void memory::free(const void* data)
{
free(const_cast<void*>(data));
}
bool memory::is_set(const void* mem, const char chr, const size_t length)
{
const auto mem_arr = static_cast<const char*>(mem);
for (size_t i = 0; i < length; ++i)
{
if (mem_arr[i] != chr)
{
return false;
}
}
return true;
}
memory::allocator* memory::get_allocator()
{
return &memory::mem_allocator_;
}
std::uint8_t* find_sig(LPCSTR module_name, const std::string& byte_array)
{
HMODULE module = GetModuleHandleA(module_name);
if (!module)
return nullptr;
static const auto pattern_to_byte = [](const char* pattern)
{
auto bytes = std::vector<int>{};
const auto start = const_cast<char*>(pattern);
const auto end = const_cast<char*>(pattern) + std::strlen(pattern);
for (auto current = start; current < end; ++current)
{
if (*current == '?')
{
++current;
if (*current == '?')
++current;
bytes.push_back(-1);
}
else
{
bytes.push_back(std::strtoul(current, &current, 16));
}
}
return bytes;
};
const auto dos_header = reinterpret_cast<PIMAGE_DOS_HEADER>(module);
const auto nt_headers =
reinterpret_cast<PIMAGE_NT_HEADERS>(reinterpret_cast<std::uint8_t*>(module) + dos_header->e_lfanew);
const auto size_of_image = nt_headers->OptionalHeader.SizeOfImage;
const auto pattern_bytes = pattern_to_byte(byte_array.c_str());
const auto scan_bytes = reinterpret_cast<std::uint8_t*>(module);
const auto pattern_size = pattern_bytes.size();
const auto pattern_data = pattern_bytes.data();
for (auto i = 0ul; i < size_of_image - pattern_size; ++i)
{
auto found = true;
for (auto j = 0ul; j < pattern_size; ++j)
{
if (scan_bytes[i + j] == pattern_data[j] || pattern_data[j] == -1)
continue;
found = false;
break;
}
if (!found)
continue;
return &scan_bytes[i];
}
return nullptr;
}
signature::signature(const std::string& sig)
{
this->imported = false;
this->sig = sig;
this->pointer = 0;
}
signature signature::import(const char* module_name)
{
this->imported = true;
this->pointer = (uint64_t)find_sig(module_name, this->sig);
return *this;
}
signature signature::add(uint32_t value)
{
if (!this->imported)
*this = this->import();
this->pointer += value;
return *this;
}
signature signature::sub(uint32_t value)
{
if (!this->imported)
*this = this->import();
this->pointer -= value;
return *this;
}
signature signature::dump(const std::string& func_name)
{
if (!this->imported)
*this = this->import();
return *this;
}
signature signature::instruction(uint32_t offset)
{
if (!this->imported)
*this = this->import();
this->pointer = *(int*)(this->pointer + offset) + this->pointer;
return *this;
}
#ifdef _WIN64
uint64_t signature::GetPointer()
{
if (!this->imported)
*this = this->import();
return this->pointer;
}
#else
uint32_t signature::GetPointer()
{
if (!this->imported)
*this = this->import();
return this->pointer;
}
#endif
static inline void memcpy_(void* _Dst, void const* _Src, size_t _Size)
{
auto csrc = (char*)_Src;
auto cdest = (char*)_Dst;
for (int i = 0; i < _Size; i++)
{
cdest[i] = csrc[i];
}
}
void swap_virtual_table(void* obj, uint32_t index, void* func)
{
auto currVt = *(void**)(obj);
auto vtable = *(void***)(obj);
int i = 0;
for (; vtable[i]; i++)
__noop();
auto newVt = new uintptr_t[i];
memcpy_(newVt, currVt, i * 0x8);
newVt[index] = (uintptr_t)func;
*(uintptr_t**)(obj) = newVt;
}
bool get_key_state(int key, int flag)
{
clock_t c = clock();
static BYTE btKeyState[0x100] = { 0 };
static clock_t clockKeyState[0x100] = { c };
bool r = false;
BYTE btState = (GetAsyncKeyState(key) & 0x8000U) >> 0xF;
short wDelay = btKeyState[key] & 2 ? 0x50 : 0x200;
if (btState) {
if (flag & 1) {
if (!(btKeyState[key] & 1) || c - clockKeyState[key] > wDelay)
{
if ((btKeyState[key] & 3) == 1)
btKeyState[key] |= 2;
r = true;
clockKeyState[key] = c;
}
}
else
r = true;
}
else
btKeyState[key] &= ~(2);
btKeyState[key] ^= (-btState ^ btKeyState[key]) & 1;
return r;
}
+105
View File
@@ -0,0 +1,105 @@
#pragma once
#include <Windows.h>
#include <mutex>
#include <vector>
std::uint8_t* find_sig(LPCSTR module_name, const std::string& byte_array);
class memory final
{
public:
class allocator final
{
public:
~allocator();
void clear();
void free(void* data);
void free(const void* data);
void* allocate(size_t length);
template <typename T>
T* allocate()
{
return this->allocate_array<T>(1);
}
template <typename T>
T* allocate_array(const size_t count = 1)
{
auto base = static_cast<T*>(this->allocate(count * sizeof(T)));
T();
return base;
}
bool empty() const;
private:
std::mutex mutex_;
std::vector<void*> pool_;
};
static void* allocate(size_t length);
template <typename T>
static inline T* allocate()
{
return allocate_array<T>(1);
}
template <typename T>
static inline T* allocate_array(const size_t count = 1)
{
return static_cast<T*>(allocate(count * sizeof(T)));
}
static void free(void* data);
static void free(const void* data);
static bool is_set(const void* mem, char chr, size_t length);
static allocator* get_allocator();
private:
static allocator mem_allocator_;
};
class signature
{
public:
signature(const std::string& sig);
signature import(const char* module_name = NULL);
signature add(uint32_t offset);
signature sub(uint32_t offset);
signature instruction(uint32_t offset);
signature dump(const std::string& func_name);
#ifdef _WIN64
uint64_t GetPointer();
#else
uint32_t GetPointer();
#endif
private:
#ifdef _WIN64
uint64_t pointer;
#else
uint32_t pointer;
#endif
bool imported;
std::string sig;
};
void swap_virtual_table(void* obj, uint32_t index, void* func);
bool get_key_state(int key, int flag = 1);
+5
View File
@@ -0,0 +1,5 @@
// pch.cpp: 与预编译标头对应的源文件
#include "pch.h"
// 当使用预编译的头时,需要使用此源文件,编译才能成功。
+13
View File
@@ -0,0 +1,13 @@
// pch.h: 这是预编译标头文件。
// 下方列出的文件仅编译一次,提高了将来生成的生成性能。
// 这还将影响 IntelliSense 性能,包括代码完成和许多代码浏览功能。
// 但是,如果此处列出的文件中的任何一个在生成之间有更新,它们全部都将被重新编译。
// 请勿在此处添加要频繁更新的文件,这将使得性能优势无效。
#ifndef PCH_H
#define PCH_H
// 添加要在此处预编译的标头
#include "framework.h"
#endif //PCH_H
+98
View File
@@ -0,0 +1,98 @@
// https://github.com/t-mat/pseudo-version-dll/
#include "pch.h"
#include "plugin.hpp"
#include <functional>
#include <algorithm>
#include <array>
#include <vector>
static std::vector<HMODULE> hModules;
struct Path : std::array<wchar_t, MAX_PATH+1> {
Path() : Path {L""} {}
Path(const wchar_t* s) { wcsncpy_s(data(), size(), s, size()); }
operator const wchar_t*() const { return data(); }
static Path make(const wchar_t* fmt, ...) {
va_list args;
va_start(args, fmt);
Path path;
vswprintf_s(path.data(), path.size(), fmt, args);
return path;
}
};
Path getModuleFilenameW(HMODULE h) {
std::array<wchar_t, MAX_PATH> moduleName;
GetModuleFileNameW(h, moduleName.data(), static_cast<UINT>(moduleName.size()));
return Path(moduleName.data());
}
void recursiveFileEnumerator(const wchar_t* path, const std::function<void(const wchar_t*)>& func) {
WIN32_FIND_DATAW wfd;
HANDLE h = FindFirstFileW(Path::make(L"%s\\*.*", path), &wfd);
if(h == INVALID_HANDLE_VALUE) {
return;
}
do {
const auto a = wfd.dwFileAttributes;
const auto newPath = Path::make(L"%s\\%s", path, wfd.cFileName);
if(a & FILE_ATTRIBUTE_DIRECTORY) {
if(wfd.cFileName[0] != L'.') {
recursiveFileEnumerator(newPath, func);
}
} else if((a & (FILE_ATTRIBUTE_ARCHIVE | FILE_ATTRIBUTE_NORMAL)) != 0) {
if(wfd.cFileName[0] != L'.') {
func(newPath);
}
}
} while(FindNextFileW(h, &wfd));
}
void loadPluginDlls() {
unloadPluginDlls();
Path pluginsPath = Path::make(L"CSharpLoader\\Plugins");
wprintf(L"loadPluginDlls : pluginsPath=[%s]\n", pluginsPath.data());
std::vector<Path> pluginFilenames;
recursiveFileEnumerator(pluginsPath, [&](const wchar_t* path) {
const wchar_t* ext = L".dll";
const auto extLen = wcslen(ext);
const auto pathLen = wcslen(path);
if(pathLen < extLen) {
return;
}
if(_wcsicmp(path + pathLen - extLen, ext) != 0) {
return;
}
pluginFilenames.push_back(path);
});
std::sort(pluginFilenames.begin(), pluginFilenames.end());
for(const auto& pluginFilename : pluginFilenames) {
auto* filename = static_cast<const wchar_t*>(pluginFilename);
auto hm = LoadLibraryW(filename);
if(hm == nullptr) {
wprintf(L"loadPluginDlls : failed to load %s\n", filename);
} else {
hModules.push_back(hm);
wprintf(L"loadPluginDlls : load %s\n", filename);
}
}
}
void unloadPluginDlls() {
for(auto it = hModules.rbegin(); it != hModules.rend(); ++it) {
const auto h = *it;
if(h != nullptr) {
wprintf(L"unloadPluginDlls : unload %s (hModules=0x%p)", getModuleFilenameW(h).data(), h);
FreeLibrary(h);
}
}
hModules.clear();
}
+4
View File
@@ -0,0 +1,4 @@
#pragma once
void loadPluginDlls();
void unloadPluginDlls();
+166
View File
@@ -0,0 +1,166 @@
#include "pch.h"
#pragma unmanaged
#include "version_dll.hpp"
#include <windows.h>
#include <array>
#include <stdio.h>
///////////////////////////////////////////////////////////////
static HMODULE hModuleDll = nullptr;
static const wchar_t dllFname[] = L"version";
bool init_version_dll() {
deinit_version_dll();
wchar_t systemDirectory[MAX_PATH]{0};
GetSystemDirectoryW(systemDirectory, MAX_PATH);
wchar_t fullpathDllName[MAX_PATH]{0};
swprintf_s(fullpathDllName, MAX_PATH, L"%s\\%s.dll", systemDirectory, dllFname);
if ((hModuleDll = LoadLibraryW(fullpathDllName)) == NULL) {
return false;
}
wprintf(L"load %s success\n", dllFname);
return true;
}
bool deinit_version_dll() {
if(hModuleDll != nullptr) {
FreeLibrary(hModuleDll);
hModuleDll = nullptr;
}
return true;
}
///////////////////////////////////////////////////////////////
template<typename T>
void setup(T*& funcPtr, const char* funcName) {
if(funcPtr != nullptr) {
return;
}
funcPtr = reinterpret_cast<T*>(GetProcAddress(hModuleDll, funcName));
}
#define D(funcname, ...) \
static decltype(funcname)* p; \
setup(p, #funcname); \
return p(__VA_ARGS__); \
__pragma(comment(linker, "/EXPORT:" __FUNCTION__))
// Function Name : GetFileVersionInfoA
// Ordinal : 1 (0x1)
extern "C" BOOL WINAPI GetFileVersionInfoA (LPCSTR lptstrFilename, DWORD dwHandle, DWORD dwLen, LPVOID lpData) {
D(GetFileVersionInfoA, lptstrFilename, dwHandle, dwLen, lpData);
}
// Function Name : GetFileVersionInfoByHandle
// Ordinal : 2 (0x2)
extern "C" int WINAPI GetFileVersionInfoByHandle (int hMem, LPCWSTR lpFileName, int v2, int v3) {
D(GetFileVersionInfoByHandle, hMem, lpFileName, v2, v3);
}
// Function Name : GetFileVersionInfoExA
// Ordinal : 3 (0x3)
extern "C" BOOL WINAPI GetFileVersionInfoExA (DWORD dwFlags, LPCSTR lpwstrFilename, DWORD dwHandle, DWORD dwLen, LPVOID lpData) {
D(GetFileVersionInfoExA, dwFlags, lpwstrFilename, dwHandle, dwLen, lpData);
}
// Function Name : GetFileVersionInfoExW
// Ordinal : 4 (0x4)
extern "C" BOOL WINAPI GetFileVersionInfoExW (DWORD dwFlags, LPCWSTR lpwstrFilename, DWORD dwHandle, DWORD dwLen, LPVOID lpData) {
D(GetFileVersionInfoExW, dwFlags, lpwstrFilename, dwHandle, dwLen, lpData);
}
// Function Name : GetFileVersionInfoSizeA
// Ordinal : 5 (0x5)
extern "C" DWORD WINAPI GetFileVersionInfoSizeA (LPCSTR lptstrFilename, LPDWORD lpdwHandle) {
D(GetFileVersionInfoSizeA, lptstrFilename, lpdwHandle);
}
// Function Name : GetFileVersionInfoSizeExA
// Ordinal : 6 (0x6)
extern "C" DWORD WINAPI GetFileVersionInfoSizeExA (DWORD dwFlags, LPCSTR lpwstrFilename, LPDWORD lpdwHandle) {
D(GetFileVersionInfoSizeExA, dwFlags, lpwstrFilename, lpdwHandle);
}
// Function Name : GetFileVersionInfoSizeExW
// Ordinal : 7 (0x7)
extern "C" DWORD WINAPI GetFileVersionInfoSizeExW (DWORD dwFlags, LPCWSTR lpwstrFilename, LPDWORD lpdwHandle) {
D(GetFileVersionInfoSizeExW, dwFlags, lpwstrFilename, lpdwHandle);
}
// Function Name : GetFileVersionInfoSizeW
// Ordinal : 8 (0x8)
extern "C" DWORD WINAPI GetFileVersionInfoSizeW (LPCWSTR lptstrFilename, LPDWORD lpdwHandle) {
D(GetFileVersionInfoSizeW, lptstrFilename, lpdwHandle);
}
// Function Name : GetFileVersionInfoW
// Ordinal : 9 (0x9)
extern "C" BOOL WINAPI GetFileVersionInfoW (LPCWSTR lptstrFilename, DWORD dwHandle, DWORD dwLen, LPVOID lpData) {
D(GetFileVersionInfoW, lptstrFilename, dwHandle, dwLen, lpData);
}
// Function Name : VerFindFileA
// Ordinal : 10 (0xa)
extern "C" DWORD WINAPI VerFindFileA (DWORD uFlags, LPCSTR szFileName, LPCSTR szWinDir, LPCSTR szAppDir, LPSTR szCurDir, PUINT lpuCurDirLen, LPSTR szDestDir, PUINT lpuDestDirLen) {
D(VerFindFileA, uFlags, szFileName, szWinDir, szAppDir, szCurDir, lpuCurDirLen, szDestDir, lpuDestDirLen);
}
// Function Name : VerFindFileW
// Ordinal : 11 (0xb)
extern "C" DWORD WINAPI VerFindFileW (DWORD uFlags, LPCWSTR szFileName, LPCWSTR szWinDir, LPCWSTR szAppDir, LPWSTR szCurDir, PUINT lpuCurDirLen, LPWSTR szDestDir, PUINT lpuDestDirLen) {
D(VerFindFileW, uFlags, szFileName, szWinDir, szAppDir, szCurDir, lpuCurDirLen, szDestDir, lpuDestDirLen);
}
// Function Name : VerInstallFileA
// Ordinal : 12 (0xc)
extern "C" DWORD WINAPI VerInstallFileA (DWORD uFlags, LPCSTR szSrcFileName, LPCSTR szDestFileName, LPCSTR szSrcDir, LPCSTR szDestDir, LPCSTR szCurDir, LPSTR szTmpFile, PUINT lpuTmpFileLen) {
D(VerInstallFileA, uFlags, szSrcFileName, szDestFileName, szSrcDir, szDestDir, szCurDir, szTmpFile, lpuTmpFileLen);
}
// Function Name : VerInstallFileW
// Ordinal : 13 (0xd)
extern "C" DWORD WINAPI VerInstallFileW (DWORD uFlags, LPCWSTR szSrcFileName, LPCWSTR szDestFileName, LPCWSTR szSrcDir, LPCWSTR szDestDir, LPCWSTR szCurDir, LPWSTR szTmpFile, PUINT lpuTmpFileLen) {
D(VerInstallFileW, uFlags, szSrcFileName, szDestFileName, szSrcDir, szDestDir, szCurDir, szTmpFile, lpuTmpFileLen);
}
// Function Name : VerLanguageNameA
// Ordinal : 14 (0xe)
extern "C" DWORD WINAPI VerLanguageNameA (DWORD wLang, LPSTR szLang, DWORD cchLang) {
D(VerLanguageNameA, wLang, szLang, cchLang);
}
// Function Name : VerLanguageNameW
// Ordinal : 15 (0xf)
extern "C" DWORD WINAPI VerLanguageNameW (DWORD wLang, LPWSTR szLang, DWORD cchLang) {
D(VerLanguageNameW, wLang, szLang, cchLang);
}
// Function Name : VerQueryValueA
// Ordinal : 16 (0x10)
extern "C" BOOL WINAPI VerQueryValueA (LPCVOID pBlock, LPCSTR lpSubBlock, LPVOID * lplpBuffer, PUINT puLen) {
D(VerQueryValueA, pBlock, lpSubBlock, lplpBuffer, puLen);
}
// Function Name : VerQueryValueW
// Ordinal : 17 (0x11)
extern "C" BOOL WINAPI VerQueryValueW (LPCVOID pBlock, LPCWSTR lpSubBlock, LPVOID * lplpBuffer, PUINT puLen) {
D(VerQueryValueW, pBlock, lpSubBlock, lplpBuffer, puLen);
}
+4
View File
@@ -0,0 +1,4 @@
#pragma once
bool init_version_dll();
bool deinit_version_dll();