@@ -54,50 +54,67 @@ BOOL delete_file(const std::wstring& file)
54
54
return ret;
55
55
}
56
56
57
+ BOOL is_wow64 ()
58
+ {
59
+ DWORD errorCode;
60
+ if (GetSystemWow64DirectoryW (NULL , 0 ) == 0 )
61
+ if ((errorCode = GetLastError ()) == ERROR_CALL_NOT_IMPLEMENTED)
62
+ return FALSE ;
63
+ else
64
+ ExitProcess ((UINT)errorCode);
65
+ else
66
+ return TRUE ;
67
+ }
68
+
69
+ typedef BOOL (WINAPI *PW64DW64FR)(PVOID *);
70
+ typedef BOOL (WINAPI *PW64RW64FR)(PVOID);
57
71
typedef int (*ime_register_func)(const std::wstring& ime_path, bool register_ime, bool is_wow64, bool hant, bool silent);
58
72
59
73
int install_ime_file (std::wstring& srcPath, const std::wstring& ext, bool hant, bool silent, ime_register_func func)
60
74
{
61
- std::wstring destPath;
62
- std::wstring wow64Path;
63
-
64
75
WCHAR path[MAX_PATH];
65
- GetModuleFileName (GetModuleHandle (NULL ), path, _countof (path));
76
+ GetModuleFileNameW (GetModuleHandle (NULL ), path, _countof (path));
66
77
67
- bool is_x64 = (sizeof (HANDLE) == 8 );
68
78
std::wstring srcFileName = (hant ? L" weaselt" : L" weasel" );
69
- srcFileName += (is_x64 ? L" x64 " + ext : ext) ;
79
+ srcFileName += ext;
70
80
WCHAR drive[_MAX_DRIVE];
71
81
WCHAR dir[_MAX_DIR];
72
82
_wsplitpath_s (path, drive, _countof (drive), dir, _countof (dir), NULL , 0 , NULL , 0 );
73
- srcPath = std::wstring (drive) + dir + L ' \\ ' + srcFileName;
83
+ srcPath = std::wstring (drive) + dir + srcFileName;
74
84
75
- GetSystemDirectory (path, _countof (path));
76
- destPath = std::wstring (path) + L" \\ weasel" + ext;
77
-
78
- if (GetSystemWow64Directory (path, _countof (path)))
79
- {
80
- wow64Path = std::wstring (path) + L" \\ weasel" + ext;
81
- }
85
+ GetSystemDirectoryW (path, _countof (path));
86
+ std::wstring destPath = std::wstring (path) + L" \\ weasel" + ext;
82
87
83
88
int retval = 0 ;
84
89
// 复制 .dll/.ime 到系统目录
85
90
if (!copy_file (srcPath, destPath))
86
91
{
87
- if (!silent) MessageBox (NULL , destPath.c_str (), L" 安裝失敗" , MB_ICONERROR | MB_OK);
92
+ if (!silent) MessageBoxW (NULL , destPath.c_str (), L" 安裝失敗" , MB_ICONERROR | MB_OK);
88
93
return 1 ;
89
94
}
90
95
retval += func (destPath, true , false , hant, silent);
91
- if (!wow64Path. empty ())
96
+ if (is_wow64 ())
92
97
{
93
- std::wstring x86 = srcPath;
94
- ireplace_last (x86, L" x64" + ext, ext);
95
- if (!copy_file (x86, wow64Path))
98
+ ireplace_last (srcPath, ext, L" x64" + ext);
99
+ PVOID OldValue = NULL ;
100
+ PW64DW64FR fnWow64DisableWow64FsRedirection = (PW64DW64FR)GetProcAddress (GetModuleHandle (_T (" kernel32.dll" )), " Wow64DisableWow64FsRedirection" );
101
+ PW64RW64FR fnWow64RevertWow64FsRedirection = (PW64RW64FR)GetProcAddress (GetModuleHandle (_T (" kernel32.dll" )), " Wow64RevertWow64FsRedirection" );
102
+ if (fnWow64DisableWow64FsRedirection == NULL || fnWow64DisableWow64FsRedirection (&OldValue) == FALSE )
103
+ {
104
+ if (!silent) MessageBoxW (NULL , L" 無法取消文件系統重定向" , L" 安裝失敗" , MB_ICONERROR | MB_OK);
105
+ return 1 ;
106
+ }
107
+ if (!copy_file (srcPath, destPath))
96
108
{
97
- if (!silent) MessageBox (NULL , wow64Path .c_str (), L" 安裝失敗" , MB_ICONERROR | MB_OK);
109
+ if (!silent) MessageBoxW (NULL , destPath .c_str (), L" 安裝失敗" , MB_ICONERROR | MB_OK);
98
110
return 1 ;
99
111
}
100
- retval += func (wow64Path, true , true , hant, silent);
112
+ if (fnWow64RevertWow64FsRedirection == NULL || fnWow64RevertWow64FsRedirection (OldValue) == FALSE )
113
+ {
114
+ if (!silent) MessageBoxW (NULL , L" 無法恢復文件系統重定向" , L" 安裝失敗" , MB_ICONERROR | MB_OK);
115
+ return 1 ;
116
+ }
117
+ retval += func (destPath, true , true , hant, silent);
101
118
}
102
119
return retval;
103
120
}
@@ -106,7 +123,7 @@ int uninstall_ime_file(const std::wstring& ext, bool silent, ime_register_func f
106
123
{
107
124
int retval = 0 ;
108
125
WCHAR path[MAX_PATH];
109
- GetSystemDirectory (path, _countof (path));
126
+ GetSystemDirectoryW (path, _countof (path));
110
127
std::wstring imePath (path);
111
128
imePath += L" \\ weasel" + ext;
112
129
retval += func (imePath, false , false , false , silent);
@@ -115,16 +132,27 @@ int uninstall_ime_file(const std::wstring& ext, bool silent, ime_register_func f
115
132
if (!silent) MessageBox (NULL , imePath.c_str (), L" 卸載失敗" , MB_ICONERROR | MB_OK);
116
133
retval += 1 ;
117
134
}
118
- if (GetSystemWow64Directory (path, _countof (path) ))
135
+ if (is_wow64 ( ))
119
136
{
120
- std::wstring wow64Path (path);
121
- wow64Path += L" \\ weasel" + ext;
122
- retval += func (wow64Path, false , true , false , silent);
123
- if (!delete_file (wow64Path))
137
+ retval += func (imePath, false , true , false , silent);
138
+ PVOID OldValue = NULL ;
139
+ PW64DW64FR fnWow64DisableWow64FsRedirection = (PW64DW64FR)GetProcAddress (GetModuleHandle (_T (" kernel32.dll" )), " Wow64DisableWow64FsRedirection" );
140
+ PW64RW64FR fnWow64RevertWow64FsRedirection = (PW64RW64FR)GetProcAddress (GetModuleHandle (_T (" kernel32.dll" )), " Wow64RevertWow64FsRedirection" );
141
+ if (fnWow64DisableWow64FsRedirection == NULL || fnWow64DisableWow64FsRedirection (&OldValue) == FALSE )
142
+ {
143
+ if (!silent) MessageBoxW (NULL , L" 無法取消文件系統重定向" , L" 卸載失敗" , MB_ICONERROR | MB_OK);
144
+ return 1 ;
145
+ }
146
+ if (!delete_file (imePath))
124
147
{
125
- if (!silent) MessageBox (NULL , wow64Path .c_str (), L" 卸載失敗" , MB_ICONERROR | MB_OK);
148
+ if (!silent) MessageBoxW (NULL , imePath .c_str (), L" 卸載失敗" , MB_ICONERROR | MB_OK);
126
149
retval += 1 ;
127
150
}
151
+ if (fnWow64RevertWow64FsRedirection == NULL || fnWow64RevertWow64FsRedirection (OldValue) == FALSE )
152
+ {
153
+ if (!silent) MessageBoxW (NULL , L" 無法恢復文件系統重定向" , L" 卸載失敗" , MB_ICONERROR | MB_OK);
154
+ return 1 ;
155
+ }
128
156
}
129
157
return retval;
130
158
}
@@ -378,7 +406,7 @@ int install(bool hant, bool silent)
378
406
// 写注册表
379
407
HKEY hKey;
380
408
LSTATUS ret = RegCreateKeyEx (HKEY_LOCAL_MACHINE, WEASEL_REG_KEY,
381
- 0 , NULL , 0 , KEY_ALL_ACCESS | KEY_WOW64_32KEY , 0 , &hKey, NULL );
409
+ 0 , NULL , 0 , KEY_ALL_ACCESS, 0 , &hKey, NULL );
382
410
if (FAILED (HRESULT_FROM_WIN32 (ret)))
383
411
{
384
412
if (!silent) MessageBox (NULL , WEASEL_REG_KEY, L" 安裝失敗" , MB_ICONERROR | MB_OK);
@@ -389,6 +417,7 @@ int install(bool hant, bool silent)
389
417
WCHAR dir[_MAX_DIR];
390
418
_wsplitpath_s (ime_src_path.c_str (), drive, _countof (drive), dir, _countof (dir), NULL , 0 , NULL , 0 );
391
419
std::wstring rootDir = std::wstring (drive) + dir;
420
+ rootDir.pop_back ();
392
421
ret = RegSetValueEx (hKey, L" WeaselRoot" , 0 , REG_SZ,
393
422
(const BYTE*)rootDir.c_str (),
394
423
(rootDir.length () + 1 ) * sizeof (WCHAR));
0 commit comments