我正在学习COM。 Visual Studio 2013. C ++上的DLL x64项目(COM组件)。
当我尝试编译“Hello World”项目时,我收到错误:
Error 1 error LNK2001: unresolved external symbol DllCanUnloadNow C:\COMInteropSample\my_com_sandbox\my_com_component_01\my_com_component_01.def
Error 2 error LNK2001: unresolved external symbol DllGetClassObject C:\COMInteropSample\my_com_sandbox\my_com_component_01\my_com_component_01.def
Error 3 error LNK2001: unresolved external symbol DllRegisterServer C:\COMInteropSample\my_com_sandbox\my_com_component_01\my_com_component_01.def
Error 4 error LNK2001: unresolved external symbol DllUnregisterServer C:\COMInteropSample\my_com_sandbox\my_com_component_01\my_com_component_01.def
Error 5 error LNK1120: 4 unresolved externals C:\COMInteropSample\my_com_sandbox\Debug\my_com_component_01.lib my_com_component_01
但我不明白这些错误的原因......我正在查看我的DEF和CPP文件,我看到没有错误。 :(((
my_com_component_01.def
档案的内容:
LIBRARY my_com_component_01.dll
EXPORTS
DllMain PRIVATE
DllGetClassObject PRIVATE
DllRegisterServer PRIVATE
DllUnregisterServer PRIVATE
DllCanUnloadNow PRIVATE
我的dll.cpp
文件的内容(导出功能的定义):
/* dll.cpp */
#include <objbase.h>
#include <sal.h>
#include "tools.h"
#include "global_vars.h"
#include "CA.h"
#include "My_com_factory.h"
static HINSTANCE pDll = nullptr;
extern "C" {
BOOL WINAPI DllMain(_In_ HINSTANCE hinstDLL, _In_ DWORD fdwReason,
_In_ LPVOID lpvReserved){
char* s = nullptr;
if (DLL_PROCESS_ATTACH == fdwReason){
s = "DLL_PROCESS_ATTACH";
}
else if (DLL_PROCESS_DETACH == fdwReason){
s = "DLL_PROCESS_DETACH";
}
else if (DLL_THREAD_ATTACH == fdwReason){
s = "DLL_THREAD_ATTACH";
}
else if (DLL_THREAD_DETACH == fdwReason){
s = "DLL_THREAD_DETACH";
}
pDll = hinstDLL;
std::cout << "DllMain: ";
trace(s);
return true;
}
HRESULT __stdcall DllGetClassObject(_In_ REFCLSID rclsid,
_In_ REFIID riid, _Out_ LPVOID *ppv){
if (_uuidof(CA) == rclsid){
My_com_factory* mf = new My_com_factory();
HRESULT hr = mf->QueryInterface(riid, ppv);
return hr;
}
else{
*ppv = nullptr;
return CLASS_E_CLASSNOTAVAILABLE;
}
}
// TODO: Is not implemented still.
HRESULT __stdcall DllRegisterServer(){
return S_OK;
}
// TODO: Is not implemented still.
HRESULT __stdcall DllUnregisterServer(){
return S_OK;
}
HRESULT __stdcall DllCanUnloadNow(){
return (obj_count == 0 && locks_count == 0) ? S_OK : S_FALSE;
}
}
我做错了什么?
谢谢。
P.S.to πάνταῥεῖ 你看起来不太对劲。在没有调查问题本质的情况下给出答案是很糟糕的。我找到了IDE不良行为的原因并予以纠正。我认为这是Visual Studio 2013的一个错误。