0

I am learning COM. Visual Studio 2013. DLL x64 project (COM component) on C++.

I get the errors when I try to compile my "Hello World" project:

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

But I don't understand the reason of these errors... I am looking at my DEF and CPP files and I see no errors. :(((

Content of my_com_component_01.def file:

LIBRARY my_com_component_01.dll
EXPORTS
  DllMain PRIVATE
  DllGetClassObject PRIVATE
  DllRegisterServer PRIVATE
  DllUnregisterServer PRIVATE
  DllCanUnloadNow PRIVATE

Content of my dll.cpp file (definitions of the export functions):

/* 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;
  }
}

What I did incorrectly?

Thank you.

P.S.to πάντα ῥεῖ You are not right. It is bad when give the answer without investigating into a question essence. I found the reason of bad behavior of IDE and corrected it. I think that it is a bug of Visual Studio 2013.

Andrey Bushman
  • 11,712
  • 17
  • 87
  • 182
  • No, this is not a duplicate! I found the reason of problem. This is the wrong behaviour of Visual Studio 2013. I edited PROJ-file manually and now it works fine. But I can't publish an answer, because someone, without investigating into an essence of my question, already gave an assessment allegedly what my question is the counterpart and closed my topic. – Andrey Bushman May 31 '15 at 19:38
  • Can you provide the answer in a comment, so people can vote to reopen if they find it different from the other answer? – acelent Jun 01 '15 at 15:43
  • Yes, I can. I published it here: http://stackoverflow.com/questions/30572678/file-extension-changing-h-to-cpp-is-it-a-bug-of-visual-studio – Andrey Bushman Jun 01 '15 at 17:47

0 Answers0