-2

I know that simply posting code and asking for a solution isn't a good idea, but I have no idea what's causing this.

I'm trying to find the installation path of PowerPoint based on this code, however, the compiler gives this error:

error LNK2019: unresolved external symbol _MsiLocateComponentW@12 referenced in function _WinMain@16

I'm using Visual Studio 2019 and IntelliSense doesn't notice the error, only the compiler. Here's the code:

#include <Windows.h>
#include <msi.h>

int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nShowCmd) {
    LPCWSTR PowerPoint = L"{CC29E94B-7BC2-11D1-A921-00A0C91E2AA2}";

    DWORD size = 300;
    INSTALLSTATE installstate;
    LPWSTR sPath;

    sPath = new wchar_t[size];
    installstate = MsiLocateComponent(PowerPoint, sPath, &size);

    if (installstate == INSTALLSTATE_LOCAL || installstate == INSTALLSTATE_SOURCE)
        MessageBox(NULL, sPath, L"PowerPoint path", MB_OK | MB_ICONASTERISK );
    delete[] sPath;
    return 0;
}

As you can see, I included the msi.h header. What's wrong with my code?

D. Pardal
  • 6,173
  • 1
  • 17
  • 37

1 Answers1

2

You are getting a linker error, not a compiler error.

Make sure your project is linking to msi.lib. Using msi.h is not enough by itself.

msi.h tells the compiler what the function LOOKS LIKE so that your code can make calls to it.

But you also need to tell the linker WHERE the function is actually located. msi.lib tells the linker that the function is exported from msi.dll, so then the linker can link your function call to that DLL.

To link to msi.lib, you can either specify msi.lib as an "Additional Dependency" for Linker Input in your project options, or you can use a #pragma comment(lib, "msi.lib") statement directly in your code.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thank you. It works, but now it's returning INSTALLSTATE_UNKNOWN :(... – D. Pardal Feb 19 '20 at 19:52
  • @D.Pardal Per the [documentation](https://learn.microsoft.com/en-us/windows/win32/api/msi/nf-msi-msilocatecomponentw): "*INSTALLSTATE_UNKNOWN **The feature is not published**. The application should have determined this earlier by calling MsiQueryFeatureState or MsiEnumFeatures. The application makes these calls while it initializes. An application should only use features that are known to be published. Since INSTALLSTATE_UNKNOWN should have been returned by MsiUseFeature as well, either MsiUseFeature was not called, or its return value was not properly checked.*" – Remy Lebeau Feb 19 '20 at 19:55
  • @D.Pardal IOW, it is telling you that PowerPoint is not installed/published on your system. If you feel that is not true, then you need to post a new question about THAT issue. It has nothing to do with the linker error in THIS question. – Remy Lebeau Feb 19 '20 at 19:57
  • I know, but thanks anyway. I had already seen that in the docs and I wasn't expecting you to answer it. – D. Pardal Feb 19 '20 at 20:19
  • Sigh, the things you have to do to get a helpful vote these days. – Hans Passant Feb 19 '20 at 21:52