aboutsummaryrefslogtreecommitdiff
path: root/src/libs/wcautil/WixToolset.WcaUtil/wcautil.cpp
blob: 11867d101874dcd7ec1f6ea6cf4396197cf27b49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
// Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information.

#include "precomp.h"

// globals
HMODULE g_hInstCADLL;

// statics
static BOOL s_fInitialized;
static MSIHANDLE s_hInstall;
static MSIHANDLE s_hDatabase;
static char s_szCustomActionLogName[32];
static UINT s_iRetVal;

static void CALLBACK WcaTraceError(
    __in_z LPCSTR szFile,
    __in int iLine,
    __in REPORT_LEVEL rl,
    __in UINT source,
    __in HRESULT hrError,
    __in_z __format_string LPCSTR szFormat,
    __in va_list args
    );

/********************************************************************
 WcaGlobalInitialize() - initializes the Wca library, should be 
                         called once per custom action Dll during
                         DllMain on DLL_PROCESS_ATTACH

********************************************************************/
extern "C" void WIXAPI WcaGlobalInitialize(
    __in HINSTANCE hInst
    )
{
    g_hInstCADLL = hInst;
    DutilInitialize(&WcaTraceError);
    MemInitialize();

    AssertSetModule(g_hInstCADLL);
    AssertSetDisplayFunction(WcaDisplayAssert);
}


/********************************************************************
 WcaGlobalFinalize() - finalizes the Wca library, should be the
                       called once per custom action Dll during
                       DllMain on DLL_PROCESS_DETACH

********************************************************************/
extern "C" void WIXAPI WcaGlobalFinalize()
{
#ifdef DEBUG
    if (WcaIsInitialized())
    {
        CHAR szBuf[2048];
        StringCchPrintfA(szBuf, countof(szBuf), "CustomAction %s called WcaInitialize() but not WcaFinalize()", WcaGetLogName());

        AssertSz(FALSE, szBuf);
    }
#endif
    MemUninitialize();
    DutilUninitialize();
    g_hInstCADLL = NULL;
}


/********************************************************************
 WcaInitialize() - initializes the Wca framework, should be the first 
                   thing called by all CustomActions

********************************************************************/
extern "C" HRESULT WIXAPI WcaInitialize(
    __in MSIHANDLE hInstall,
    __in_z PCSTR szCustomActionLogName
    )
{
    WCHAR wzCAFileName[MAX_PATH] = {0};
    DWORD dwMajorVersion = 0; 
    DWORD dwMinorVersion = 0; 

    // these statics should be called once per CustomAction invocation.
    // Darwin does doesn't preserve DLL state across CustomAction calls so
    // these should always be initialized to NULL.  If that behavior changes
    // we would need to do a careful review of all of our module/global data.
    AssertSz(!s_fInitialized, "WcaInitialize() should only be called once per CustomAction");
    Assert(NULL == s_hInstall);
    Assert(NULL == s_hDatabase);
    Assert(0 == *s_szCustomActionLogName);

    HRESULT hr = S_OK;

    s_fInitialized = TRUE;
    s_iRetVal = ERROR_SUCCESS; // assume all will go well

    s_hInstall = hInstall;
    s_hDatabase = ::MsiGetActiveDatabase(s_hInstall); // may return null if deferred CustomAction

    hr = ::StringCchCopy(s_szCustomActionLogName, countof(s_szCustomActionLogName), szCustomActionLogName);
    ExitOnFailure(hr, "Failed to copy CustomAction log name: %s", szCustomActionLogName);

    // If we got the database handle IE: immediate CA
    if (s_hDatabase)
    {
        hr = SetVerboseLoggingAtom(IsVerboseLogging());
        ExitOnFailure(hr, "Failed to set verbose logging global atom");
    }

    if (!::GetModuleFileNameW(g_hInstCADLL, wzCAFileName, countof(wzCAFileName)))
    {
        ExitWithLastError(hr, "Failed to get module filename");
    }

    FileVersion(wzCAFileName, &dwMajorVersion, &dwMinorVersion);  // Ignore failure, just log 0.0.0.0

    WcaLog(LOGMSG_VERBOSE, "Entering %s in %ls, version %u.%u.%u.%u", szCustomActionLogName, wzCAFileName, (DWORD)HIWORD(dwMajorVersion), (DWORD)LOWORD(dwMajorVersion), (DWORD)HIWORD(dwMinorVersion), (DWORD)LOWORD(dwMinorVersion));

    Assert(s_hInstall);
LExit:
    if (FAILED(hr))
    {
        if (s_hDatabase)
        {
            ::MsiCloseHandle(s_hDatabase);
            s_hDatabase = NULL;
        }

        s_hInstall = NULL;
        s_fInitialized = FALSE;
    }

    return hr;
}


/********************************************************************
 WcaFinalize() - cleans up after the Wca framework, should be the last 
                 thing called by all CustomActions

********************************************************************/
extern "C" UINT WIXAPI WcaFinalize(
    __in UINT iReturnValue
    )
{
    AssertSz(!WcaIsWow64Initialized(), "WcaFinalizeWow64() should be called before calling WcaFinalize()");

    // clean up after our initialization
    if (s_hDatabase)
    {
        ::MsiCloseHandle(s_hDatabase);
        s_hDatabase = NULL;
    }

    s_hInstall = NULL;
    s_fInitialized = FALSE;

    // if no error occurred during the processing of the CusotmAction return the passed in return value
    // otherwise return the previous failure
    return (ERROR_SUCCESS == s_iRetVal) ? iReturnValue : s_iRetVal; 
}


/********************************************************************
 WcaIsInitialized() - determines if WcaInitialize() has been called

********************************************************************/
extern "C" BOOL WIXAPI WcaIsInitialized()
{
    return s_fInitialized;
}


/********************************************************************
 WcaGetInstallHandle() - gets the handle to the active install session

********************************************************************/
extern "C" MSIHANDLE WIXAPI WcaGetInstallHandle()
{
    AssertSz(s_hInstall, "WcaInitialize() should be called before attempting to access the install handle.");
    return s_hInstall;
}


/********************************************************************
 WcaGetDatabaseHandle() - gets the handle to the active database

 NOTE: this function can only be used in immediate CustomActions.
       Deferred CustomActions do not have access to the active
       database.
********************************************************************/
extern "C" MSIHANDLE WIXAPI WcaGetDatabaseHandle()
{
    AssertSz(s_hDatabase, "WcaInitialize() should be called before attempting to access the install handle.  Also note that deferred CustomActions do not have access to the active database.");
    return s_hDatabase;
}


/********************************************************************
 WcaGetLogName() - gets the name of the CustomAction used in logging

********************************************************************/
extern "C" const char* WIXAPI WcaGetLogName()
{
    return s_szCustomActionLogName;
}


/********************************************************************
 WcaSetReturnValue() - sets the value to return from the CustomAction

********************************************************************/
extern "C" void WIXAPI WcaSetReturnValue(
    __in UINT iReturnValue
    )
{
    s_iRetVal = iReturnValue;
}


/********************************************************************
 WcaCancelDetected() - determines if the user has canceled yet

 NOTE: returns true when WcaSetReturnValue() is set to ERROR_INSTALL_USEREXIT
********************************************************************/
extern "C" BOOL WIXAPI WcaCancelDetected()
{
    return ERROR_INSTALL_USEREXIT == s_iRetVal;
}

static void CALLBACK WcaTraceError(
    __in_z LPCSTR /*szFile*/,
    __in int /*iLine*/,
    __in REPORT_LEVEL /*rl*/,
    __in UINT source,
    __in HRESULT hrError,
    __in_z __format_string LPCSTR szFormat,
    __in va_list args
    )
{
    if (DUTIL_SOURCE_DEFAULT == source)
    {
        WcaLogErrorArgs(hrError, szFormat, args);
    }
}