Search code examples
c++dllcomdllimportdllexport

Using a non-COM object from a COM dll


A Visual Studio C++ solution is made of 2 projects both in C++:

  • Client produces a client.exe
  • Algo produces a algo.dll

Client and Algo communicate together via a COM Layer, hence Algo is a COM dll.

I want to add a function in Algo.dll and call it directly from Client, avoiding the COM Layer, but did not succeed to solve the "unresolved symbol" link errors when building the Client.exe


What was tried ?

In the Algo project, I've added the LIBRARY_EXPORT preprocessor definition and the following files: TestFile.h

#ifdef LIBRARY_EXPORTS
#    define LIBRARY_API __declspec(dllexport)
#else
#    define LIBRARY_API __declspec(dllimport)
#endif

extern "C"  LIBRARY_API bool __stdcall TestFunction();

TestFile.cpp

extern "C"  LIBRARY_API bool __stdcall TestFunction()
{
    return true;
}

In the client project, there is a call to TestFunction();

I get an link error when building the client: unresolved external symbol "__declspec(dllimport) bool __cdecl TestFunction(void)

Apparently, I do not get correctly how to export/import my function.


Solution

  • You need to add algo.lib to the linker input files of client.exe