| 1 | // 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. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | #define IDNOACTION 0 |
| 6 | #define INITIAL_STRINGDICT_SIZE 4 |
| 7 | |
| 8 | LPCWSTR vcsDependencyProviderQuery = |
| 9 | L"SELECT `Wix4DependencyProvider`.`WixDependencyProvider`, `Wix4DependencyProvider`.`Component_`, `Wix4DependencyProvider`.`ProviderKey`, `Wix4DependencyProvider`.`Attributes` " |
| 10 | L"FROM `Wix4DependencyProvider`"; |
| 11 | enum eDependencyProviderQuery { dpqId = 1, dpqComponent, dpqProviderKey, dpqAttributes }; |
| 12 | |
| 13 | LPCWSTR vcsDependencyQuery = |
| 14 | L"SELECT `Wix4Dependency`.`WixDependency`, `Wix4DependencyProvider`.`Component_`, `Wix4Dependency`.`ProviderKey`, `Wix4Dependency`.`MinVersion`, `Wix4Dependency`.`MaxVersion`, `Wix4Dependency`.`Attributes` " |
| 15 | L"FROM `Wix4DependencyProvider`, `Wix4Dependency`, `Wix4DependencyRef` " |
| 16 | L"WHERE `Wix4Dependency`.`WixDependency` = `Wix4DependencyRef`.`WixDependency_` AND `Wix4DependencyProvider`.`WixDependencyProvider` = `Wix4DependencyRef`.`WixDependencyProvider_`"; |
| 17 | enum eDependencyComponentQuery { dqId = 1, dqComponent, dqProviderKey, dqMinVersion, dqMaxVersion, dqAttributes }; |
| 18 | |
| 19 | static HRESULT EnsureRequiredDependencies( |
| 20 | __in MSIHANDLE hInstall, |
| 21 | __in BOOL fMachineContext |
| 22 | ); |
| 23 | |
| 24 | static HRESULT EnsureAbsentDependents( |
| 25 | __in MSIHANDLE hInstall, |
| 26 | __in BOOL fMachineContext |
| 27 | ); |
| 28 | |
| 29 | static HRESULT SplitIgnoredDependents( |
| 30 | __deref_inout STRINGDICT_HANDLE* psdIgnoredDependents |
| 31 | ); |
| 32 | |
| 33 | static HRESULT CreateDependencyRecord( |
| 34 | __in int iMessageId, |
| 35 | __in_ecount(cDependencies) const DEPENDENCY* rgDependencies, |
| 36 | __in UINT cDependencies, |
| 37 | __out MSIHANDLE *phRecord |
| 38 | ); |
| 39 | |
| 40 | static LPCWSTR LogDependencyName( |
| 41 | __in_z LPCWSTR wzName |
| 42 | ); |
| 43 | |
| 44 | /*************************************************************************** |
| 45 | WixDependencyRequire - Checks that all required dependencies are installed. |
| 46 | |
| 47 | ***************************************************************************/ |
| 48 | extern "C" UINT __stdcall WixDependencyRequire( |
| 49 | __in MSIHANDLE hInstall |
| 50 | ) |
| 51 | { |
| 52 | HRESULT hr = S_OK; |
| 53 | UINT er = ERROR_SUCCESS; |
| 54 | BOOL fMachineContext = FALSE; |
| 55 | |
| 56 | hr = WcaInitialize(hInstall, "WixDependencyRequire"); |
| 57 | ExitOnFailure(hr, "Failed to initialize."); |
| 58 | |
| 59 | hr = RegInitialize(); |
| 60 | ExitOnFailure(hr, "Failed to initialize the registry functions."); |
| 61 | |
| 62 | // Determine whether we're installing per-user or per-machine. |
| 63 | fMachineContext = WcaIsPropertySet("ALLUSERS"); |
| 64 | |
| 65 | // Check for any provider components being (re)installed that their requirements are already installed. |
| 66 | hr = EnsureRequiredDependencies(hInstall, fMachineContext); |
| 67 | ExitOnFailure(hr, "Failed to ensure required dependencies for (re)installing components."); |
| 68 | |
| 69 | LExit: |
| 70 | RegUninitialize(); |
| 71 | |
| 72 | er = FAILED(hr) ? ERROR_INSTALL_FAILURE : ERROR_SUCCESS; |
| 73 | return WcaFinalize(er); |
| 74 | } |
| 75 | |
| 76 | /*************************************************************************** |
| 77 | WixDependencyCheck - Check dependencies based on component state. |
| 78 | |
| 79 | Note: may return ERROR_NO_MORE_ITEMS to terminate the session early. |
| 80 | ***************************************************************************/ |
| 81 | extern "C" UINT __stdcall WixDependencyCheck( |
| 82 | __in MSIHANDLE hInstall |
| 83 | ) |
| 84 | { |
| 85 | HRESULT hr = S_OK; |
| 86 | UINT er = ERROR_SUCCESS; |
| 87 | BOOL fMachineContext = FALSE; |
| 88 | |
| 89 | hr = WcaInitialize(hInstall, "WixDependencyCheck"); |
| 90 | ExitOnFailure(hr, "Failed to initialize."); |
| 91 | |
| 92 | hr = RegInitialize(); |
| 93 | ExitOnFailure(hr, "Failed to initialize the registry functions."); |
| 94 | |
| 95 | // Determine whether we're installing per-user or per-machine. |
| 96 | fMachineContext = WcaIsPropertySet("ALLUSERS"); |
| 97 | |
| 98 | // Check for any dependents of provider components being uninstalled. |
| 99 | hr = EnsureAbsentDependents(hInstall, fMachineContext); |
| 100 | ExitOnFailure(hr, "Failed to ensure absent dependents for uninstalling components."); |
| 101 | |
| 102 | LExit: |
| 103 | RegUninitialize(); |
| 104 | |
| 105 | er = FAILED(hr) ? ERROR_INSTALL_FAILURE : ERROR_SUCCESS; |
| 106 | return WcaFinalize(er); |
| 107 | } |
| 108 | |
| 109 | /*************************************************************************** |
| 110 | EnsureRequiredDependencies - Check that dependencies are installed for |
| 111 | any provider component that is being installed or reinstalled. |
| 112 | |
| 113 | Note: Skipped if DISABLEDEPENDENCYCHECK is set. |
| 114 | ***************************************************************************/ |
| 115 | static HRESULT EnsureRequiredDependencies( |
| 116 | __in MSIHANDLE /*hInstall*/, |
| 117 | __in BOOL fMachineContext |
| 118 | ) |
| 119 | { |
| 120 | HRESULT hr = S_OK; |
| 121 | DWORD er = ERROR_SUCCESS; |
| 122 | STRINGDICT_HANDLE sdDependencies = NULL; |
| 123 | HKEY hkHive = NULL; |
| 124 | PMSIHANDLE hView = NULL; |
| 125 | PMSIHANDLE hRec = NULL; |
| 126 | LPWSTR sczId = NULL; |
| 127 | LPWSTR sczComponent = NULL; |
| 128 | LPWSTR sczProviderKey = NULL; |
| 129 | LPWSTR sczMinVersion = NULL; |
| 130 | LPWSTR sczMaxVersion = NULL; |
| 131 | int iAttributes = 0; |
| 132 | WCA_TODO tComponentAction = WCA_TODO_UNKNOWN; |
| 133 | DEPENDENCY* rgDependencies = NULL; |
| 134 | UINT cDependencies = 0; |
| 135 | PMSIHANDLE hDependencyRec = NULL; |
| 136 | |
| 137 | // Skip the dependency check if the Wix4Dependency table is missing (no dependencies to check for). |
| 138 | hr = WcaTableExists(L"Wix4Dependency"); |
| 139 | if (S_FALSE == hr) |
| 140 | { |
| 141 | WcaLog(LOGMSG_STANDARD, "Skipping the dependency check since no dependencies are authored."); |
| 142 | ExitFunction1(hr = S_OK); |
| 143 | } |
| 144 | |
| 145 | // If the table exists but not the others, the database was not authored correctly. |
| 146 | ExitOnFailure(hr, "Failed to check if the Wix4Dependency table exists."); |
| 147 | |
| 148 | // Initialize the dictionary to keep track of unique dependency keys. |
| 149 | hr = DictCreateStringList(&sdDependencies, INITIAL_STRINGDICT_SIZE, DICT_FLAG_CASEINSENSITIVE); |
| 150 | ExitOnFailure(hr, "Failed to initialize the unique dependency string list."); |
| 151 | |
| 152 | // Set the registry hive to use depending on install context. |
| 153 | hkHive = fMachineContext ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
| 154 | |
| 155 | // Loop over the provider components. |
| 156 | hr = WcaOpenExecuteView(vcsDependencyQuery, &hView); |
| 157 | ExitOnFailure(hr, "Failed to open the query view for dependencies."); |
| 158 | |
| 159 | while (S_OK == (hr = WcaFetchRecord(hView, &hRec))) |
| 160 | { |
| 161 | hr = WcaGetRecordString(hRec, dqId, &sczId); |
| 162 | ExitOnFailure(hr, "Failed to get Wix4Dependency.WixDependency."); |
| 163 | |
| 164 | hr = WcaGetRecordString(hRec, dqComponent, &sczComponent); |
| 165 | ExitOnFailure(hr, "Failed to get Wix4DependencyProvider.Component_."); |
| 166 | |
| 167 | // Skip the current component if its not being installed or reinstalled. |
| 168 | tComponentAction = WcaGetComponentToDo(sczComponent); |
| 169 | if (WCA_TODO_INSTALL != tComponentAction && WCA_TODO_REINSTALL != tComponentAction) |
| 170 | { |
| 171 | WcaLog(LOGMSG_STANDARD, "Skipping dependency check for %ls because the component %ls is not being (re)installed.", sczId, sczComponent); |
| 172 | continue; |
| 173 | } |
| 174 | |
| 175 | hr = WcaGetRecordString(hRec, dqProviderKey, &sczProviderKey); |
| 176 | ExitOnFailure(hr, "Failed to get Wix4Dependency.ProviderKey."); |
| 177 | |
| 178 | hr = WcaGetRecordString(hRec, dqMinVersion, &sczMinVersion); |
| 179 | ExitOnFailure(hr, "Failed to get Wix4Dependency.MinVersion."); |
| 180 | |
| 181 | hr = WcaGetRecordString(hRec, dqMaxVersion, &sczMaxVersion); |
| 182 | ExitOnFailure(hr, "Failed to get Wix4Dependency.MaxVersion."); |
| 183 | |
| 184 | hr = WcaGetRecordInteger(hRec, dqAttributes, &iAttributes); |
| 185 | ExitOnFailure(hr, "Failed to get Wix4Dependency.Attributes."); |
| 186 | |
| 187 | // Check the registry to see if the required providers (dependencies) exist. |
| 188 | hr = DepCheckDependency(hkHive, sczProviderKey, sczMinVersion, sczMaxVersion, iAttributes, sdDependencies, &rgDependencies, &cDependencies); |
| 189 | if (E_NOTFOUND != hr) |
| 190 | { |
| 191 | ExitOnFailure(hr, "Failed dependency check for %ls.", sczId); |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | if (E_NOMOREITEMS != hr) |
| 196 | { |
| 197 | ExitOnFailure(hr, "Failed to enumerate all of the rows in the dependency query view."); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | hr = S_OK; |
| 202 | } |
| 203 | |
| 204 | // If we collected any dependencies in the previous check, pump a message and prompt the user. |
| 205 | if (0 < cDependencies) |
| 206 | { |
| 207 | hr = CreateDependencyRecord(msierrDependencyMissingDependencies, rgDependencies, cDependencies, &hDependencyRec); |
| 208 | ExitOnFailure(hr, "Failed to create the dependency record for message %d.", msierrDependencyMissingDependencies); |
| 209 | |
| 210 | // Send a yes/no message with a warning icon since continuing could be detrimental. |
| 211 | // This is sent as a USER message to better detect whether a user or dependency-aware bootstrapper is responding |
| 212 | // or if Windows Installer or a dependency-unaware bootstrapper is returning a typical default response. |
| 213 | er = WcaProcessMessage(static_cast<INSTALLMESSAGE>(INSTALLMESSAGE_USER | MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2), hDependencyRec); |
| 214 | switch (er) |
| 215 | { |
| 216 | // Only a user or dependency-aware bootstrapper that prompted the user should return IDYES to continue anyway. |
| 217 | case IDYES: |
| 218 | ExitFunction1(hr = S_OK); |
| 219 | |
| 220 | // Only a user or dependency-aware bootstrapper that prompted the user should return IDNO to terminate the operation. |
| 221 | case IDNO: |
| 222 | WcaSetReturnValue(ERROR_INSTALL_USEREXIT); |
| 223 | ExitFunction1(hr = S_OK); |
| 224 | |
| 225 | // A dependency-aware bootstrapper should return IDCANCEL if running silently and the operation should be canceled. |
| 226 | case IDCANCEL: |
| 227 | __fallthrough; |
| 228 | |
| 229 | // Bootstrappers which are not dependency-aware may return IDOK for unhandled messages. |
| 230 | case IDOK: |
| 231 | __fallthrough; |
| 232 | |
| 233 | // Windows Installer returns 0 for USER messages when silent or passive, or when a bootstrapper does not handle the message. |
| 234 | case IDNOACTION: |
| 235 | WcaSetReturnValue(ERROR_INSTALL_FAILURE); |
| 236 | ExitFunction1(hr = S_OK); |
| 237 | |
| 238 | default: |
| 239 | ExitOnFailure(hr = E_UNEXPECTED, "Unexpected message response %d from user or bootstrapper application.", er); |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | LExit: |
| 244 | ReleaseDependencyArray(rgDependencies, cDependencies); |
| 245 | ReleaseStr(sczId); |
| 246 | ReleaseStr(sczComponent); |
| 247 | ReleaseStr(sczProviderKey); |
| 248 | ReleaseStr(sczMinVersion); |
| 249 | ReleaseStr(sczMaxVersion); |
| 250 | ReleaseDict(sdDependencies); |
| 251 | |
| 252 | return hr; |
| 253 | } |
| 254 | |
| 255 | /*************************************************************************** |
| 256 | EnsureAbsentDependents - Checks that there are no dependents |
| 257 | registered for providers that are being uninstalled. |
| 258 | |
| 259 | Note: Skipped if UPGRADINGPRODUCTCODE is set. |
| 260 | ***************************************************************************/ |
| 261 | static HRESULT EnsureAbsentDependents( |
| 262 | __in MSIHANDLE /*hInstall*/, |
| 263 | __in BOOL fMachineContext |
| 264 | ) |
| 265 | { |
| 266 | HRESULT hr = S_OK; |
| 267 | DWORD er = ERROR_SUCCESS; |
| 268 | STRINGDICT_HANDLE sdIgnoredDependents = NULL; |
| 269 | HKEY hkHive = NULL; |
| 270 | PMSIHANDLE hView = NULL; |
| 271 | PMSIHANDLE hRec = NULL; |
| 272 | LPWSTR sczId = NULL; |
| 273 | LPWSTR sczComponent = NULL; |
| 274 | LPWSTR sczProviderKey = NULL; |
| 275 | int iAttributes = 0; |
| 276 | WCA_TODO tComponentAction = WCA_TODO_UNKNOWN; |
| 277 | DEPENDENCY* rgDependents = NULL; |
| 278 | UINT cDependents = 0; |
| 279 | PMSIHANDLE hDependencyRec = NULL; |
| 280 | BOOL fExists = FALSE; |
| 281 | |
| 282 | // Skip the dependent check if the Wix4DependencyProvider table is missing (no dependency providers). |
| 283 | hr = WcaTableExists(L"Wix4DependencyProvider"); |
| 284 | if (S_FALSE == hr) |
| 285 | { |
| 286 | WcaLog(LOGMSG_STANDARD, "Skipping the dependents check since no dependency providers are authored."); |
| 287 | ExitFunction1(hr = S_OK); |
| 288 | } |
| 289 | |
| 290 | ExitOnFailure(hr, "Failed to check if the Wix4DependencyProvider table exists."); |
| 291 | |
| 292 | // Split the IGNOREDEPENDENCIES property for use below if set. If it is "ALL", then quit now. |
| 293 | hr = SplitIgnoredDependents(&sdIgnoredDependents); |
| 294 | ExitOnFailure(hr, "Failed to get the ignored dependents."); |
| 295 | |
| 296 | hr = DictKeyExists(sdIgnoredDependents, L"ALL"); |
| 297 | if (E_NOTFOUND != hr) |
| 298 | { |
| 299 | ExitOnFailure(hr, "Failed to check if \"ALL\" was set in IGNOREDEPENDENCIES."); |
| 300 | |
| 301 | // Otherwise... |
| 302 | WcaLog(LOGMSG_STANDARD, "Skipping the dependencies check since IGNOREDEPENDENCIES contains \"ALL\"."); |
| 303 | ExitFunction(); |
| 304 | } |
| 305 | else |
| 306 | { |
| 307 | // Key was not found, so proceed. |
| 308 | hr = S_OK; |
| 309 | } |
| 310 | |
| 311 | // Set the registry hive to use depending on install context. |
| 312 | hkHive = fMachineContext ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; |
| 313 | |
| 314 | // Loop over the provider components. |
| 315 | hr = WcaOpenExecuteView(vcsDependencyProviderQuery, &hView); |
| 316 | ExitOnFailure(hr, "Failed to open the query view for dependency providers."); |
| 317 | |
| 318 | while (S_OK == (hr = WcaFetchRecord(hView, &hRec))) |
| 319 | { |
| 320 | hr = WcaGetRecordString(hRec, dpqId, &sczId); |
| 321 | ExitOnFailure(hr, "Failed to get Wix4DependencyProvider.WixDependencyProvider."); |
| 322 | |
| 323 | hr = WcaGetRecordString(hRec, dpqComponent, &sczComponent); |
| 324 | ExitOnFailure(hr, "Failed to get Wix4DependencyProvider.Component."); |
| 325 | |
| 326 | // Skip the current component if its not being uninstalled. |
| 327 | tComponentAction = WcaGetComponentToDo(sczComponent); |
| 328 | if (WCA_TODO_UNINSTALL != tComponentAction) |
| 329 | { |
| 330 | WcaLog(LOGMSG_STANDARD, "Skipping dependents check for %ls because the component %ls is not being uninstalled.", sczId, sczComponent); |
| 331 | continue; |
| 332 | } |
| 333 | |
| 334 | hr = WcaGetRecordString(hRec, dpqProviderKey, &sczProviderKey); |
| 335 | ExitOnFailure(hr, "Failed to get Wix4DependencyProvider.ProviderKey."); |
| 336 | |
| 337 | hr = WcaGetRecordInteger(hRec, dpqAttributes, &iAttributes); |
| 338 | ExitOnFailure(hr, "Failed to get Wix4DependencyProvider.Attributes."); |
| 339 | |
| 340 | // Check the registry to see if the provider has any dependents registered. |
| 341 | hr = DepCheckDependents(hkHive, sczProviderKey, iAttributes, sdIgnoredDependents, &rgDependents, &cDependents); |
| 342 | ExitOnPathFailure(hr, fExists, "Failed dependents check for %ls.", sczId); |
| 343 | } |
| 344 | |
| 345 | if (E_NOMOREITEMS != hr) |
| 346 | { |
| 347 | ExitOnFailure(hr, "Failed to enumerate all of the rows in the dependency provider query view."); |
| 348 | } |
| 349 | else |
| 350 | { |
| 351 | hr = S_OK; |
| 352 | } |
| 353 | |
| 354 | // If we collected any providers with dependents in the previous check, pump a message and prompt the user. |
| 355 | if (0 < cDependents) |
| 356 | { |
| 357 | hr = CreateDependencyRecord(msierrDependencyHasDependents, rgDependents, cDependents, &hDependencyRec); |
| 358 | ExitOnFailure(hr, "Failed to create the dependency record for message %d.", msierrDependencyHasDependents); |
| 359 | |
| 360 | // Send a yes/no message with a warning icon since continuing could be detrimental. |
| 361 | // This is sent as a USER message to better detect whether a user or dependency-aware bootstrapper is responding |
| 362 | // or if Windows Installer or a dependency-unaware bootstrapper is returning a typical default response. |
| 363 | er = WcaProcessMessage(static_cast<INSTALLMESSAGE>(INSTALLMESSAGE_USER | MB_ICONWARNING | MB_YESNO | MB_DEFBUTTON2), hDependencyRec); |
| 364 | switch (er) |
| 365 | { |
| 366 | // Only a user or dependency-aware bootstrapper that prompted the user should return IDYES to continue anyway. |
| 367 | case IDYES: |
| 368 | ExitFunction1(hr = S_OK); |
| 369 | |
| 370 | // Only a user or dependency-aware bootstrapper that prompted the user should return IDNO to terminate the operation. |
| 371 | case IDNO: |
| 372 | __fallthrough; |
| 373 | |
| 374 | // Bootstrappers which are not dependency-aware may return IDOK for unhandled messages. |
| 375 | case IDOK: |
| 376 | __fallthrough; |
| 377 | |
| 378 | // Windows Installer returns 0 for USER messages when silent or passive, or when a bootstrapper does not handle the message. |
| 379 | case IDNOACTION: |
| 380 | WcaSetReturnValue(ERROR_NO_MORE_ITEMS); |
| 381 | ExitFunction1(hr = S_OK); |
| 382 | |
| 383 | // A dependency-aware bootstrapper should return IDCANCEL if running silently and the operation should be canceled. |
| 384 | case IDCANCEL: |
| 385 | WcaSetReturnValue(ERROR_INSTALL_FAILURE); |
| 386 | ExitFunction1(hr = S_OK); |
| 387 | |
| 388 | default: |
| 389 | hr = E_UNEXPECTED; |
| 390 | ExitOnFailure(hr, "Unexpected message response %d from user or bootstrapper application.", er); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | LExit: |
| 395 | ReleaseDependencyArray(rgDependents, cDependents); |
| 396 | ReleaseStr(sczId); |
| 397 | ReleaseStr(sczComponent); |
| 398 | ReleaseStr(sczProviderKey); |
| 399 | |
| 400 | return hr; |
| 401 | } |
| 402 | |
| 403 | /*************************************************************************** |
| 404 | SplitIgnoredDependents - Splits the IGNOREDEPENDENCIES property into a map. |
| 405 | |
| 406 | ***************************************************************************/ |
| 407 | static HRESULT SplitIgnoredDependents( |
| 408 | __deref_inout STRINGDICT_HANDLE* psdIgnoredDependents |
| 409 | ) |
| 410 | { |
| 411 | HRESULT hr = S_OK; |
| 412 | LPWSTR sczIgnoreDependencies = NULL; |
| 413 | LPCWSTR wzDelim = L";"; |
| 414 | LPWSTR wzContext = NULL; |
| 415 | |
| 416 | hr = WcaGetProperty(L"IGNOREDEPENDENCIES", &sczIgnoreDependencies); |
| 417 | ExitOnFailure(hr, "Failed to get the string value of the IGNOREDEPENDENCIES property."); |
| 418 | |
| 419 | hr = DictCreateStringList(psdIgnoredDependents, INITIAL_STRINGDICT_SIZE, DICT_FLAG_CASEINSENSITIVE); |
| 420 | ExitOnFailure(hr, "Failed to create the string dictionary."); |
| 421 | |
| 422 | // Parse through the semicolon-delimited tokens and add to the string dictionary. |
| 423 | for (LPCWSTR wzToken = ::wcstok_s(sczIgnoreDependencies, wzDelim, &wzContext); wzToken; wzToken = ::wcstok_s(NULL, wzDelim, &wzContext)) |
| 424 | { |
| 425 | hr = DictAddKey(*psdIgnoredDependents, wzToken); |
| 426 | ExitOnFailure(hr, "Failed to ignored dependency \"%ls\" to the string dictionary.", wzToken); |
| 427 | } |
| 428 | |
| 429 | LExit: |
| 430 | ReleaseStr(sczIgnoreDependencies); |
| 431 | |
| 432 | return hr; |
| 433 | } |
| 434 | |
| 435 | /*************************************************************************** |
| 436 | CreateDependencyRecord - Creates a record containing the message template |
| 437 | and records to send to the UI handler. |
| 438 | |
| 439 | Notes: Callers should call WcaProcessMessage and handle return codes. |
| 440 | ***************************************************************************/ |
| 441 | static HRESULT CreateDependencyRecord( |
| 442 | __in int iMessageId, |
| 443 | __in_ecount(cDependencies) const DEPENDENCY* rgDependencies, |
| 444 | __in UINT cDependencies, |
| 445 | __out MSIHANDLE *phRecord |
| 446 | ) |
| 447 | { |
| 448 | HRESULT hr = S_OK; |
| 449 | UINT er = ERROR_SUCCESS; |
| 450 | UINT cParams = 0; |
| 451 | UINT iParam = 0; |
| 452 | |
| 453 | // Should not be PMSIHANDLE. |
| 454 | MSIHANDLE hRec = NULL; |
| 455 | |
| 456 | // Calculate the number of parameters based on the format: |
| 457 | // msgId, count, key1, name1, key2, name2, etc. |
| 458 | cParams = 2 + 2 * cDependencies; |
| 459 | |
| 460 | hRec = ::MsiCreateRecord(cParams); |
| 461 | ExitOnNull(hRec, hr, E_OUTOFMEMORY, "Not enough memory to create the message record."); |
| 462 | |
| 463 | er = ::MsiRecordSetInteger(hRec, ++iParam, iMessageId); |
| 464 | ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to set the message identifier into the message record."); |
| 465 | |
| 466 | er = ::MsiRecordSetInteger(hRec, ++iParam, cDependencies); |
| 467 | ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to set the number of dependencies into the message record."); |
| 468 | |
| 469 | // Now loop through each dependency and add the key and name to the record. |
| 470 | for (UINT i = 0; i < cDependencies; i++) |
| 471 | { |
| 472 | const DEPENDENCY* pDependency = &rgDependencies[i]; |
| 473 | |
| 474 | // Log message type-specific information. |
| 475 | switch (iMessageId) |
| 476 | { |
| 477 | // Send a user message when installing a component that is missing some dependencies. |
| 478 | case msierrDependencyMissingDependencies: |
| 479 | WcaLog(LOGMSG_VERBOSE, "The dependency \"%ls\" is missing or is not the required version.", pDependency->sczKey); |
| 480 | break; |
| 481 | |
| 482 | // Send a user message when uninstalling a component that still has registered dependents. |
| 483 | case msierrDependencyHasDependents: |
| 484 | WcaLog(LOGMSG_VERBOSE, "Found dependent \"%ls\", name: \"%ls\".", pDependency->sczKey, LogDependencyName(pDependency->sczName)); |
| 485 | break; |
| 486 | } |
| 487 | |
| 488 | er = ::MsiRecordSetStringW(hRec, ++iParam, pDependency->sczKey); |
| 489 | ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to set the dependency key \"%ls\" into the message record.", pDependency->sczKey); |
| 490 | |
| 491 | er = ::MsiRecordSetStringW(hRec, ++iParam, pDependency->sczName); |
| 492 | ExitOnFailure(hr = HRESULT_FROM_WIN32(er), "Failed to set the dependency name \"%ls\" into the message record.", pDependency->sczName); |
| 493 | } |
| 494 | |
| 495 | // Only assign the out parameter if successful to this point. |
| 496 | *phRecord = hRec; |
| 497 | hRec = NULL; |
| 498 | |
| 499 | LExit: |
| 500 | if (hRec) |
| 501 | { |
| 502 | ::MsiCloseHandle(hRec); |
| 503 | } |
| 504 | |
| 505 | return hr; |
| 506 | } |
| 507 | |
| 508 | /*************************************************************************** |
| 509 | LogDependencyName - Returns the dependency name or "Unknown" if null. |
| 510 | |
| 511 | ***************************************************************************/ |
| 512 | static LPCWSTR LogDependencyName( |
| 513 | __in_z LPCWSTR wzName |
| 514 | ) |
| 515 | { |
| 516 | return wzName ? wzName : L"Unknown"; |
| 517 | } |