未解析的外部符号_MsiLocateComponentW@12.

unresolved external symbol _MsiLocateComponentW@12

本文关键字:符号 MsiLocateComponentW@12 外部      更新时间:2023-10-16

我知道简单地发布代码并寻求解决方案不是一个好主意,但我不知道是什么导致了这种情况。

我正试图根据这段代码找到PowerPoint的安装路径,然而,编译器给出了以下错误:

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

我使用的是Visual Studio 2019,IntelliSense没有注意到错误,只有编译器。这是代码:

#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;
}

如您所见,我包含了msi.h标头。我的代码出了什么问题?

您得到的是链接器错误,而不是编译器的错误。

确保您的项目链接到msi.lib。仅使用msi.h本身是不够的。

msi.h告诉编译器函数的外观,以便您的代码可以对其进行调用

但是,您还需要告诉链接器函数的实际位置。msi.lib告诉链接器函数是从msi.dll导出的,这样链接器就可以将函数调用链接到该DLL。

要链接到msi.lib,可以在项目选项中将msi.lib指定为Linker Input的"附加依赖项",也可以直接在代码中使用#pragma comment(lib, "msi.lib")语句。