Use winMain instead of main

This commit is contained in:
2024-11-22 08:45:36 +08:00
parent ae89581628
commit 898bc4c433
4 changed files with 22 additions and 17 deletions

View File

@@ -13,5 +13,4 @@ set(DETOURS_LIB "${CMAKE_CURRENT_SOURCE_DIR}/lib/detours.lib")
add_library(jewena_patch SHARED dllmain.cpp) add_library(jewena_patch SHARED dllmain.cpp)
target_link_libraries(jewena_patch "${DETOURS_LIB}") target_link_libraries(jewena_patch "${DETOURS_LIB}")
add_executable(jewena-chs main.cpp) add_executable(jewena-chs WIN32 main.cpp winres.rc)
target_link_libraries(jewena-chs "${DETOURS_LIB}")

BIN
ICON.ico Normal file
View File

Binary file not shown.

After

Width:  |  Height:  |  Size: 148 KiB

View File

@@ -1,14 +1,20 @@
#include <windows.h> #include <windows.h>
#include <iostream> #include <stdio.h>
int main() { void ShowErrorMsg(LPCWSTR text) {
wchar_t* buf[1024];
_swprintf((wchar_t *const)buf, L"%s%i", text, GetLastError());
MessageBoxW(nullptr, (LPCWSTR)buf, L"错误消息", MB_OK);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
// 要启动的进程名 // 要启动的进程名
const char* processName = "jewena.exe"; const wchar_t* processName = L"jewena.exe";
// 要注入的 DLL 路径 // 要注入的 DLL 路径
const char* dllPath = "jewena_patch.dll"; const wchar_t* dllPath = L"jewena_patch.dll";
// 启动进程 // 启动进程
STARTUPINFOA si; STARTUPINFOW si;
PROCESS_INFORMATION pi; PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si)); ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi)); ZeroMemory(&pi, sizeof(pi));
@@ -16,23 +22,25 @@ int main() {
si.cb = sizeof(si); si.cb = sizeof(si);
// 创建新进程 // 创建新进程
if (!CreateProcessA(processName, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi)) { if (!CreateProcessW((LPCWSTR)processName, nullptr, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi)) {
std::cerr << "CreateProcess failed: " << GetLastError() << std::endl; ShowErrorMsg(L"CreateProcessW failed: ");
return 1; return 1;
} }
size_t memSize = (wcslen(dllPath) + 1) * sizeof(wchar_t);
// 在新进程中分配内存以存放 DLL 路径 // 在新进程中分配内存以存放 DLL 路径
LPVOID pDllPath = VirtualAllocEx(pi.hProcess, NULL, strlen(dllPath) + 1, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE); LPVOID pDllPath = VirtualAllocEx(pi.hProcess, NULL, memSize, MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (!pDllPath) { if (!pDllPath) {
std::cerr << "VirtualAllocEx failed: " << GetLastError() << std::endl; ShowErrorMsg(L"VirtualAllocEx failed: ");
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
return 1; return 1;
} }
// 将 DLL 路径写入新进程的内存 // 将 DLL 路径写入新进程的内存
if (!WriteProcessMemory(pi.hProcess, pDllPath, (LPVOID)dllPath, strlen(dllPath) + 1, NULL)) { if (!WriteProcessMemory(pi.hProcess, pDllPath, (LPVOID)dllPath, memSize, NULL)) {
std::cerr << "WriteProcessMemory failed: " << GetLastError() << std::endl; ShowErrorMsg(L"WriteProcessMemory failed: ");
VirtualFreeEx(pi.hProcess, pDllPath, 0, MEM_RELEASE); VirtualFreeEx(pi.hProcess, pDllPath, 0, MEM_RELEASE);
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
@@ -40,9 +48,9 @@ int main() {
} }
// 创建远程线程以加载 DLL // 创建远程线程以加载 DLL
HANDLE hThread = CreateRemoteThread(pi.hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryA"), pDllPath, 0, NULL); HANDLE hThread = CreateRemoteThread(pi.hProcess, NULL, 0, (LPTHREAD_START_ROUTINE)GetProcAddress(GetModuleHandleA("kernel32.dll"), "LoadLibraryW"), pDllPath, 0, NULL);
if (!hThread) { if (!hThread) {
std::cerr << "CreateRemoteThread failed: " << GetLastError() << std::endl; ShowErrorMsg(L"CreateRemoteThread failed: ");
VirtualFreeEx(pi.hProcess, pDllPath, 0, MEM_RELEASE); VirtualFreeEx(pi.hProcess, pDllPath, 0, MEM_RELEASE);
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
@@ -58,7 +66,5 @@ int main() {
ResumeThread(pi.hThread); // 恢复新进程的执行 ResumeThread(pi.hThread); // 恢复新进程的执行
CloseHandle(pi.hProcess); CloseHandle(pi.hProcess);
CloseHandle(pi.hThread); CloseHandle(pi.hThread);
std::cout << "DLL injected successfully." << std::endl;
return 0; return 0;
} }

BIN
winres.rc Normal file
View File

Binary file not shown.