Skip to content

Commit c37177a

Browse files
committed
add wchar support
Signed-off-by: xaxys <[email protected]>
1 parent 9e864c4 commit c37177a

File tree

8 files changed

+75
-68
lines changed

8 files changed

+75
-68
lines changed

inc/inject.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ extern "C" {
77
#endif
88

99
bool grant_se_debug_privilege();
10-
bool inject_dll(DWORD pid, const char *dll_path);
11-
DWORD find_pid_by_name(const char *name);
12-
HMODULE find_module_handle_from_pid(DWORD pid, const char *module_name);
10+
bool inject_dll(DWORD pid, const wchar_t *dll_path);
11+
DWORD find_pid_by_name(const wchar_t *name);
12+
HMODULE find_module_handle_from_pid(DWORD pid, const wchar_t *module_name);
1313
bool remove_module(DWORD pid, HMODULE module_handle);
1414

1515
#ifdef __cplusplus

injciv6-gui/utils/inject.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,11 @@ func IsAdmin() bool {
7676
}
7777

7878
func IsCiv6Injected() InjectStatus {
79-
dllCstr := C.CString("hookdll64.dll")
80-
defer C.free(unsafe.Pointer(dllCstr))
79+
dllName := "hookdll64.dll"
80+
dllNameW, _ := syscall.UTF16FromString(dllName)
81+
dllCstrW := (*C.wchar_t)(unsafe.Pointer(&dllNameW[0]))
8182
pid := C.get_civ6_proc()
82-
handle := C.find_module_handle_from_pid(pid, dllCstr)
83+
handle := C.find_module_handle_from_pid(pid, dllCstrW)
8384
if C.is_null(handle) {
8485
return InjectStatusNotInjected
8586
}

injciv6-gui/utils/inject_util.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ bool runas_admin(LPCWSTR exename) {
2222
}
2323

2424
DWORD get_civ6_dx11_proc() {
25-
return find_pid_by_name("CivilizationVI.exe");
25+
return find_pid_by_name(L"CivilizationVI.exe");
2626
}
2727

2828
DWORD get_civ6_dx12_proc() {
29-
return find_pid_by_name("CivilizationVI_DX12.exe");
29+
return find_pid_by_name(L"CivilizationVI_DX12.exe");
3030
}
3131

3232
DWORD get_civ6_proc() {

injciv6/civ6remove.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@
44

55
int main(int argc, char *argv[])
66
{
7+
wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
78
bool isadmin = IsUserAnAdmin();
89
int msgres = 0;
910
if (!isadmin) {
1011
retry_runas:
11-
if (runas_admin(argv[0])) // 成功运行就退出自己
12+
if (runas_admin(wargv[0])) // 成功运行就退出自己
1213
return 0;
1314
msgres = MessageBoxW(0, L"请允许管理员权限", L"错误", MB_ICONERROR | MB_RETRYCANCEL);
1415
if (msgres == IDRETRY)
@@ -26,7 +27,7 @@ int main(int argc, char *argv[])
2627
MessageBoxW(0, L"找不到游戏进程", L"错误", MB_ICONERROR);
2728
return 0;
2829
}
29-
module_handle = find_module_handle_from_pid(pid, "hookdll64.dll");
30+
module_handle = find_module_handle_from_pid(pid, L"hookdll64.dll");
3031
if (module_handle == 0) {
3132
MessageBoxW(0, L"当前没有注入DLL", L"错误", MB_ICONERROR);
3233
return 0;
@@ -35,4 +36,6 @@ int main(int argc, char *argv[])
3536
MessageBoxW(0, L"成功移除DLL!", L"成功", MB_OK);
3637
else
3738
msgres = MessageBoxW(0, L"移除失败", L"错误", MB_ICONERROR);
39+
40+
return 0;
3841
}

injciv6/injciv6.cpp

+11-10
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,22 @@
55

66
int main(int argc, char *argv[])
77
{
8+
wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
89
bool silence = false;
910
for (int i = 1; i < argc; i++) {
1011
if (strcmp(argv[i], "-s") == 0) silence = true;
1112
}
12-
char dll_path[MAX_PATH];
13-
strcpy(dll_path, argv[0]);
14-
int pos = strlen(dll_path) - 1;
13+
wchar_t dll_path[MAX_PATH];
14+
wcscpy_s(dll_path, MAX_PATH, wargv[0]);
15+
int pos = wcslen(dll_path);
1516
while (pos > 0) {
16-
if (dll_path[pos] == '\\')
17+
if (dll_path[pos] == L'\\')
1718
break;
1819
pos--;
1920
}
20-
dll_path[pos + 1] = '\0';
21-
strcat(dll_path, "hookdll64.dll");
22-
FILE *fp = fopen(dll_path, "rb");
21+
dll_path[pos + 1] = L'\0';
22+
wcscat_s(dll_path, MAX_PATH, L"hookdll64.dll");
23+
FILE *fp = _wfopen(dll_path, L"rb");
2324
if (fp == NULL) {
2425
MessageBoxW(0, L"找不到hookdll64.dll", L"错误", MB_ICONERROR);
2526
return 0;
@@ -44,7 +45,7 @@ int main(int argc, char *argv[])
4445
return 0;
4546
}
4647
bool reinj = false;
47-
HMODULE dll = find_module_handle_from_pid(civ6pid, "hookdll64.dll");
48+
HMODULE dll = find_module_handle_from_pid(civ6pid, L"hookdll64.dll");
4849
if (dll != 0) {
4950
msgres = IDYES;
5051
if (!silence)
@@ -53,7 +54,7 @@ int main(int argc, char *argv[])
5354
return 0;
5455
if (!isadmin) {
5556
retry_runas_silence:
56-
if (runas_admin(argv[0], "-s")) // 成功运行就退出自己
57+
if (runas_admin(wargv[0], L"-s")) // 成功运行就退出自己
5758
return 0;
5859
msgres = MessageBoxW(0, L"请允许管理员权限", L"错误", MB_ICONERROR | MB_RETRYCANCEL);
5960
if (msgres == IDRETRY)
@@ -92,7 +93,7 @@ int main(int argc, char *argv[])
9293
msgres = MessageBoxW(0, L"注入失败,是否以管理员权限重试?", L"错误", MB_ICONERROR | MB_RETRYCANCEL);
9394
if (msgres == IDRETRY) {
9495
retry_runas:
95-
if (runas_admin(argv[0])) // 成功运行就退出自己
96+
if (runas_admin(wargv[0])) // 成功运行就退出自己
9697
return 0;
9798
msgres = MessageBoxW(0, L"请在弹出的窗口中点击“是”", L"错误", MB_ICONERROR | MB_RETRYCANCEL);
9899
if (msgres == IDRETRY)

injciv6/injciv6.h

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
#include <shellapi.h>
44
#include "inject.h"
55

6-
inline bool runas_admin(const char *exename, const char* args = NULL)
6+
inline bool runas_admin(const wchar_t *exename, const wchar_t* args = NULL)
77
{
8-
SHELLEXECUTEINFOA sei;
8+
SHELLEXECUTEINFOW sei;
99
memset(&sei, 0, sizeof(sei));
1010
sei.cbSize = sizeof(sei);
1111
sei.fMask = SEE_MASK_FLAG_DDEWAIT | SEE_MASK_FLAG_NO_UI;
12-
sei.lpVerb = "runas";
12+
sei.lpVerb = L"runas";
1313
sei.lpFile = exename;
1414
sei.nShow = SW_SHOWNORMAL;
1515
sei.lpParameters = args;
16-
return ShellExecuteExA(&sei);
16+
return ShellExecuteExW(&sei);
1717
}
1818

1919
inline DWORD get_civ6_proc()
2020
{
21-
DWORD pid = find_pid_by_name("CivilizationVI.exe");
21+
DWORD pid = find_pid_by_name(L"CivilizationVI.exe");
2222
if (pid == 0)
23-
pid = find_pid_by_name("CivilizationVI_DX12.exe");
23+
pid = find_pid_by_name(L"CivilizationVI_DX12.exe");
2424
return pid;
2525
}

injector/injector.cpp

+27-26
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@
55
#include <windows.h>
66

77
#ifdef _CPU_X64
8-
#define DLL_NAME "hookdll64.dll"
8+
#define DLL_NAME L"hookdll64.dll"
99
#endif
1010
#ifdef _CPU_X86
11-
#define DLL_NAME "hookdll32.dll"
11+
#define DLL_NAME L"hookdll32.dll"
1212
#endif
1313

1414
void write_help()
@@ -38,12 +38,12 @@ void format_error()
3838
write_help();
3939
}
4040

41-
bool doinject(const char *dllpath, char mode, const char *param)
41+
bool doinject(const wchar_t *dllpath, wchar_t mode, const wchar_t *param)
4242
{
43-
if (!param || *param == '\0')
43+
if (!param || *param == L'\0')
4444
return false;
45-
if (mode == 'i') {
46-
DWORD pid = atoi(param);
45+
if (mode == L'i') {
46+
DWORD pid = _wtoi(param);
4747
if (pid == 0) {
4848
printf("\"%s\" is not a number\n", param);
4949
return false;
@@ -54,7 +54,7 @@ bool doinject(const char *dllpath, char mode, const char *param)
5454
}
5555
return inject_dll(pid, dllpath);
5656
}
57-
else if (mode == 'x') {
57+
else if (mode == L'x') {
5858
DWORD pid = find_pid_by_name(param);
5959
if (pid == 0) {
6060
printf("Can not find process by \"%s\"\n", param);
@@ -67,44 +67,45 @@ bool doinject(const char *dllpath, char mode, const char *param)
6767

6868
int main(int argc, char *argv[])
6969
{
70-
char dll_path[MAX_PATH];
71-
GetModuleFileNameA(NULL, dll_path, MAX_PATH);
72-
char *pos = strrchr(dll_path, '\\');
73-
*(pos + 1) = '\0';
74-
strcat(pos, DLL_NAME);
75-
FILE *fp = fopen(dll_path, "rb");
70+
wchar_t **wargv = CommandLineToArgvW(GetCommandLineW(), &argc);
71+
wchar_t dll_path[MAX_PATH];
72+
GetModuleFileNameW(NULL, dll_path, MAX_PATH);
73+
wchar_t *pos = wcsrchr(dll_path, L'\\');
74+
*(pos + 1) = L'\0';
75+
wcscat(pos, DLL_NAME);
76+
FILE *fp = _wfopen(dll_path, L"rb");
7677
if (fp == NULL) {
7778
printf("Can not find DLL \"%s\"\n", dll_path);
7879
exit(1);
7980
}
8081
fclose(fp);
8182
argc--;
82-
argv++;
83+
wargv++;
8384
if (argc == 0)
8485
write_help();
8586
bool result = false;
8687
while (argc--) {
87-
if (**argv != '-') {
88+
if (**wargv != L'-') {
8889
format_error();
8990
return 0;
9091
}
91-
(*argv)++;
92-
switch (**argv) {
93-
case 'h': write_help(); break;
94-
case 's': grant_privilege(); break;
95-
case 'i':
96-
case 'x':
92+
(*wargv)++;
93+
switch (**wargv) {
94+
case L'h': write_help(); break;
95+
case L's': grant_privilege(); break;
96+
case L'i':
97+
case L'x':
9798
{
98-
char mode = **argv;
99-
(*argv)++;
100-
if (**argv == '\0' || **argv != '=')
99+
wchar_t mode = **wargv;
100+
(*wargv)++;
101+
if (**wargv == L'\0' || **wargv != L'=')
101102
format_error();
102-
result = doinject(dll_path, mode, *argv + 1);
103+
result = doinject(dll_path, mode, *wargv + 1);
103104
break;
104105
}
105106
default: format_error(); break;
106107
}
107-
argv++;
108+
wargv++;
108109
}
109110
if (result) {
110111
printf("Inject OK\n");

src/inject.cpp

+17-16
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ bool grant_se_debug_privilege()
3232
return true;
3333
}
3434

35-
bool inject_dll(DWORD pid, const char *dll_path)
35+
bool inject_dll(DWORD pid, const wchar_t *dll_path)
3636
{
37-
int path_len = strlen(dll_path) + 1;
37+
int path_len = (wcslen(dll_path) + 1) * sizeof(wchar_t);
3838
HANDLE hproc = 0;
3939
LPVOID pmem = NULL;
4040
HANDLE hthread = 0;
@@ -44,7 +44,7 @@ bool inject_dll(DWORD pid, const char *dll_path)
4444
pmem = VirtualAllocEx(hproc, NULL, path_len, MEM_COMMIT, PAGE_READWRITE); // 申请内存
4545
if (pmem == NULL) goto finally;
4646
WriteProcessMemory(hproc, pmem, dll_path, path_len, NULL); // 把dll路径写进去
47-
hthread = CreateRemoteThread(hproc, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibraryA, pmem, 0, NULL); // 创建远程线程注入
47+
hthread = CreateRemoteThread(hproc, NULL, 0, (LPTHREAD_START_ROUTINE)LoadLibraryW, pmem, 0, NULL); // 创建远程线程注入
4848
if (hthread == 0) goto finally;
4949
WaitForSingleObject(hthread, INFINITE); // 等待线程执行
5050
DWORD threadres;
@@ -61,18 +61,18 @@ bool inject_dll(DWORD pid, const char *dll_path)
6161
return result;
6262
}
6363

64-
DWORD find_pid_by_name(const char *name)
64+
DWORD find_pid_by_name(const wchar_t *name)
6565
{
6666
HANDLE procsnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
67-
PROCESSENTRY32 procentry;
68-
procentry.dwSize = sizeof(PROCESSENTRY32);
69-
Process32First(procsnapshot, &procentry);
70-
if (strcmp(procentry.szExeFile, name) == 0) {
67+
PROCESSENTRY32W procentry;
68+
procentry.dwSize = sizeof(PROCESSENTRY32W);
69+
Process32FirstW(procsnapshot, &procentry);
70+
if (wcscmp(procentry.szExeFile, name) == 0) {
7171
CloseHandle(procsnapshot);
7272
return procentry.th32ProcessID;
7373
}
74-
while (Process32Next(procsnapshot, &procentry)) {
75-
if (strcmp(procentry.szExeFile, name) == 0) {
74+
while (Process32NextW(procsnapshot, &procentry)) {
75+
if (wcscmp(procentry.szExeFile, name) == 0) {
7676
CloseHandle(procsnapshot);
7777
return procentry.th32ProcessID;
7878
}
@@ -81,21 +81,22 @@ DWORD find_pid_by_name(const char *name)
8181
return 0;
8282
}
8383

84-
HMODULE find_module_handle_from_pid(DWORD pid, const char *module_name)
84+
HMODULE find_module_handle_from_pid(DWORD pid, const wchar_t *module_name)
8585
{
8686
HMODULE h_result = 0;
8787
HANDLE hsnap = CreateToolhelp32Snapshot(TH32CS_SNAPMODULE, pid);
88-
MODULEENTRY32 module_entry;
89-
module_entry.dwSize = sizeof(MODULEENTRY32);
90-
Module32First(hsnap, &module_entry);
88+
MODULEENTRY32W module_entry;
89+
module_entry.dwSize = sizeof(MODULEENTRY32W);
90+
Module32FirstW(hsnap, &module_entry);
9191
do {
92-
if (strcmp(module_entry.szModule, module_name) == 0) {
92+
if (wcscmp(module_entry.szModule, module_name) == 0) {
9393
h_result = module_entry.hModule;
9494
break;
9595
}
96-
} while (Module32Next(hsnap, &module_entry));
96+
} while (Module32NextW(hsnap, &module_entry));
9797
CloseHandle(hsnap);
9898
return h_result;
99+
99100
}
100101

101102
bool remove_module(DWORD pid, HMODULE module_handle)

0 commit comments

Comments
 (0)