1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski *
3*b1cdbd2cSJim Jagielski * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski * or more contributor license agreements. See the NOTICE file
5*b1cdbd2cSJim Jagielski * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski * regarding copyright ownership. The ASF licenses this file
7*b1cdbd2cSJim Jagielski * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski * with the License. You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski *
11*b1cdbd2cSJim Jagielski * http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski *
13*b1cdbd2cSJim Jagielski * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski * KIND, either express or implied. See the License for the
17*b1cdbd2cSJim Jagielski * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski * under the License.
19*b1cdbd2cSJim Jagielski *
20*b1cdbd2cSJim Jagielski *************************************************************/
21*b1cdbd2cSJim Jagielski
22*b1cdbd2cSJim Jagielski
23*b1cdbd2cSJim Jagielski
24*b1cdbd2cSJim Jagielski #ifdef _MSC_VER
25*b1cdbd2cSJim Jagielski #pragma warning(push, 1) /* disable warnings within system headers */
26*b1cdbd2cSJim Jagielski #endif
27*b1cdbd2cSJim Jagielski #define WIN32_LEAN_AND_MEAN
28*b1cdbd2cSJim Jagielski #include <windows.h>
29*b1cdbd2cSJim Jagielski #include <msiquery.h>
30*b1cdbd2cSJim Jagielski #ifdef _MSC_VER
31*b1cdbd2cSJim Jagielski #pragma warning(pop)
32*b1cdbd2cSJim Jagielski #endif
33*b1cdbd2cSJim Jagielski
34*b1cdbd2cSJim Jagielski #include <malloc.h>
35*b1cdbd2cSJim Jagielski #include <tchar.h>
36*b1cdbd2cSJim Jagielski #include <string>
37*b1cdbd2cSJim Jagielski #include <stdexcept>
38*b1cdbd2cSJim Jagielski #include <vector>
39*b1cdbd2cSJim Jagielski
40*b1cdbd2cSJim Jagielski class RegistryKeyGuard
41*b1cdbd2cSJim Jagielski {
42*b1cdbd2cSJim Jagielski public:
RegistryKeyGuard(HKEY hkey=0)43*b1cdbd2cSJim Jagielski RegistryKeyGuard(HKEY hkey = 0) :
44*b1cdbd2cSJim Jagielski hkey_(hkey)
45*b1cdbd2cSJim Jagielski {
46*b1cdbd2cSJim Jagielski }
47*b1cdbd2cSJim Jagielski
~RegistryKeyGuard()48*b1cdbd2cSJim Jagielski ~RegistryKeyGuard()
49*b1cdbd2cSJim Jagielski {
50*b1cdbd2cSJim Jagielski if (hkey_)
51*b1cdbd2cSJim Jagielski RegCloseKey(hkey_);
52*b1cdbd2cSJim Jagielski }
53*b1cdbd2cSJim Jagielski private:
54*b1cdbd2cSJim Jagielski HKEY hkey_;
55*b1cdbd2cSJim Jagielski
56*b1cdbd2cSJim Jagielski private:
57*b1cdbd2cSJim Jagielski RegistryKeyGuard(const RegistryKeyGuard&);
58*b1cdbd2cSJim Jagielski RegistryKeyGuard& operator=(const RegistryKeyGuard&);
59*b1cdbd2cSJim Jagielski };
60*b1cdbd2cSJim Jagielski
61*b1cdbd2cSJim Jagielski typedef std::vector<TCHAR> CharacterBuffer_t;
62*b1cdbd2cSJim Jagielski
63*b1cdbd2cSJim Jagielski /* throws std::runtime_error when the value "Path" could
64*b1cdbd2cSJim Jagielski not be found or contains an empty string or is not of
65*b1cdbd2cSJim Jagielski type REG_SZ. All such conditions are invalid for a
66*b1cdbd2cSJim Jagielski properly installed product. */
FindProductInstallationPath(HKEY hkey)67*b1cdbd2cSJim Jagielski std::string FindProductInstallationPath(HKEY hkey)
68*b1cdbd2cSJim Jagielski {
69*b1cdbd2cSJim Jagielski DWORD nSubKeys;
70*b1cdbd2cSJim Jagielski DWORD lLongestSubKey;
71*b1cdbd2cSJim Jagielski
72*b1cdbd2cSJim Jagielski if (RegQueryInfoKey(hkey, NULL, NULL, NULL, &nSubKeys, &lLongestSubKey, NULL, NULL, NULL, NULL, NULL, NULL) !=
73*b1cdbd2cSJim Jagielski ERROR_SUCCESS)
74*b1cdbd2cSJim Jagielski throw std::runtime_error("Cannot query info for registery key");
75*b1cdbd2cSJim Jagielski
76*b1cdbd2cSJim Jagielski CharacterBuffer_t buff(lLongestSubKey + 1);
77*b1cdbd2cSJim Jagielski
78*b1cdbd2cSJim Jagielski for (DWORD i = 0; i < nSubKeys; i++)
79*b1cdbd2cSJim Jagielski {
80*b1cdbd2cSJim Jagielski buff[0] = 0;
81*b1cdbd2cSJim Jagielski LONG ret = RegEnumKey(hkey, i, &buff[0], buff.size());
82*b1cdbd2cSJim Jagielski
83*b1cdbd2cSJim Jagielski if ((ret != ERROR_SUCCESS) && (ret != ERROR_MORE_DATA))
84*b1cdbd2cSJim Jagielski throw std::runtime_error("Error enumerating registry key");
85*b1cdbd2cSJim Jagielski
86*b1cdbd2cSJim Jagielski HKEY hSubKey;
87*b1cdbd2cSJim Jagielski if (RegOpenKey(hkey, &buff[0], &hSubKey) != ERROR_SUCCESS)
88*b1cdbd2cSJim Jagielski continue;
89*b1cdbd2cSJim Jagielski
90*b1cdbd2cSJim Jagielski RegistryKeyGuard guard(hSubKey);
91*b1cdbd2cSJim Jagielski
92*b1cdbd2cSJim Jagielski DWORD type;
93*b1cdbd2cSJim Jagielski TCHAR pbuff[MAX_PATH];
94*b1cdbd2cSJim Jagielski DWORD size = sizeof(pbuff);
95*b1cdbd2cSJim Jagielski if ((RegQueryValueEx(
96*b1cdbd2cSJim Jagielski hSubKey, TEXT("Path"), NULL, &type, reinterpret_cast<LPBYTE>(pbuff), &size) != ERROR_SUCCESS) ||
97*b1cdbd2cSJim Jagielski (type != REG_SZ))
98*b1cdbd2cSJim Jagielski continue;
99*b1cdbd2cSJim Jagielski
100*b1cdbd2cSJim Jagielski std::string path(pbuff);
101*b1cdbd2cSJim Jagielski std::string::size_type idx = path.rfind("program\\soffice.exe");
102*b1cdbd2cSJim Jagielski if (idx != std::string::npos)
103*b1cdbd2cSJim Jagielski return path.substr(0, idx);
104*b1cdbd2cSJim Jagielski } // for
105*b1cdbd2cSJim Jagielski
106*b1cdbd2cSJim Jagielski throw std::runtime_error("No valid product path found");
107*b1cdbd2cSJim Jagielski }
108*b1cdbd2cSJim Jagielski
GetInstallProperty(MSIHANDLE handle,LPCTSTR name,CharacterBuffer_t * buffer)109*b1cdbd2cSJim Jagielski UINT GetInstallProperty(MSIHANDLE handle, LPCTSTR name, CharacterBuffer_t* buffer)
110*b1cdbd2cSJim Jagielski {
111*b1cdbd2cSJim Jagielski DWORD size = buffer->size();
112*b1cdbd2cSJim Jagielski UINT ret = MsiGetProperty(handle, name, &(*buffer)[0], &size);
113*b1cdbd2cSJim Jagielski
114*b1cdbd2cSJim Jagielski if (ret == ERROR_MORE_DATA)
115*b1cdbd2cSJim Jagielski {
116*b1cdbd2cSJim Jagielski buffer->resize(size + 1);
117*b1cdbd2cSJim Jagielski size = buffer->size();
118*b1cdbd2cSJim Jagielski ret = MsiGetProperty(handle, name, &(*buffer)[0], &size);
119*b1cdbd2cSJim Jagielski }
120*b1cdbd2cSJim Jagielski return ret;
121*b1cdbd2cSJim Jagielski }
122*b1cdbd2cSJim Jagielski
123*b1cdbd2cSJim Jagielski /*
124*b1cdbd2cSJim Jagielski Try to find the installation path to an already installed product.
125*b1cdbd2cSJim Jagielski The installation path will be written in the Windows registry
126*b1cdbd2cSJim Jagielski during the installation. There may exist different products in
127*b1cdbd2cSJim Jagielski parallel e.g. StarOffice, StarSuite, OpenOffice.org, Apache OpenOffice.
128*b1cdbd2cSJim Jagielski It will be searched in this order for an installed product. If a product
129*b1cdbd2cSJim Jagielski will be found the path to the product will be set in the property
130*b1cdbd2cSJim Jagielski "INSTALLLOCATION" else nothing will be done.
131*b1cdbd2cSJim Jagielski */
SetProductInstallationPath(MSIHANDLE handle)132*b1cdbd2cSJim Jagielski extern "C" UINT __stdcall SetProductInstallationPath(MSIHANDLE handle)
133*b1cdbd2cSJim Jagielski {
134*b1cdbd2cSJim Jagielski //MessageBox(NULL, TEXT("SetProductInstallationPath"), TEXT("Language Pack Installation Helper"), MB_OK | MB_ICONINFORMATION);
135*b1cdbd2cSJim Jagielski
136*b1cdbd2cSJim Jagielski try
137*b1cdbd2cSJim Jagielski {
138*b1cdbd2cSJim Jagielski CharacterBuffer_t regKeyProdPath(MAX_PATH);
139*b1cdbd2cSJim Jagielski
140*b1cdbd2cSJim Jagielski GetInstallProperty(handle, TEXT("REGKEYPRODPATH"), ®KeyProdPath);
141*b1cdbd2cSJim Jagielski
142*b1cdbd2cSJim Jagielski HKEY hKey;
143*b1cdbd2cSJim Jagielski if ((RegOpenKey(HKEY_CURRENT_USER, ®KeyProdPath[0], &hKey) == ERROR_SUCCESS) ||
144*b1cdbd2cSJim Jagielski (RegOpenKey(HKEY_LOCAL_MACHINE, ®KeyProdPath[0], &hKey) == ERROR_SUCCESS))
145*b1cdbd2cSJim Jagielski {
146*b1cdbd2cSJim Jagielski RegistryKeyGuard guard(hKey);
147*b1cdbd2cSJim Jagielski std::string path = FindProductInstallationPath(hKey);
148*b1cdbd2cSJim Jagielski MsiSetProperty(handle, TEXT("INSTALLLOCATION"), path.c_str());
149*b1cdbd2cSJim Jagielski }
150*b1cdbd2cSJim Jagielski }
151*b1cdbd2cSJim Jagielski catch(std::runtime_error& ex)
152*b1cdbd2cSJim Jagielski {
153*b1cdbd2cSJim Jagielski ex = ex; // no warnings
154*b1cdbd2cSJim Jagielski }
155*b1cdbd2cSJim Jagielski return ERROR_SUCCESS;
156*b1cdbd2cSJim Jagielski }
157*b1cdbd2cSJim Jagielski
MakeCfgimportCommandLine(CharacterBuffer_t * productPath)158*b1cdbd2cSJim Jagielski void MakeCfgimportCommandLine(CharacterBuffer_t* productPath)
159*b1cdbd2cSJim Jagielski {
160*b1cdbd2cSJim Jagielski char* p = &(*productPath)[0] + lstrlen(&(*productPath)[0]) - 1;
161*b1cdbd2cSJim Jagielski
162*b1cdbd2cSJim Jagielski if (*p != '\\')
163*b1cdbd2cSJim Jagielski lstrcat(&(*productPath)[0], "\\program\\configimport.exe --spool");
164*b1cdbd2cSJim Jagielski else
165*b1cdbd2cSJim Jagielski lstrcat(&(*productPath)[0], "program\\configimport.exe --spool");
166*b1cdbd2cSJim Jagielski }
167*b1cdbd2cSJim Jagielski
168*b1cdbd2cSJim Jagielski /*
169*b1cdbd2cSJim Jagielski Calls configimport.exe --spool
170*b1cdbd2cSJim Jagielski */
RegisterLanguagePack(MSIHANDLE handle)171*b1cdbd2cSJim Jagielski extern "C" UINT __stdcall RegisterLanguagePack(MSIHANDLE handle)
172*b1cdbd2cSJim Jagielski {
173*b1cdbd2cSJim Jagielski //MessageBox(NULL, TEXT("RegisterLanguagePack"), TEXT("Language Pack Installation Helper"), MB_OK | MB_ICONINFORMATION);
174*b1cdbd2cSJim Jagielski
175*b1cdbd2cSJim Jagielski CharacterBuffer_t productPath(MAX_PATH);
176*b1cdbd2cSJim Jagielski GetInstallProperty(handle, TEXT("INSTALLLOCATION"), &productPath);
177*b1cdbd2cSJim Jagielski MakeCfgimportCommandLine(&productPath);
178*b1cdbd2cSJim Jagielski
179*b1cdbd2cSJim Jagielski STARTUPINFO si;
180*b1cdbd2cSJim Jagielski ZeroMemory(&si, sizeof(si));
181*b1cdbd2cSJim Jagielski si.cb = sizeof(si);
182*b1cdbd2cSJim Jagielski
183*b1cdbd2cSJim Jagielski PROCESS_INFORMATION pi;
184*b1cdbd2cSJim Jagielski ZeroMemory(&pi, sizeof(pi));
185*b1cdbd2cSJim Jagielski
186*b1cdbd2cSJim Jagielski if (CreateProcess(
187*b1cdbd2cSJim Jagielski NULL, &productPath[0], NULL, NULL,
188*b1cdbd2cSJim Jagielski FALSE, CREATE_NO_WINDOW | NORMAL_PRIORITY_CLASS, NULL,
189*b1cdbd2cSJim Jagielski NULL, &si, &pi))
190*b1cdbd2cSJim Jagielski {
191*b1cdbd2cSJim Jagielski // Wait until child process exits.
192*b1cdbd2cSJim Jagielski WaitForSingleObject(pi.hProcess, INFINITE);
193*b1cdbd2cSJim Jagielski
194*b1cdbd2cSJim Jagielski // Close process and thread handles.
195*b1cdbd2cSJim Jagielski CloseHandle(pi.hProcess);
196*b1cdbd2cSJim Jagielski CloseHandle(pi.hThread);
197*b1cdbd2cSJim Jagielski }
198*b1cdbd2cSJim Jagielski return ERROR_SUCCESS;
199*b1cdbd2cSJim Jagielski }
200*b1cdbd2cSJim Jagielski
201