How does Windows find the installed location when uninstalling software

谁都会走 提交于 2020-07-30 04:06:29

问题


When I run an installer that allows a custom install location/path, the files will be correctly placed at the location that I select.

When I run the same MSI and select remove (or uninstall from add/remove programs), how does it know the install location so the correct files are removed?

I thought it would be stored at 'Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall{GUID}', but when I look at that location for my installed software, the 'InstallLocation' key is empty.

However, no matter how I uninstall it, it knows which folder to go remove, no matter where I put it. Is that information stored elsewhere in the registry, or in the MSI file itself?


回答1:


This is a very complicated question as MSI can be configured to drop its uninstall files anywhere you tell it. Usually by default though it will create an uninstall .msi file with a specific name in C:\Windows\Installer.

But don't depend on the uninstall .msi being placed in this directory and don't rely on there being an uninstall path in the Uninstall registry key. This key is as much about convenience for the end-user as anything else.

The uninstall information is usually contained within the MSI file, but it need not be and during installation it can create keys to aid upgrading and uninstallation. The information that an installation will leave in the registry is entirely down to how you configure the .msi database.

Adding a few more things... many installers like Nullsoft, InstallAware and InstallShield like to do their own stuff and put their uninstall information in other places. So InstallShield likes to create an InstallShield Installation Information folder and Nullsoft likes to create .dat files and an uninstall.exe. But beyond all this, these installers are still invoking MSI and creating installation tables and database. So where the uninstall information is actually located it not an exact science!




回答2:


UPDATE:

Find Component's Installation Location: Is there way to detect install location without uninstall registry nor C:\Windows\Installer?


Implementation Details: How MSI stores these things are implementation details that should not be meddled with, attempted modified or used directly for any purpose - just so that is clear. You should go through the MSI API which is implemented as Win32 functions with complementary COM wrappers for access via scripting languages.

Registry: The MSI database is stored mostly in the registry, but there also components on disk - some of which you refer to - for example %SystemDrive%\Windows\Installer (a super-hidden folder that should not be modified in any way). The MSI database is stored in numerous locations throughout the registry:

  • HKCR\Installer
  • HKCU\Software\Microsoft\Installer
  • HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Installer
  • HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall
  • HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Uninstall
  • HKLM\SOFTWARE\WOW6432Node\Microsoft\Windows\CurrentVersion\Installer
  • Etc...

Some of these are real, some are aliases, some are merges. It is all a bit fuzzy. Again: implementation details - a well-known euphemism for all of us to: "give up right now, will you"? :-). Just apply the MSI API to acquire the information you need.


MSI API: A lot of stuff to read above to get to the point, go via the MSI API to get your information on directory resolution. What we have to do is a bit exotic, we have to spin up a session object for the installed product and run two standard actions (built-in MSI actions from Microsoft) in order to resolve the directory table and installation directories of the MSI in question (about "costing"). Below is a practical sample:

For the record:

  • How can I find the product GUID of an installed MSI setup?

  • How can I compare the content of two (or more) MSI files? (on Orca and other, free MSI tools)

  • Uninstalling an MSI file from the command line without using msiexec (a myriad of ways to uninstall MSI packages)

Set installer = CreateObject("WindowsInstaller.Installer")

' Other test product codes: {2F73A7B2-E50E-39A6-9ABC-EF89E4C62E36}

productcode = Trim(InputBox("Please paste or type in the product code you want to look up details for:", _
              "Find Product Details (test GUID provided):", "{766AD270-A684-43D6-AF9A-74165C9B5796}"))
If search = vbCancel Or Trim(productcode) = "" Then
   WScript.Quit(0)
End If

Set session = installer.OpenProduct(productcode)

' Crucially, resolve the directory table and properties by running "MSI Costing"
session.DoAction("CostInitialize")
session.DoAction("CostFinalize")

' Can be any directory property from the Directory table in the MSI:
MsgBox session.Property("INSTALLFOLDER")

' Open the MSI in Orca to find the directory folder property names

Throwing in a link to an old answer on how to list tables inside an MSI file.


Resolve All: Got a bit carried away and made one more update to resolve ALL directories for any installed package. Here is a script (not tested much):

' https://stackoverflow.com/questions/17543132/how-can-i-resolve-msi-paths-in-vbscript
' On Error resume Next

Set installer = CreateObject("WindowsInstaller.Installer")

' Other test product codes: {2F73A7B2-E50E-39A6-9ABC-EF89E4C62E36}

const READONLY = 0
Dim DirList

productcode = Trim(InputBox("Please paste or type in the product code you want to look up details for:", _
              "Find Product Details (test GUID provided):", "{766AD270-A684-43D6-AF9A-74165C9B5796}"))
If search = vbCancel Or Trim(productcode) = "" Then
   WScript.Quit(0)
End If

Set session = installer.OpenProduct(productcode)
session.DoAction("CostInitialize")
session.DoAction("CostFinalize")

set view = session.Database.OpenView("SELECT * FROM Directory")
view.Execute
set record = view.Fetch

Do until record is Nothing
    
    ResolvedDir = session.Property(record.StringData(1))
    DirList = DirList + record.StringData(1) + " => " + ResolvedDir + vbCrLf
    set record = view.Fetch

Loop

' Dismiss dialog with ESC key if it falls off screen 
WScript.Echo DirList ' Use WScript.Echo due to MsgBox restrictions (number of characters)

Links:

  • How can I resolve MSI paths in VBScript?


来源:https://stackoverflow.com/questions/60172976/how-does-windows-find-the-installed-location-when-uninstalling-software

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!