TAP: Await anything (not only Tasks)

The async/await pattern is simple yet effective way to work with tasks. Its ingeniousity is that the programmer is focused on implementation of the function at the first place, leaving the technical details to the compiler which does some behind-the-scenes magic to make sure that async methods can:

  • run asynchronously,
  • capture or disregard the context (via ConfigureAwait(bool))
  • use all benefits of TAP (Task-based Asynchronous Pattern)

A nice touch of the async/await syntax is that it is easy to convert any synchronous method into its asynchronous counterpart, without sacrificing code clarity or increasing its complexity.

Interestingly enough, the await keyword is not really limited to Tasks, and as a matter of fact it can be used for any object for which it makes sense to wait for. This blog post is devoted to defining a class which can be awaited with await keyword, and yet is by no mean a Task at all!

So what is the compiler doing and how to make sure it understands our custom awaiting desire? In order to demonstrate, let’s try the following code which uses a plain AutoResetEvent. One thing to notice is that – instead of WaitOne() in line 21 – we try to await it by adding the await keyword before.
Continue reading “TAP: Await anything (not only Tasks)”

MsiClient PowerShell Module

Based on my previous post, I created a small project on my github and adjusted existing codebase to provide a simplistic PowerShell module. The code has been reorganized into proper units with exported functions. Here is a sample usage:

$Header = @"
  TABLE {border: 1px solid gray collapse; }
  TH { border: 1px solid gray; padding: 6px 4px; background-color: #eaeaea; }
  TD { border: 1px solid gray; padding: 4px; }
Import-Module MsiClient
Get-MsiClientPackage | where { $_.Publisher -contains 'Microsoft Corporation' } | sort-object -Property ProductName | ConvertTo-Html -Property ProductCode,PackageName,ProductName,VersionString,Language,Publisher -Head $Header | Out-File c:\temp\test.html

The above snippet produces a HTML based report, showing all Microsoft products available on the current system.

There are still lots of TODOs, which I am going to cover soon, for example:

  • More methods supported (install, uninstall, repair etc.)
  • Better exception handling
  • Support for -WhatIf and -Confirm switches

In any case, my design goal is to keep the syntax similar to functions available in the AppVClient module, so that ideally for basic scenarios they have a 1-1 mapping in the MSI counterpart.

Link to my guthub (the project is licensed under MIT):

Enumerating installed MSI products with PowerShell and msi.dll

If you were ever wondering how to properly read the list of installed MSI software, then two popular choices are available:

  • Querying uninstall registry keys (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall), filtering out-non MSI entries and outputting the rest
  • Using Win32_Product from WMI

They both have their pros and cons. Querying registry is straightforward on its own, but requires awkward manipulations and accessing the data which is actually backing the Add/Remove Applet, not necessarily the Windows Installer API which uses its own complex registration. Additionally it may not work correctly with different installation context (user/machine) and you may have to query two places to get both x86 and x64 installations on a x64 system.

On the other hand, while WMI query is also pretty straightforward (seeĀ https://msdn.microsoft.com/en-us/library/aa394378(v=VS.85).aspx?f=255&MSPPError=-2147217396), it has a really big drawback. Running it is painfully slow, because Windows Installer checks integrity of each entry and triggers appropriate action (for example repair) if necessary.

So to have a solution which is both fast, reliable and without any side-effects, you may go for a third solution which is more complex, but once setup can be reused not only for querying but for a whole management of MSI-based installations. And so this blog today will be about P/invoking native msi.dll to get results returned by the true Windows Installer API.

This post may be too technical if you have never programmed in C/C++ or C#. If you just want the results without understanding how to implement them on your own, scroll to the bottom, the full content of the PowerShell script is there.
Continue reading “Enumerating installed MSI products with PowerShell and msi.dll”

Make VMware Workstation 14 and VIX COM API work together

After installing newest VMware Workstation 14, it turned out that the automation API stopped working with the following error

Retrieving the COM class factory for component with CLSID{COMPONENT-ID} failed due to the following error: 80040154 Class not registered(Exception from HRESULT: 0x80040154(REGDB_E_CLASSNOTREG)).

It seems that the newest version does not install two important COM DLL files (and consequently does not register them): VIXCOM.dll and VIXCOM64.dll. The solution is to copy them over from older installation and register them (as administrator):

regsvr32.exe VIXCOM.dll
regsvr32.exe VIXCOM64.dll

Things start working after this little trick. Alternatively, a full VIX API 1.15 has to be installed (it is available as a separate download from VMware support pages).

The problem has been tackled by our support and is described here

Note: Since both DLLs can be freely distributed, you can download them from the above link in case you don’t have older installation of VMware Workstation.

Files not installed when applying a Minor Upgrade and how to troubleshoot it

While analyzing one of customer issues reported to me, I came accross an interesting problem which – having prior knowledge how it works – would be solved in minutes, not in hours and days as it really took.

The problem

The customer reported that an MSP patch created by RayPack works fine, but not really up to his expectations. While being considerably smaller than a full MSI upgrade, the installation took a while because the previous version must have been uninstalled and installed again in the background. This is basically the idea how major upgrades are working, and the customer wanted to switch to a typical patching, which (depending on conditions) would mean going for a small update or a minor update.

We suggested to him that while usually not recommended in packaging projects, minor upgrades are different from major upgrades that they must not change ProductCode, aside of some other limitations. The customer tried it anyway and reported that our suggestion initially helped, only to report some minutes later that it actually hadn’t. The new version of product had been apparently installed (and visible in ARP), but the files were not updated – new files were still missing, binary changes were not applied etc. There were no errors during update, the msiexec exit code was also fine.

Another issue was, that after installing the patch, self-repair stopped worked for random components. Removing certain key path resources triggered repair, while for others it did not work. Weird.

Analysis + findings

Our analysis indicated that it was not actually a problem with the patch itself, but more like with the way Minor Upgrades are working, and thereof the problem could be easily reproduced by installing the second MSI containing a minor upgrade with a command line:

msiexec /i <path> REINSTALLMODE=vamus REINSTALL=all /qb

(note: vamusis non-standard, but was chosen for troubleshooting to also overwrite files in the same version). It is important to indicate that the cached MSI has to be recached from source (thus v in the reinstall mode), otherwise you obviously can’t install an MSI as minor upgrade.

We don’t do that many minor upgrades, but enough to be aware of certain limitations, including that names of MSIs must be the same (so that Product2.0.msi cannot do a minor upgrade of Product1.0.msi etc.), the feature trees must remain unchanged, some other conditions apply (more on msdn sites).

If you ever encounter a problem like that, have some time saved by this ultimate troubleshooting:
Continue reading “Files not installed when applying a Minor Upgrade and how to troubleshoot it”