[我知道简单地发布代码并寻求解决方案不是一个好主意,但是我不知道是什么原因造成的。
我正在尝试基于this code查找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
只是告诉compiler函数看什么LiKE,以便您的代码可以对其进行调用,但是您还需要告诉linker函数实际位于何处。 Msi.lib
告诉链接器函数是从Msi.dll
导出的,因此链接器可以将函数调用链接到该DLL。