gpt4 book ai didi

c++ - 未解析的外部符号 CLRCreateInstance

转载 作者:搜寻专家 更新时间:2023-10-31 01:34:47 25 4
gpt4 key购买 nike

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

但是,我似乎无法使用 Visual Studio 将代码编译为 native 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 文件不能解决链接问题,它只是添加它们。您链接到的 MSDN 文章以通常的方式记录了包含和链接提示,“作为资源包含”非常无益。在 C++ 中,您必须链接 mscoree.lib 才能解析符号。它是 mscoree.dll 的导入库,包含在 SDK 中。

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

关于c++ - 未解析的外部符号 CLRCreateInstance,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38659097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com