Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: src/installer-ca/wcautil.cpp

Issue 11521026: initial custom action library, "hello, world" quality (Closed)
Patch Set: Created Sept. 3, 2013, 12:48 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 //------------------------------------------------------------------------------ -------------------
2 // <copyright file="wcautil.cpp" company="Outercurve Foundation">
3 // Copyright (c) 2004, Outercurve Foundation.
4 // This software is released under Microsoft Reciprocal License (MS-RL).
5 // The license and further copyright text can be found in the file
6 // LICENSE.TXT at the root directory of the distribution.
7 // </copyright>
8 //
9 // <summary>
10 // Windows Installer XML CustomAction utility library.
11 // </summary>
12 //------------------------------------------------------------------------------ -------------------
13
14 #include "precomp.h"
15
16 // globals
17 HMODULE g_hInstCADLL;
18
19 // statics
20 static BOOL s_fInitialized;
21 static MSIHANDLE s_hInstall;
22 static MSIHANDLE s_hDatabase;
23 static char s_szCustomActionLogName[32];
24 static UINT s_iRetVal;
25
26
27 /********************************************************************
28 WcaGlobalInitialize() - initializes the Wca library, should be
29 called once per custom action Dll during
30 DllMain on DLL_PROCESS_ATTACH
31
32 ********************************************************************/
33 extern "C" void WIXAPI WcaGlobalInitialize(
34 __in HINSTANCE hInst
35 )
36 {
37 g_hInstCADLL = hInst;
38 MemInitialize();
39
40 AssertSetModule(g_hInstCADLL);
41 AssertSetDisplayFunction(WcaDisplayAssert);
42 }
43
44
45 /********************************************************************
46 WcaGlobalFinalize() - finalizes the Wca library, should be the
47 called once per custom action Dll during
48 DllMain on DLL_PROCESS_DETACH
49
50 ********************************************************************/
51 extern "C" void WIXAPI WcaGlobalFinalize()
52 {
53 #ifdef DEBUG
54 if (WcaIsInitialized())
55 {
56 CHAR szBuf[2048];
57 StringCchPrintfA(szBuf, countof(szBuf), "CustomAction %s called WcaIniti alize() but not WcaFinalize()", WcaGetLogName());
58
59 AssertSz(FALSE, szBuf);
60 }
61 #endif
62 MemUninitialize();
63 g_hInstCADLL = NULL;
64 }
65
66
67 /********************************************************************
68 WcaInitialize() - initializes the Wca framework, should be the first
69 thing called by all CustomActions
70
71 ********************************************************************/
72 extern "C" HRESULT WIXAPI WcaInitialize(
73 __in MSIHANDLE hInstall,
74 __in_z PCSTR szCustomActionLogName
75 )
76 {
77 WCHAR wzCAFileName[MAX_PATH] = {0};
78 DWORD dwMajorVersion = 0;
79 DWORD dwMinorVersion = 0;
80
81 // these statics should be called once per CustomAction invocation.
82 // Darwin does doesn't preserve DLL state across CustomAction calls so
83 // these should always be initialized to NULL. If that behavior changes
84 // we would need to do a careful review of all of our module/global data.
85 AssertSz(!s_fInitialized, "WcaInitialize() should only be called once per Cu stomAction");
86 Assert(NULL == s_hInstall);
87 Assert(NULL == s_hDatabase);
88 Assert(0 == *s_szCustomActionLogName);
89
90 HRESULT hr = S_OK;
91
92 s_fInitialized = TRUE;
93 s_iRetVal = ERROR_SUCCESS; // assume all will go well
94
95 s_hInstall = hInstall;
96 s_hDatabase = ::MsiGetActiveDatabase(s_hInstall); // may return null if defe rred CustomAction
97
98 hr = ::StringCchCopyA(s_szCustomActionLogName, countof(s_szCustomActionLogNa me), szCustomActionLogName);
99 ExitOnFailure1(hr, "Failed to copy CustomAction log name: %s", szCustomActio nLogName);
100
101 // If we got the database handle IE: immediate CA
102 if (s_hDatabase)
103 {
104 hr = SetVerboseLoggingAtom(IsVerboseLogging());
105 ExitOnFailure(hr, "Failed to set verbose logging global atom");
106 }
107
108 if (!::GetModuleFileNameW(g_hInstCADLL, wzCAFileName, countof(wzCAFileName)) )
109 {
110 ExitWithLastError(hr, "Failed to get module filename");
111 }
112
113 FileVersion(wzCAFileName, &dwMajorVersion, &dwMinorVersion); // Ignore fail ure, just log 0.0.0.0
114
115 WcaLog(LOGMSG_VERBOSE, "Entering %s in %ls, version %u.%u.%u.%u", szCustomAc tionLogName, wzCAFileName, (DWORD)HIWORD(dwMajorVersion), (DWORD)LOWORD(dwMajorV ersion), (DWORD)HIWORD(dwMinorVersion), (DWORD)LOWORD(dwMinorVersion));
116
117 Assert(s_hInstall);
118 LExit:
119 if (FAILED(hr))
120 {
121 if (s_hDatabase)
122 {
123 ::MsiCloseHandle(s_hDatabase);
124 s_hDatabase = NULL;
125 }
126
127 s_hInstall = NULL;
128 s_fInitialized = FALSE;
129 }
130
131 return hr;
132 }
133
134
135 /********************************************************************
136 WcaFinalize() - cleans up after the Wca framework, should be the last
137 thing called by all CustomActions
138
139 ********************************************************************/
140 extern "C" UINT WIXAPI WcaFinalize(
141 __in UINT iReturnValue
142 )
143 {
144 AssertSz(!WcaIsWow64Initialized(), "WcaFinalizeWow64() should be called befo re calling WcaFinalize()");
145
146 // clean up after our initialization
147 if (s_hDatabase)
148 {
149 ::MsiCloseHandle(s_hDatabase);
150 s_hDatabase = NULL;
151 }
152
153 s_hInstall = NULL;
154 s_fInitialized = FALSE;
155
156 // if no error occurred during the processing of the CusotmAction return the passed in return value
157 // otherwise return the previous failure
158 return (ERROR_SUCCESS == s_iRetVal) ? iReturnValue : s_iRetVal;
159 }
160
161
162 /********************************************************************
163 WcaIsInitialized() - determines if WcaInitialize() has been called
164
165 ********************************************************************/
166 extern "C" BOOL WIXAPI WcaIsInitialized()
167 {
168 return s_fInitialized;
169 }
170
171
172 /********************************************************************
173 WcaGetInstallHandle() - gets the handle to the active install session
174
175 ********************************************************************/
176 extern "C" MSIHANDLE WIXAPI WcaGetInstallHandle()
177 {
178 AssertSz(s_hInstall, "WcaInitialize() should be called before attempting to access the install handle.");
179 return s_hInstall;
180 }
181
182
183 /********************************************************************
184 WcaGetDatabaseHandle() - gets the handle to the active database
185
186 NOTE: this function can only be used in immediate CustomActions.
187 Deferred CustomActions do not have access to the active
188 database.
189 ********************************************************************/
190 extern "C" MSIHANDLE WIXAPI WcaGetDatabaseHandle()
191 {
192 AssertSz(s_hDatabase, "WcaInitialize() should be called before attempting to access the install handle. Also note that deferred CustomActions do not have a ccess to the active database.");
193 return s_hDatabase;
194 }
195
196
197 /********************************************************************
198 WcaGetLogName() - gets the name of the CustomAction used in logging
199
200 ********************************************************************/
201 extern "C" const char* WIXAPI WcaGetLogName()
202 {
203 return s_szCustomActionLogName;
204 }
205
206
207 /********************************************************************
208 WcaSetReturnValue() - sets the value to return from the CustomAction
209
210 ********************************************************************/
211 extern "C" void WIXAPI WcaSetReturnValue(
212 __in UINT iReturnValue
213 )
214 {
215 s_iRetVal = iReturnValue;
216 }
217
218
219 /********************************************************************
220 WcaCancelDetected() - determines if the user has canceled yet
221
222 NOTE: returns true when WcaSetReturnValue() is set to ERROR_INSTALL_USEREXIT
223 ********************************************************************/
224 extern "C" BOOL WIXAPI WcaCancelDetected()
225 {
226 return ERROR_INSTALL_USEREXIT == s_iRetVal;
227 }
OLDNEW

Powered by Google App Engine
This is Rietveld