Skip to content

Commit d178e49

Browse files
committed
direct p/invoke
1 parent 21711de commit d178e49

File tree

5 files changed

+22
-36
lines changed

5 files changed

+22
-36
lines changed

Directory.Build.props

+1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
</PropertyGroup>
2121

2222
<PropertyGroup>
23+
<SdkSrcRoot>$([MSBuild]::NormalizeDirectory('$(MSBuildThisFileDirectory)', 'src'))</SdkSrcRoot>
2324
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2425
<LangVersion>Latest</LangVersion>
2526
<TreatWarningsAsErrors>True</TreatWarningsAsErrors>

NuGet.config

-3
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@
2828
<add key="vs-impl" value="https://pkgs.dev.azure.com/azure-public/vside/_packaging/vs-impl/nuget/v3/index.json" />
2929
<!-- Used for Rich Navigation indexing task -->
3030
<add key="richnav" value="https://pkgs.dev.azure.com/azure-public/vside/_packaging/vs-buildservices/nuget/v3/index.json" />
31-
32-
<!-- remove once https://www.nuget.org/packages/WixToolset.Dtf.WindowsInstaller is uploaded to dotnet feed -->
33-
<add key="local" value="c:\temp\packages" />
3431
</packageSources>
3532
<disabledPackageSources>
3633
<clear />

eng/dependabot/Packages.props

-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
<!-- Packages in this file have versions updated periodically by Dependabot. Versions managed by Darc/Maestro should be in ..\Packages.props. -->
44

55
<ItemGroup>
6-
<PackageVersion Include="WixToolset.Dtf.WindowsInstaller" Version="4.0.6" />
7-
86
<!--Test dependencies-->
97
<PackageVersion Include="Verify.Xunit" Version="25.0.2" />
108
<PackageVersion Include="Verify.DiffPlex" Version="3.0.0" />

src/Installer/finalizer/Program.cs

+18-29
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
using System;
55
using System.IO;
6+
using System.Runtime.InteropServices;
67
using Microsoft.Win32;
7-
using WixToolset.Dtf.WindowsInstaller;
8+
using Microsoft.Win32.Msi;
89

910
if (args.Length < 3)
1011
{
@@ -99,51 +100,34 @@ static bool DetectSdk(string featureBandVersion, string platform)
99100

100101
static bool RemoveDependent(string dependent)
101102
{
102-
Installer.SetInternalUI(InstallUIOptions.Silent);
103+
// Disable MSI UI
104+
_ = MsiSetInternalUI((uint)InstallUILevel.NoChange, IntPtr.Zero);
103105

104106
// Open the installer dependencies registry key
105-
// This has to be an exhaustive search as we're not looking for a specific provider key, but for a specific dependent
106-
// that could be registered against any provider key.
107107
using var hkInstallerDependenciesKey = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Classes\Installer\Dependencies", writable: true);
108108
if (hkInstallerDependenciesKey == null)
109109
{
110110
Console.WriteLine("Installer dependencies key does not exist.");
111-
return false; // No dependencies to remove
111+
return false;
112112
}
113113

114-
// Iterate over each provider key in the dependencies
115114
foreach (string providerKeyName in hkInstallerDependenciesKey.GetSubKeyNames())
116115
{
117116
Console.WriteLine($"Processing provider key: {providerKeyName}");
118117

119118
using var hkProviderKey = hkInstallerDependenciesKey.OpenSubKey(providerKeyName, writable: true);
120119
if (hkProviderKey == null) continue;
121120

122-
// Open the Dependents subkey
123121
using var hkDependentsKey = hkProviderKey.OpenSubKey("Dependents", writable: true);
124122
if (hkDependentsKey == null) continue;
125123

126-
// Check if the dependent exists and continue if it does not
127-
string[] dependentsKeys = hkDependentsKey.GetSubKeyNames();
128-
bool dependentExists = false;
124+
bool dependentExists = hkDependentsKey.GetSubKeyNames()
125+
.Any(dependentsKeyName => string.Equals(dependentsKeyName, dependent, StringComparison.OrdinalIgnoreCase));
129126

130-
foreach (string dependentsKeyName in dependentsKeys)
131-
{
132-
if (string.Equals(dependentsKeyName, dependent, StringComparison.OrdinalIgnoreCase))
133-
{
134-
dependentExists = true;
135-
break;
136-
}
137-
}
138-
139-
if (!dependentExists)
140-
{
141-
continue; // Skip to the next provider key if the dependent does not exist
142-
}
127+
if (!dependentExists) continue;
143128

144129
Console.WriteLine($"Dependent match found: {dependent}");
145130

146-
// Attempt to remove the dependent key
147131
try
148132
{
149133
hkDependentsKey.DeleteSubKey(dependent);
@@ -155,16 +139,14 @@ static bool RemoveDependent(string dependent)
155139
return false;
156140
}
157141

158-
// Check if any dependents are left
159142
if (hkDependentsKey.SubKeyCount == 0)
160143
{
161-
// No remaining dependents, handle product uninstallation
162144
try
163145
{
164146
string productCode = hkProviderKey.GetValue("ProductId").ToString();
165147

166-
// Configure the product to be absent
167-
Installer.ConfigureProduct(productCode, 0, InstallState.Absent, "");
148+
// Configure the product to be absent (uninstall the product)
149+
uint error = MsiConfigureProductEx(productCode, (int)InstallUILevel.Default, InstallState.ABSENT, "");
168150
Console.WriteLine("Product configured to absent successfully.");
169151
}
170152
catch (Exception ex)
@@ -175,7 +157,6 @@ static bool RemoveDependent(string dependent)
175157
}
176158
return true;
177159
}
178-
179160
return false;
180161
}
181162

@@ -244,3 +225,11 @@ static bool IsRebootPending()
244225
Console.WriteLine("No reboot pending.");
245226
return false;
246227
}
228+
229+
[DllImport("msi.dll", CharSet = CharSet.Unicode)]
230+
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
231+
static extern uint MsiConfigureProductEx(string szProduct, int iInstallLevel, InstallState eInstallState, string szCommandLine);
232+
233+
[DllImport("msi.dll", CharSet = CharSet.Unicode)]
234+
[DefaultDllImportSearchPaths(DllImportSearchPath.System32)]
235+
static extern uint MsiSetInternalUI(uint dwUILevel, IntPtr phWnd);

src/Installer/finalizer/finalizer.csproj

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22

33
<PropertyGroup>
44
<TargetFramework>$(SdkTargetFramework)-windows</TargetFramework>
5-
<OutputType>Exe</OutputType>
5+
<OutputType>WinExe</OutputType>
66
<PublishAot>true</PublishAot>
77
<UseCurrentRuntimeIdentifier>true</UseCurrentRuntimeIdentifier>
88
<_IsPublishing>true</_IsPublishing>
99
<PublishDir>$(ArtifactsDir)bin\finalizer\$(Configuration)\$(SdkTargetFramework)-win-$(Architecture)\publish</PublishDir>
1010
</PropertyGroup>
1111

1212
<ItemGroup>
13-
<PackageReference Include="WixToolset.Dtf.WindowsInstaller" />
13+
<Compile Include="$(SdkSrcRoot)Microsoft.Win32.Msi\InstallUILevel.cs" />
14+
<Compile Include="$(SdkSrcRoot)Microsoft.Win32.Msi\InstallState.cs" />
1415
</ItemGroup>
1516

1617
<Target Name="PublishOnBuild" AfterTargets="Build" DependsOnTargets="Publish" />

0 commit comments

Comments
 (0)