Skip to content

Commit

Permalink
win,msi: change InstallScope to perMachine
Browse files Browse the repository at this point in the history
The MSI install scope was set to the WiX default, which is per-user.
However, with UAC, it could not be installed by a standard user because
InstallPrivileges is elevated by default, hence the install scope
should be set to per-machine. Furthermore, the default install path is
a per-machine location and setting the system path requires
administrator privileges.

By changing the InstallScope to perMachine, Start Menu shortcuts are
placed in ProgramData and not the installing user's AppData folder,
making the shortcuts available to other users. This also fixes the
installation when AppData is a network folder.

The custom action is necessary to allow upgrades. Since a per-machine
MSI cannot upgrade an application installed per-user, the custom action
checks if there is going to be an upgrade to a previous version
installed per-user and sets the installation as per-user to allow
upgrading. Hence, the advantages of installing per-machine will only
apply in fresh installations.

Fixes nodejs#5849
Fixes nodejs#7629

PR-URL: nodejs/node-v0.x-archive#25640
Reviewed-By: Alexis Campailla <[email protected]>
Reviewed-By: Bert Belder <[email protected]>
  • Loading branch information
joaocgreis authored and orangemocha committed Jul 22, 2015
1 parent b8d47a7 commit 8e80528
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 5 deletions.
52 changes: 51 additions & 1 deletion tools/msvs/msi/custom_actions.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,60 @@

#define WIN32_LEAN_AND_MEAN

#include <windows.h>
#include <msiquery.h>
#include <wcautil.h>

#define GUID_BUFFER_SIZE 39 // {8-4-4-4-12}\0


UINT WINAPI SetInstallScope(MSIHANDLE hInstall) {
HRESULT hr = S_OK;
UINT er = ERROR_SUCCESS;
TCHAR upgrade_code[GUID_BUFFER_SIZE];
DWORD upgrade_code_len = GUID_BUFFER_SIZE;
DWORD iProductIndex;
TCHAR product_code[GUID_BUFFER_SIZE];
TCHAR assignment_type[2];
DWORD assignment_type_len = 2;

hr = WcaInitialize(hInstall, "SetInstallScope");
ExitOnFailure(hr, "Failed to initialize");

er = MsiGetProperty(hInstall, TEXT("UpgradeCode"), upgrade_code,
&upgrade_code_len);
ExitOnWin32Error(er, hr, "Failed to get UpgradeCode property");

for (iProductIndex = 0;; iProductIndex++) {
er = MsiEnumRelatedProducts(upgrade_code, 0, iProductIndex, product_code);
if (er == ERROR_NO_MORE_ITEMS) break;
ExitOnWin32Error(er, hr, "Failed to get related product code");

er = MsiGetProductInfo(product_code, INSTALLPROPERTY_ASSIGNMENTTYPE,
assignment_type, &assignment_type_len);
ExitOnWin32Error(er, hr, "Failed to get the assignment type property "
"from related product");

// '0' = per-user; '1' = per-machine
if (assignment_type[0] == '0') {
/* When old versions which were installed as per-user are detected, the
* installation scope has to be set to per-user to be able to do an
* upgrade. If not, two versions will be installed side-by-side: one as
* per-user and the other as per-machine.
*
* If we wanted to disable backward compatibility, the installer should
* abort here, and request the previous version to be manually
* uninstalled before installing this one.
*/
er = MsiSetProperty(hInstall, TEXT("ALLUSERS"), TEXT(""));
ExitOnWin32Error(er, hr, "Failed to set the install scope to per-user");
break;
}
}

LExit:
return WcaFinalize(ERROR_SUCCESS);
}


UINT WINAPI BroadcastEnvironmentUpdate(MSIHANDLE hInstall) {
HRESULT hr = S_OK;
Expand Down
3 changes: 2 additions & 1 deletion tools/msvs/msi/custom_actions.def
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
LIBRARY "custom_actions"

EXPORTS
BroadcastEnvironmentUpdate
SetInstallScope
BroadcastEnvironmentUpdate
17 changes: 14 additions & 3 deletions tools/msvs/msi/product.wxs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
Manufacturer="$(var.ProductAuthor)"
UpgradeCode="1d60944c-b9ce-4a71-a7c0-0384eb884baa">

<Package InstallerVersion="200" Compressed="yes"/>
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine"/>

<Media Id="1" Cabinet="media1.cab" EmbedCab="yes"/>

Expand Down Expand Up @@ -249,16 +249,27 @@
</Component>
</DirectoryRef>

<Binary Id='BroadcastEnvironmentUpdate'
<Binary Id='CustomActionsDLL'
SourceFile='$(var.custom_actions.TargetDir)$(var.custom_actions.TargetName).dll' />

<CustomAction Id="SetInstallScope"
BinaryKey="CustomActionsDLL"
DllEntry="SetInstallScope"
Execute="immediate"
Return="check" />

<CustomAction Id="BroadcastEnvironmentUpdate"
BinaryKey="BroadcastEnvironmentUpdate"
BinaryKey="CustomActionsDLL"
DllEntry="BroadcastEnvironmentUpdate"
Execute="immediate"
Return="check" />

<InstallUISequence>
<Custom Action='SetInstallScope' Before='FindRelatedProducts'/>
</InstallUISequence>

<InstallExecuteSequence>
<Custom Action='SetInstallScope' Before='FindRelatedProducts'/>
<Custom Action='BroadcastEnvironmentUpdate' After='InstallFinalize'/>
</InstallExecuteSequence>

Expand Down

0 comments on commit 8e80528

Please sign in to comment.