Pregunta

I am trying to get the installation path of Office programs as this microsoft kb article suggests (since Office start menu shortcuts don't point to paths anymore; thank you Microsoft).

Of course, the KB example uses C++ and native libraries, which I tried to replicate in VB.NET with the following code

<Runtime.InteropServices.DllImport("msi.dll")> Public Shared Function MsiLocateComponent(szComponent As String, ByRef lpPathBuf As Char(), ByRef pcchBuf As Integer)
End Function

I call this function from the following, which I expect to return a message box with at least the dot (if the rest fails). Instead, I get nothing at all, so I assume that the code runs into some sort of error (which is silent though because I get no exception).

Shared Function DealWithWinInstallerPath(ProgramPath As String) As String
    Dim sPath(300) As Char
    Dim sSize As Integer = 300
    Dim state As Integer = MsiLocateComponent("{019C826E-445A-4649-A5B0-0BF08FCC4EEE}", sPath, sSize)
    MsgBox(state & ".")
End Function

(Note that the function has an argument that will be used in the future, but its contents are just for testing purposes).

Am I declaring the function incorrectly? Passing the wrong arguments? Is msi.dll not the right name for the library? The msdn database doesn't help much either.

¿Fue útil?

Solución

For managed code, the Microsoft.Deployment.WindowsInstaller interop assembly found in Windows Installer XML's (WiX) Deployment Tools Foundation (DTF) is the way to go. DTF's ComponentInstallation class has a read only property called Path that encapsulates the call to MsiLocateComponent()

Once installed, you can find the DLL in C:\Program Files (x86)\WiX Toolset v3.8\SDK.

You can also read through the source code for pointers on how to P/Invoke MSI API calls.

Otros consejos

MsiGetComponentPath is preferred if you read the MSDN docs, and there's an interop example here:

http://www.pinvoke.net/default.aspx/msi.MsiGetComponentPath

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top