Skip to content

Commit

Permalink
feat(WeaselSetup): detect 64-bit on single 32-bit build (#266)
Browse files Browse the repository at this point in the history
* feat(WeaselSetup): detect 64-bit on single 32-bit build

* fix(WeaselSetup): remove redundant backslash
  • Loading branch information
Prcuvu committed Sep 7, 2018
1 parent 835df2b commit fb3ae0f
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 132 deletions.
2 changes: 1 addition & 1 deletion WeaselSetup/TSFRegister.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ BOOL RegisterServer(std::wstring filename, bool wow64)
char achIMEKey[ARRAYSIZE(c_szInfoKeyPrefix) + CLSID_STRLEN];
DWORD flags = KEY_WRITE;
if (wow64) {
flags |= KEY_WOW64_32KEY;
flags |= KEY_WOW64_64KEY;
}
//TCHAR achFileName[MAX_PATH];

Expand Down
2 changes: 1 addition & 1 deletion WeaselSetup/WeaselSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ static int CustomInstall(bool installing)
return 1;

ret = RegCreateKeyEx(HKEY_CURRENT_USER, KEY,
0, NULL, 0, KEY_ALL_ACCESS | KEY_WOW64_32KEY, 0, &hKey, NULL);
0, NULL, 0, KEY_ALL_ACCESS, 0, &hKey, NULL);
if (FAILED(HRESULT_FROM_WIN32(ret)))
{
MessageBox(NULL, KEY, L"安裝失敗", MB_ICONERROR | MB_OK);
Expand Down
77 changes: 0 additions & 77 deletions WeaselSetup/WeaselSetup.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,10 @@
<Configuration>Debug</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Debug|x64">
<Configuration>Debug</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|Win32">
<Configuration>Release</Configuration>
<Platform>Win32</Platform>
</ProjectConfiguration>
<ProjectConfiguration Include="Release|x64">
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<ProjectGuid>{04B795DB-A22B-4657-9350-29F04B8FB8F6}</ProjectGuid>
Expand All @@ -31,57 +23,30 @@
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>$(PLATFORM_TOOLSET)</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>true</UseDebugLibraries>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>$(PLATFORM_TOOLSET)</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>$(PLATFORM_TOOLSET)</PlatformToolset>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration">
<ConfigurationType>Application</ConfigurationType>
<UseDebugLibraries>false</UseDebugLibraries>
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
<PlatformToolset>$(PLATFORM_TOOLSET)</PlatformToolset>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</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 Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="PropertySheets">
<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 Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="PropertySheets">
<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'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)output\</OutDir>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<LinkIncremental>false</LinkIncremental>
<OutDir>$(SolutionDir)output\</OutDir>
<TargetName>$(ProjectName)$(Platform)</TargetName>
</PropertyGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
Expand All @@ -99,23 +64,6 @@
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<ClCompile>
<PrecompiledHeader>Use</PrecompiledHeader>
<WarningLevel>Level3</WarningLevel>
<Optimization>Disabled</Optimization>
<PreprocessorDefinitions>WIN32;_DEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)\include;$(BOOST_ROOT)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreadedDebug</RuntimeLibrary>
<AdditionalOptions>/Zc:threadSafeInit- %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<AdditionalLibraryDirectories>$(SolutionDir)\lib;$(BOOST_ROOT)\stage\lib</AdditionalLibraryDirectories>
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
Expand All @@ -139,29 +87,6 @@
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
<PrecompiledHeader>Use</PrecompiledHeader>
<Optimization>MaxSpeed</Optimization>
<FunctionLevelLinking>true</FunctionLevelLinking>
<IntrinsicFunctions>true</IntrinsicFunctions>
<PreprocessorDefinitions>WIN32;NDEBUG;_WINDOWS;%(PreprocessorDefinitions)</PreprocessorDefinitions>
<AdditionalIncludeDirectories>$(SolutionDir)\include;$(BOOST_ROOT)</AdditionalIncludeDirectories>
<RuntimeLibrary>MultiThreaded</RuntimeLibrary>
<AdditionalOptions>/Zc:threadSafeInit- %(AdditionalOptions)</AdditionalOptions>
</ClCompile>
<Link>
<SubSystem>Windows</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<EnableCOMDATFolding>true</EnableCOMDATFolding>
<OptimizeReferences>true</OptimizeReferences>
<AdditionalLibraryDirectories>$(SolutionDir)\lib;$(BOOST_ROOT)\stage_x64\lib</AdditionalLibraryDirectories>
<AdditionalDependencies>imm32.lib;%(AdditionalDependencies)</AdditionalDependencies>
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
<OutputFile>$(SolutionDir)output\$(ProjectName)$(Platform)$(TargetExt)</OutputFile>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
<None Include="ReadMe.txt" />
<None Include="small.ico" />
Expand All @@ -180,9 +105,7 @@
<ClCompile Include="InstallOptionsDialog.cpp" />
<ClCompile Include="stdafx.cpp">
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">Create</PrecompiledHeader>
<PrecompiledHeader Condition="'$(Configuration)|$(Platform)'=='Release|x64'">Create</PrecompiledHeader>
</ClCompile>
<ClCompile Include="TSFRegister.cpp" />
<ClCompile Include="WeaselSetup.cpp" />
Expand Down
87 changes: 58 additions & 29 deletions WeaselSetup/imesetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,50 +54,67 @@ BOOL delete_file(const std::wstring& file)
return ret;
}

BOOL is_wow64()
{
DWORD errorCode;
if (GetSystemWow64DirectoryW(NULL, 0) == 0)
if ((errorCode = GetLastError()) == ERROR_CALL_NOT_IMPLEMENTED)
return FALSE;
else
ExitProcess((UINT)errorCode);
else
return TRUE;
}

typedef BOOL (WINAPI *PW64DW64FR)(PVOID *);
typedef BOOL (WINAPI *PW64RW64FR)(PVOID);
typedef int (*ime_register_func)(const std::wstring& ime_path, bool register_ime, bool is_wow64, bool hant, bool silent);

int install_ime_file(std::wstring& srcPath, const std::wstring& ext, bool hant, bool silent, ime_register_func func)
{
std::wstring destPath;
std::wstring wow64Path;

WCHAR path[MAX_PATH];
GetModuleFileName(GetModuleHandle(NULL), path, _countof(path));
GetModuleFileNameW(GetModuleHandle(NULL), path, _countof(path));

bool is_x64 = (sizeof(HANDLE) == 8);
std::wstring srcFileName = (hant ? L"weaselt" : L"weasel");
srcFileName += (is_x64 ? L"x64" + ext : ext);
srcFileName += ext;
WCHAR drive[_MAX_DRIVE];
WCHAR dir[_MAX_DIR];
_wsplitpath_s(path, drive, _countof(drive), dir, _countof(dir), NULL, 0, NULL, 0);
srcPath = std::wstring(drive) + dir + L'\\' + srcFileName;
srcPath = std::wstring(drive) + dir + srcFileName;

GetSystemDirectory(path, _countof(path));
destPath = std::wstring(path) + L"\\weasel" + ext;

if (GetSystemWow64Directory(path, _countof(path)))
{
wow64Path = std::wstring(path) + L"\\weasel" + ext;
}
GetSystemDirectoryW(path, _countof(path));
std::wstring destPath = std::wstring(path) + L"\\weasel" + ext;

int retval = 0;
// 复制 .dll/.ime 到系统目录
if (!copy_file(srcPath, destPath))
{
if (!silent) MessageBox(NULL, destPath.c_str(), L"安裝失敗", MB_ICONERROR | MB_OK);
if (!silent) MessageBoxW(NULL, destPath.c_str(), L"安裝失敗", MB_ICONERROR | MB_OK);
return 1;
}
retval += func(destPath, true, false, hant, silent);
if (!wow64Path.empty())
if (is_wow64())
{
std::wstring x86 = srcPath;
ireplace_last(x86, L"x64" + ext, ext);
if (!copy_file(x86, wow64Path))
ireplace_last(srcPath, ext, L"x64" + ext);
PVOID OldValue = NULL;
PW64DW64FR fnWow64DisableWow64FsRedirection = (PW64DW64FR)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "Wow64DisableWow64FsRedirection");
PW64RW64FR fnWow64RevertWow64FsRedirection = (PW64RW64FR)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "Wow64RevertWow64FsRedirection");
if (fnWow64DisableWow64FsRedirection == NULL || fnWow64DisableWow64FsRedirection(&OldValue) == FALSE)
{
if (!silent) MessageBoxW(NULL, L"無法取消文件系統重定向", L"安裝失敗", MB_ICONERROR | MB_OK);
return 1;
}
if (!copy_file(srcPath, destPath))
{
if (!silent) MessageBox(NULL, wow64Path.c_str(), L"安裝失敗", MB_ICONERROR | MB_OK);
if (!silent) MessageBoxW(NULL, destPath.c_str(), L"安裝失敗", MB_ICONERROR | MB_OK);
return 1;
}
retval += func(wow64Path, true, true, hant, silent);
if (fnWow64RevertWow64FsRedirection == NULL || fnWow64RevertWow64FsRedirection(OldValue) == FALSE)
{
if (!silent) MessageBoxW(NULL, L"無法恢復文件系統重定向", L"安裝失敗", MB_ICONERROR | MB_OK);
return 1;
}
retval += func(destPath, true, true, hant, silent);
}
return retval;
}
Expand All @@ -106,7 +123,7 @@ int uninstall_ime_file(const std::wstring& ext, bool silent, ime_register_func f
{
int retval = 0;
WCHAR path[MAX_PATH];
GetSystemDirectory(path, _countof(path));
GetSystemDirectoryW(path, _countof(path));
std::wstring imePath(path);
imePath += L"\\weasel" + ext;
retval += func(imePath, false, false, false, silent);
Expand All @@ -115,16 +132,27 @@ int uninstall_ime_file(const std::wstring& ext, bool silent, ime_register_func f
if (!silent) MessageBox(NULL, imePath.c_str(), L"卸載失敗", MB_ICONERROR | MB_OK);
retval += 1;
}
if (GetSystemWow64Directory(path, _countof(path)))
if (is_wow64())
{
std::wstring wow64Path(path);
wow64Path += L"\\weasel" + ext;
retval += func(wow64Path, false, true, false, silent);
if (!delete_file(wow64Path))
retval += func(imePath, false, true, false, silent);
PVOID OldValue = NULL;
PW64DW64FR fnWow64DisableWow64FsRedirection = (PW64DW64FR)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "Wow64DisableWow64FsRedirection");
PW64RW64FR fnWow64RevertWow64FsRedirection = (PW64RW64FR)GetProcAddress(GetModuleHandle(_T("kernel32.dll")), "Wow64RevertWow64FsRedirection");
if (fnWow64DisableWow64FsRedirection == NULL || fnWow64DisableWow64FsRedirection(&OldValue) == FALSE)
{
if (!silent) MessageBoxW(NULL, L"無法取消文件系統重定向", L"卸載失敗", MB_ICONERROR | MB_OK);
return 1;
}
if (!delete_file(imePath))
{
if (!silent) MessageBox(NULL, wow64Path.c_str(), L"卸載失敗", MB_ICONERROR | MB_OK);
if (!silent) MessageBoxW(NULL, imePath.c_str(), L"卸載失敗", MB_ICONERROR | MB_OK);
retval += 1;
}
if (fnWow64RevertWow64FsRedirection == NULL || fnWow64RevertWow64FsRedirection(OldValue) == FALSE)
{
if (!silent) MessageBoxW(NULL, L"無法恢復文件系統重定向", L"卸載失敗", MB_ICONERROR | MB_OK);
return 1;
}
}
return retval;
}
Expand Down Expand Up @@ -378,7 +406,7 @@ int install(bool hant, bool silent)
// 写注册表
HKEY hKey;
LSTATUS ret = RegCreateKeyEx(HKEY_LOCAL_MACHINE, WEASEL_REG_KEY,
0, NULL, 0, KEY_ALL_ACCESS | KEY_WOW64_32KEY, 0, &hKey, NULL);
0, NULL, 0, KEY_ALL_ACCESS, 0, &hKey, NULL);
if (FAILED(HRESULT_FROM_WIN32(ret)))
{
if (!silent) MessageBox(NULL, WEASEL_REG_KEY, L"安裝失敗", MB_ICONERROR | MB_OK);
Expand All @@ -389,6 +417,7 @@ int install(bool hant, bool silent)
WCHAR dir[_MAX_DIR];
_wsplitpath_s(ime_src_path.c_str(), drive, _countof(drive), dir, _countof(dir), NULL, 0, NULL, 0);
std::wstring rootDir = std::wstring(drive) + dir;
rootDir.pop_back();
ret = RegSetValueEx(hKey, L"WeaselRoot", 0, REG_SZ,
(const BYTE*)rootDir.c_str(),
(rootDir.length() + 1) * sizeof(WCHAR));
Expand Down
24 changes: 4 additions & 20 deletions output/install.nsi
Original file line number Diff line number Diff line change
Expand Up @@ -140,11 +140,7 @@ program_files:
${EndIf}
File "WeaselDeployer.exe"
File "WeaselServer.exe"
${If} ${RunningX64}
File "WeaselSetupx64.exe"
${Else}
File "WeaselSetup.exe"
${EndIf}
File "WeaselSetup.exe"
File "rime.dll"
File "WinSparkle.dll"
; shared data files
Expand Down Expand Up @@ -176,11 +172,7 @@ program_files:
IfErrors +2 0
StrCpy $R2 "/t"

${If} ${RunningX64}
ExecWait '"$INSTDIR\WeaselSetupx64.exe" $R2'
${Else}
ExecWait '"$INSTDIR\WeaselSetup.exe" $R2'
${EndIf}
ExecWait '"$INSTDIR\WeaselSetup.exe" $R2'

; run as user...
ExecWait "$INSTDIR\WeaselDeployer.exe /install"
Expand Down Expand Up @@ -216,11 +208,7 @@ Section "Start Menu Shortcuts"
CreateShortCut "$SMPROGRAMS\小狼毫輸入法\【小狼毫】用戶文件夾.lnk" "$INSTDIR\WeaselServer.exe" "/userdir" "$SYSDIR\shell32.dll" 126
CreateShortCut "$SMPROGRAMS\小狼毫輸入法\【小狼毫】程序文件夾.lnk" "$INSTDIR\WeaselServer.exe" "/weaseldir" "$SYSDIR\shell32.dll" 19
CreateShortCut "$SMPROGRAMS\小狼毫輸入法\【小狼毫】檢查新版本.lnk" "$INSTDIR\WeaselServer.exe" "/update" "$SYSDIR\shell32.dll" 13
${If} ${RunningX64}
CreateShortCut "$SMPROGRAMS\小狼毫輸入法\【小狼毫】安裝選項.lnk" "$INSTDIR\WeaselSetupx64.exe" "" "$SYSDIR\shell32.dll" 162
${Else}
CreateShortCut "$SMPROGRAMS\小狼毫輸入法\【小狼毫】安裝選項.lnk" "$INSTDIR\WeaselSetup.exe" "" "$SYSDIR\shell32.dll" 162
${EndIf}
CreateShortCut "$SMPROGRAMS\小狼毫輸入法\【小狼毫】安裝選項.lnk" "$INSTDIR\WeaselSetup.exe" "" "$SYSDIR\shell32.dll" 162
CreateShortCut "$SMPROGRAMS\小狼毫輸入法\卸載小狼毫.lnk" "$INSTDIR\uninstall.exe" "" "$INSTDIR\uninstall.exe" 0

SectionEnd
Expand All @@ -233,11 +221,7 @@ Section "Uninstall"

ExecWait '"$INSTDIR\WeaselServer.exe" /quit'

${If} ${RunningX64}
ExecWait '"$INSTDIR\WeaselSetupx64.exe" /u'
${Else}
ExecWait '"$INSTDIR\WeaselSetup.exe" /u'
${EndIf}
ExecWait '"$INSTDIR\WeaselSetup.exe" /u'

; Remove registry keys
DeleteRegKey HKLM "Software\Microsoft\Windows\CurrentVersion\Uninstall\Weasel"
Expand Down
6 changes: 2 additions & 4 deletions weasel.sln
Original file line number Diff line number Diff line change
Expand Up @@ -167,12 +167,10 @@ Global
{F53F3E9C-CC4D-4D1D-9C2E-719FE60A7E6B}.ReleaseHant|x64.ActiveCfg = Release|Win32
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.Debug|Win32.ActiveCfg = Debug|Win32
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.Debug|Win32.Build.0 = Debug|Win32
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.Debug|x64.ActiveCfg = Debug|x64
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.Debug|x64.Build.0 = Debug|x64
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.Debug|x64.ActiveCfg = Debug|Win32
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.Release|Win32.ActiveCfg = Release|Win32
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.Release|Win32.Build.0 = Release|Win32
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.Release|x64.ActiveCfg = Release|x64
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.Release|x64.Build.0 = Release|x64
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.Release|x64.ActiveCfg = Release|Win32
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.ReleaseHant|Win32.ActiveCfg = Release|Win32
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.ReleaseHant|Win32.Build.0 = Release|Win32
{04B795DB-A22B-4657-9350-29F04B8FB8F6}.ReleaseHant|x64.ActiveCfg = Release|Win32
Expand Down

0 comments on commit fb3ae0f

Please sign in to comment.