未解析的外部符号 CLRCreateInstance

Unresolved external symbol CLRCreateInstance

我在 Internet 的帮助下编写了一些本机 C++ 代码,这些代码加载 .NET 运行时并调用具有签名的方法:public static int MethodNameHere(String pwzArgument) 来自托管程序集中的 class。

但是,我似乎无法将使用 Visual Studio 的代码编译成本机 DLL(64 位),因为 CLRCreateInstance() 似乎存在链接问题,即使我我在 .cpp 源文件中包含 "MetaHost.h"

完整代码如下:

#include "MetaHost.h"

extern "C" {
    __declspec(dllexport) DWORD __stdcall CallManagedMethod(LPCWSTR managedDLLPath, LPCWSTR classPathInAssembly, LPCWSTR methodName, LPCWSTR parameter) {
        // Bind to the CLR runtime..
        ICLRMetaHost* pMetaHost = nullptr;
        CLRCreateInstance(CLSID_CLRMetaHost, IID_ICLRMetaHost, (LPVOID*) &pMetaHost);

        ICLRRuntimeInfo* pRuntimeInfo = nullptr;
        pMetaHost->GetRuntime(L"v4.0.30319", IID_ICLRRuntimeInfo, (LPVOID*) &pRuntimeInfo);

        ICLRRuntimeHost* pClrRuntimeHost = nullptr;
        pRuntimeInfo->GetInterface(CLSID_CLRRuntimeHost, IID_ICLRRuntimeHost, (LPVOID*) &pClrRuntimeHost);

        pClrRuntimeHost->Start();

        // Okay, the CLR is up and running in this (previously native) process.
        // Now call a method on our managed C# class library.
        DWORD dwRet = 0;
        pClrRuntimeHost->ExecuteInDefaultAppDomain(managedDLLPath, classPathInAssembly, methodName, parameter, &dwRet);

        // Don't forget to clean up.
        pClrRuntimeHost->Release();

        pRuntimeInfo->Release();
        pMetaHost->Release();
        return dwRet;
    }
}

有什么帮助吗?

.h 文件不能解决 linking 问题,它只是添加问题。您 link 阅读的 MSDN 文章摸索了通常记录包含和 link 提示的方式,"Included as a resource" 毫无帮助。在 C++ 中,您必须 link mscoree.lib 才能解析符号。它是 mscoree.dll 的导入库,包含在 SDK 中。

最简单的方法是在#include 之后添加 #pragma comment(lib, "mscoree.lib")