我正在尝试在 C++/CLI .dll 项目中使用来自本机 .dll 项目的 C++ 类。视觉工作室 2010,.NET 框架 3.5。到目前为止,这就是我所拥有的:
// This is a common macro:
#ifdef ISDLL
#define DLL __declspec(dllexport)
#else
#define DLL __declspec(dllimport)
#endif
NativeProject:
//INativeType.h:
class DLL INativeType {
public:
virtual void Foo() = 0;
};
//NativeType.h:
class DLL NativeType : public INativeType {
public:
virtual void Foo();
};
extern "C" DLL INativeType CreateNativeType();
//NativeType.cpp:
void NativeType::Foo() {}
extern "C" DLL INativeType* CreateNativeType() {
return new NativeType();
}
C++/CLI Project:
// Wrapper.h:
#include "INativeType.h"
ref class Wrapper {
INativeType* m_nativeType;
};
// Wrapper.cpp:
#include "Wrapper.h"
#include "NativeType.h"
// in some method:
m_nativeType = CreateNativeType();
这给了我以下链接器错误:
error LNK2028: unresolved token (...) "extern "C" class INativeType * __stdcall CreateNativeType(void)"" ... in Wrapper.obj
此外,虽然函数在 NativeProject.dll 中正确导出,但正如我使用 DependencyWalker 验证的那样,链接器也给出了以下警告:
warning LNK4199: /DELAYLOAD:NativeProject.dll ignored; no imports found from NativeProject.dll
C++/CLI 项目配置为延迟加载 NativeProject.dll,并且还在其链接器选项中包含 NativeProject.lib。此时我不知道如何诊断此链接器错误。我究竟做错了什么?