diff --git a/CMakeLists.txt b/CMakeLists.txt index 7844e74..9969058 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,5 +13,4 @@ set(DETOURS_LIB "${CMAKE_CURRENT_SOURCE_DIR}/lib/detours.lib") add_library(jewena_patch SHARED dllmain.cpp) target_link_libraries(jewena_patch "${DETOURS_LIB}") -add_executable(jewena-chs main.cpp) -target_link_libraries(jewena-chs "${DETOURS_LIB}") +add_executable(jewena-chs WIN32 main.cpp winres.rc) diff --git a/ICON.ico b/ICON.ico new file mode 100644 index 0000000..00e2831 Binary files /dev/null and b/ICON.ico differ diff --git a/main.cpp b/main.cpp index 1363ff1..ad6c067 100644 --- a/main.cpp +++ b/main.cpp @@ -1,14 +1,20 @@ #include -#include +#include -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 路径 - const char* dllPath = "jewena_patch.dll"; + const wchar_t* dllPath = L"jewena_patch.dll"; // 启动进程 - STARTUPINFOA si; + STARTUPINFOW si; PROCESS_INFORMATION pi; ZeroMemory(&si, sizeof(si)); ZeroMemory(&pi, sizeof(pi)); @@ -16,23 +22,25 @@ int main() { si.cb = sizeof(si); // 创建新进程 - if (!CreateProcessA(processName, NULL, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi)) { - std::cerr << "CreateProcess failed: " << GetLastError() << std::endl; + if (!CreateProcessW((LPCWSTR)processName, nullptr, NULL, NULL, FALSE, CREATE_SUSPENDED, NULL, NULL, &si, &pi)) { + ShowErrorMsg(L"CreateProcessW failed: "); return 1; } + size_t memSize = (wcslen(dllPath) + 1) * sizeof(wchar_t); + // 在新进程中分配内存以存放 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) { - std::cerr << "VirtualAllocEx failed: " << GetLastError() << std::endl; + ShowErrorMsg(L"VirtualAllocEx failed: "); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); return 1; } // 将 DLL 路径写入新进程的内存 - if (!WriteProcessMemory(pi.hProcess, pDllPath, (LPVOID)dllPath, strlen(dllPath) + 1, NULL)) { - std::cerr << "WriteProcessMemory failed: " << GetLastError() << std::endl; + if (!WriteProcessMemory(pi.hProcess, pDllPath, (LPVOID)dllPath, memSize, NULL)) { + ShowErrorMsg(L"WriteProcessMemory failed: "); VirtualFreeEx(pi.hProcess, pDllPath, 0, MEM_RELEASE); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); @@ -40,9 +48,9 @@ int main() { } // 创建远程线程以加载 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) { - std::cerr << "CreateRemoteThread failed: " << GetLastError() << std::endl; + ShowErrorMsg(L"CreateRemoteThread failed: "); VirtualFreeEx(pi.hProcess, pDllPath, 0, MEM_RELEASE); CloseHandle(pi.hProcess); CloseHandle(pi.hThread); @@ -58,7 +66,5 @@ int main() { ResumeThread(pi.hThread); // 恢复新进程的执行 CloseHandle(pi.hProcess); CloseHandle(pi.hThread); - - std::cout << "DLL injected successfully." << std::endl; return 0; } diff --git a/winres.rc b/winres.rc new file mode 100644 index 0000000..ce7324c Binary files /dev/null and b/winres.rc differ