Using a non-COM object from a COM dll - c++

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.

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

Related

VS2013 C++ Compiler Mangling name defined with extern "C"

I'm trying to build a WIN32 console app that uses the current 2.12.28 ftd2xx.lib static library from FTDI. I'm using VS2013 and native unmanaged C++. My call looks like this.
#include "../ftd2xx.h"
. . .
DWORD port_count = 0;
FT_STATUS status = FT_OK;
status = FT_CreateDeviceInfoList(&port_count);
When I compile I get a link error
GetTopazVCP.obj : error LNK2019: unresolved external symbol __imp__FT_CreateDeviceInfoList#4 referenced in function "unsigned long __cdecl Get1stVirtualComPort(unsigned long *)" (?Get1stVirtualComPort##YAKPAK#Z)
The unresolved symbol __imp__FT_CreateDeviceInfoList#4 appears to be a mangled name version of the FT_CreateDeviceInfoList function. So it's not being resolved in the ftd2xx.lib which uses C naming. What I don't understand is why the compiler mangled the name when the ftd2xx.h file has a conditional extern "C"
#ifdef __cplusplus
extern "C" {
#endif
. . .
FTD2XX_API
FT_STATUS WINAPI FT_CreateDeviceInfoList(
LPDWORD lpdwNumDevs
);
...
#ifdef __cplusplus
}
#endif
wrapping all the FT_??? declarations. I have confirmed that __cplusplus is defined during the compile. Any ideas what is causing the unexpected name mangling?
As ChronoKitsune pointed out in his comment there is no name mangling here. The problem was I was linking the static version of the FTDI library but the default for the ftd2xx.h header file is to declare the FT_??? functions as calls to the DLL version. When I replaced the ftd2xx.lib static version with the ftd2xx.lib DLL version it built successfully. FTDI only supplies one version of the ftd2xx.h header and looking closely inside it I discovered that if you want to use the static ftd2xx.lib you need to #define FTD2XX_STATIC before including ftd2xx.h
#define FTD2XX_STATIC
#include "ftd2xx.h"

build dlls using Visual Studio

I want to build dlls using Visual Studio which I want to use as functions in Matlab.
While doing so I have used declspec(dllimport) function...but I get compilation error LNK2019: unresolved external sumbol?
Appreciate your help.
#ifdef PRC50CONTROL_EXPORTS
#define PRC50CONTROL_API __declspec(dllexport)
#else
#define PRC50CONTROL_API __declspec(dllimport)
#endif
int PRC50CONTROL_API SetPRC50Gain(double inGain);
int PRC50CONTROL_API SetPRC50Gain(double inGain);
You have the return type before the __declspec()
Move it to
PRC50CONTROL_API int SetPRC50Gain(double inGain);
Also, obviously, make sure you have
PRC50CONTROL_EXPORTS
defined in your header of the library you're exporting the symbols from.

C++ DLL compiled with /clr used in winforms compiled with /clr:pure

I created a DLL to use in my project, however, I get the following error:
error C3389: __declspec(dllexport) cannot be used with /clr:pure or /clr:safe
which I sort of expected, but I figured I could use platform invoke, however, I found NO explanations of how to do this with C++. Everything I found was C#.
Can anyone either explain or give me a link?
I have in the dll.h file:
static __declspec(dllexport) double name(function);
then the dll.cpp:
#include "dll.h"
namespace name
{
double name::name(function)
{
// code
}
}
However I'm unsure what to change if I were to use __clrcall.

making an VC++ .exe to DLL .is it possible?

I have an VC++ win 32 application which compiles into an EXE. But now I want to convert it into dll so that I can load that in another application.I tried changing in Visual Studio properties from .EXE to .DLL which successfully converted it but whn i use GetProcAddress it always returns NULL . I am not sure what I am doing is right or wrong .
Basically this what I want to achieve :
I want to link project 1 and project2
Project 2 should be able to invoke the functions of project1(which is an exe currenlty)
EDIT
Hi guys thanks for your input .I told what you guys said . even then my GetProcAddress returns zero . Am i am doing anything wrong .Shown my dll loading code below .
HINSTANCE LoadMe = LoadLibrary( _T("D:\\VC++Project\\CVAList\\CVAList\\ExportTest.dll"));
if (LoadMe != 0)
printf("LoadMe library loaded!\n");
else
printf("LoadMe library failed to load!\n");
EntryPointfuncPtr LibMainEntryPoint;
LibMainEntryPoint = (EntryPointfuncPtr)GetProcAddress(LoadMe,"PrintFloatsVal");
LibMainEntryPoint (a1 ,a,b,c,d ); // 4 double
EDIT DLL Export Code
#define DllExport __declspec( dllexport )
DllExport void PrintFloatsVal ( int amount, double &d1 ,double &d2 , double &d3 ,double &d4)
{
....
..
}
You need to export the functions you wish to access using the __declspec dllexport keyword.
So if you add the manifest constant 'BUILDING_MY_DLL' to the project, the header file that declares the functions you care about can be used in both the DLL project and any code that uses the DLL:
#ifdef BUILDING_MY_DLL
#define MY_DLL_EXPORT __declspec dllexport
#else
#define MY_DLL_EXPORT __declspec dllimport
#endif
And decorate the functions you wish to export:
MY_DLL_EXPORT BOOL Func1(int a);
If the function you wish to access is implemented in C++ it will be decorated, for the purposes of function overloading and other purposes, and it best accessed directly like any other function. If you wish to use GetProcAddress() however you are better off giving it C-linkage by surrounding the function with extern "C" { ... }. This will make the exported name the same the name used within the code.
Reference: http://msdn.microsoft.com/en-us/library/a90k134d(v=vs.80).aspx

Linker problem on VS2005 with VC++

Here's the scenario:
Platform:
VS2005 and language is VC++
Situation:
There's just 1 assembly CMPW32. It has 2 projects:
1 is a DLL project called CMPW32 and the 2nd one is an .exe project called Driver
They both share the same Debug folder under the main assembly folder.
I have been able to successfully export a few functions from the DLL. The Driver project accesses 1 of these exported functions. (First of all I am not if functions need to be exported for projects in the SAME assembly to be able to use them. I can just include the header files and use the functions I think.)
Following is are a few lines of code from some files which you might find useful to analyze my problem:
//main.cpp file from the Driver project which is meant to generate Driver.exe
#pragma comment(lib, "winmm.lib")
#include <CM.h>
#include "conio.h"
#include "CMM.h"
#include "CMF.h"
#define C_M_F _T("c:\\CannedMessages.en-US")
int_tmain (int argc, TCHAR* argv [])
{
CMM myobjModel;
CMF::Read (CANNED_MESSAGES_FILE, myobjModel);
getch();
}
//CMM.h file
#ifndef C_M_M
#define C_M_M
#include "CMD.h"
#include "CMC.h"
#include "CM.h"
#define _C_M_DLL
#include "CMP.h"
class CM_DLL_API CMM
{ //some code here...
}
//CMF.h
#ifndef C_M_F
#define C_M_F
#include "CMM.h"
#define _C_M_DLL
#include "CMP.h"
class CM_DLL_API CMF
{ //some code here...
}
//CMP.h
#ifndef C_M_P
#define C_M_P
#include "CMD.h"
#define C_M_B_F _T("CannedMessages.")
#ifdef _C_M_DLL
#define CM_DLL_API __declspec( dllexport )
#else
#define CM_DLL_API __declspec( dllimport )
#endif
extern "C"
{
//list of functions to be exported..
}
ERRORS on building the solution:
Error13 error LNK2019: unresolved external symbol "public: __thiscall CMM::~CMM(void)" (??1CMM##QAE#XZ) referenced in function _wmain main.obj
Error15 fatal error LNK1120: 2 unresolved externals C:\"somepath here which I cant disclose"\Projects\CMPW32\Debug\Driver.exe
Please Note: If I choose to build only the CMPW32 DLL project, there are no errors and the CMPW32.dll file gets generated in the debug folder with the correct functions getting getting exported.
However there seems to be some linking problem that is pretty evident and I don't know what's going on. I have included every required file and also have entered the required .lib in the input of the "Project Settings". The paths have been set correctly too.
It would be really helpful if someone could help me out with this. Please lemme know if additional information required.
Thanks,
Viren
Looks like your Driver.exe project does not include the CPP source files of the CMM class, likely CMM.cpp.
or
You have declare a destructor for CMM class in your .H file (CMM.H) and forgot to implement it in the .CPP file (CMM.CPP).