I work with an application that appears (just started a few weeks ago, so I am still learning the older apps) to be built in C and my company wants to use that program's ability to make a call to an outside DLL to extend some new functionality. To do this, I started working on my POC, which is the first two files below. The only specification we were given was that the dll has to export the following function:
extern int __stdcall TestMethod_LoadCustomer(const char * name, char * id);
I tried to implement that as follows:
TestDLL.h
#define TestDLL_API __declspec(dllexport)
namespace TestDLL
{
class TestDLL
{
public:
static TestDLL_API int TestMethod_LoadCustomer(const char* name, char* id);
};
}
TestDLL.cpp
// TestDLL.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "TestDLL.h"
#include <string.h>
extern "C" int __declspec(dllexport) __stdcall TestMethod_LoadCustomer(const char* name, char* id)
{
if (strlen(name) <= 8) {
strcpy(id, name); // name contains customer id
} else {
id[0] = 0; // Customer not found
}
return 0;
}
These two files compile fine. The problem comes in when I try to test this dll via a separate little console app shown here:
RunTEST.cpp
// RunTest.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include "TestDLL.h"
using namespace std;
int _tmain()
{
char* id= "";
TestDLL::TestDLL::TestMethod_LoadCustomer("77777", id);
cout << id;
cin >> id;
return 0;
}
All I am looking for is to be able to pass in a character string into the call TestMethod_LoadCustomer() and have it be added to the id field.
When I actually try to build this solution, I get the following error:
"error LNK2019: unresolved external symbol "public: static int __cdecl TestDLL::TestDLL::TestMethod_LoadCustomer(char const *, char *)" (?TestMethod_LoadCustomer#TestDLL#1#SAHPBDAD#Z) referenced in function _wmain"
I am assuming it has something to do with the way I am trying to reference it in my client app, but I am not sure. I have looked at other LNK2019 errors on StackOverflow, but none of those solutions seemed to work here, of I have incorrectly implemented them. Can any one assist in helping me get rid of this error message?
On the TestDLL.cpp file is missing two things:
1) The namespace TestDLL.
2) the TestDLL:: before the method name.
// TestDLL.cpp : Defines the exported functions for the DLL application.
//
#include "stdafx.h"
#include "TestDLL.h"
#include <string.h>
namespace TestDLL {
extern "C" int __declspec(dllexport) __stdcall TestDLL::TestMethod_LoadCustomer(const char* name, char* id)
{
if (strlen(name) <= 8) {
strcpy(id, name); // name contains customer id
} else {
id[0] = 0; // Customer not found
}
return 0;
}
}
While defining the function in TestDLL.cpp you didn't mention that the function is member of the class TestDLL.
Related
I am very new in C++ world and this kind of task.
I am having a hard time to resolve this issue.
Any help is kindly appreciated!
I have created a regular MFC DLL that is statically linked to MFC. (This is the first time i ever created a MFC project)
MFC DLL Header File Content
// Serial.h
#pragma once
#ifndef __AFXWIN_H__
#error "include 'pch.h' before including this file for PCH"
#endif
#include "resource.h" // main symbols
class SerialApp : public CWinApp
{
public:
SerialApp();
__declspec(dllexport) void __cdecl SerialApp::Init();
public:
virtual BOOL InitInstance();
DECLARE_MESSAGE_MAP()
};
MFC DLL Definition File Content
// Serial.cpp
#pragma once
#include "pch.h"
#include "framework.h"
#include "Serial.h"
#include <iostream>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
BEGIN_MESSAGE_MAP(SerialApp, CWinApp)
END_MESSAGE_MAP()
SerialApp::SerialApp()
{
}
SerialApp theApp;
BOOL SerialApp::InitInstance()
{
CWinApp::InitInstance();
return TRUE;
}
extern "C"
{
__declspec(dllexport) void __cdecl SerialApp::Init()
{
AfxDebugBreak();
std::cout << "Succeded";
}
}
Win32 Console Application Content
#include <iostream>
#include <Windows.h>
int main()
{
//Load the DLL
HMODULE lib = LoadLibrary(L"D:\\C++ PROJECTS\\serial\\Debug\\Serial.dll");
//Create the function
typedef void(*Init_t)(void);
Init_t Serial = (Init_t)GetProcAddress(lib, "Init");
std::cout << GetLastError(); // This output's 127
if (!Serial) {
//ERROR. Handle it.
}
__declspec(dllimport) void __cdecl Init(void);
//Init(); //LNK2019 unresolved external symbol "__declspec(dllimport) void __cdecl Init(void)"
std::getchar();
}
When i use this approach:
__declspec(dllimport) void __cdecl Init(void);
Init();
//I am getting the linker error LNK2019 unresolved external symbol "__declspec(dllimport) void __cdecl Init(void)"
When i use this approach:
typedef void(*Init_t)(void);
Init_t Serial = (Init_t)GetProcAddress(lib, "Init");
std::cout << GetLastError();
//I am getting error code 127 from the GetLastError() function. (The specified procedure could not be found)
When i use this approach:
#include "Serial.h"
int main()
{
SerialApp pSerial;
pSerial.Init();
std::getchar();
}
//Serial.h forces me to include 'pch.h' before itself. if i include 'pch.h' into Win32 Console App before Serial.h It end's up with about 1000's of errors.
For Win32 Console Application Project:
1. C/C++ -> General -> Additional Include Directories is set to: D:\C++ PROJECTS\serial\Serial
2. Linker -> Input -> Additional Dependencies is set to : Serial.lib
3. Both Serial.lib and Serial.dll are included in the project and in the Debug folder.
4. Both MFC Dll Project and Win32 Console Projects are compiled in Debug x86 mode.
Questions
What i am doing wrong?
Is this even possible?
Thank you!
The question is resolved by #dxiv's comment:
You cannot import a member function like SerialApp::Init to a non-MFC app,
because that app does not know about the SerialApp class, and cannot
know about it since SerialApp is derived from CWinApp which is an MFC
class, while your app is non-MFC. If you want to export a free
function from the DLL, then make it __declspec(dllexport) void __cdecl Init(void); outside the SerialApp class, or any class for that matter.
1. I removed the declaration __declspec(dllexport) void __cdecl SerialApp::Init(); from the header file.
2. Rewrite the definition of Init() in the Serial.cpp file as:
extern "C"
{
__declspec(dllexport) void __cdecl Init()
{
std::cout << "Succeded";
}
}
3. I call the function in Win32 Console App as:
int main()
{
//Load the DLL
HMODULE lib = LoadLibrary(L"D:\\C++ PROJECTS\\serial\\Debug\\Serial.dll");
//Create the function
typedef void(*Init_t)(void); // <- You can name Init_t whatever you want.
Init_t Serial = (Init_t)GetProcAddress(lib, "Init");
if (!Serial) {
std::cout << GetLastError();
return 1;
}
Serial(); //<- Init() Function Call from Serial.dll
std::getchar();
return 0;
}
And all worked great!
Thank you everyone, who make time to read and answer my Question!
I'm trying to move from Dev C++ to Visual Studio while studying C++ (since I'll have to work with the latter) but for some reason, a rather simple class implementation that perfectly works in Dev C++ creates a long list of errors in Visual Studio.
The files are simple:
header file, for the declaration of constructors, variables etc
cpp file, to implement said constructors, functions etc
consoleapplication file (on visual studio), to produce the "main()" function.
stock2.h
#ifndef STOCK2_H_
#define STOCK2_H_
class Stock
{
public:
Stock();
Stock(const char* co, int n = 0, double pr = 0.0);
~Stock();
void show()const;
private:
std::string company;
int shares;
double share_val;
double total_val;
};
#endif
stock2.cpp
#include "stdafx.h"
#include <iostream>
#include <string>
#include "stock2.h"
Stock::Stock() //default constructor
{
//code
}
Stock::Stock(const char* co, int n, double pr)
{
//code
}
Stock::~Stock()
{
std::cout << "Stock object has been destroyed" << std::endl;
}
//Methods
void Stock::show() const
{
//code
}
ConsoleApplication.cpp
#include "stdafx.h"
#include "stock2.cpp"
int main()
{
using std::cout;
const int STKS = 4;
Stock stocks[STKS] = {
Stock("NanoSmart", 12, 20.1),
Stock("Boffo Objects", 200, 2.0),
Stock(),
Stock("Monolithic Obelisks", 130, 3.25)
};
cout << "Stock Holdings: \n";
for (int st = 0; st<STKS; st++)
stocks[st].show();
return 0;
}
I've tried to find the solution on other questions posted here but I really can't figure out what's wrong here.
I also read that one is not supposed to #include a cpp file since the header should be the link between the main() and the cpp file itself, but if I decide to use #include stock2.H instead of .CPP in consoleapplication, then the compiler can't find the methods implementations anymore.
EDIT: In the rush i forgot to post the errors!
They're all in this form:
Error LNK2005
"public: void __thiscall Stock::update(double)" (?update#Stock##QAEXN#Z) already defined in
ConsoleApplication1.obj ConsoleApplication1 //path\ConsoleApplication1\ConsoleApplication1\stock2.obj
EDIT2: Since many of you are asking me about the "Solution Explorer", I better just add a screenshot to show you how it's made right now
You included stock2.cpp in your ConsoleApplication.cpp. This means all the code inside stock2.cpp is now compiled twice, and the linker shows the error message
Error LNK2005 "public: void __thiscall Stock::<...> already defined
for the now duplicated functions. Simply replace
#include "stock2.cpp"
with
#include "stock2.h"
If you get another error when doing so, please post the error message for this.
I am trying to export a function pointer for a function to be called. What I am after is when a function in a dll/exe needs to call a function exported by another library it gets the function pointer and calls that. The reason for this is I want to provide a hooking mechanism and I figured function pointers would be the quickest and easiest way because I can change what they point to easily are runtime.
So I found this Exporting a function pointer from dll and I cant get it to work. Whenever I call it to get the function pointer I get an error that it cant find the entry point. So the error isnt that the function pointer is working but the function to get the function pointer isnt working. I believe it is a function signature issue. Here is an example:
Colors.h
#ifndef __COLORS
#define __COLORS
#ifdef MYDLL_EXPORTS
/*Enabled as "export" while compiling the dll project*/
#define DLLEXPORT __declspec(dllexport)
#else
/*Enabled as "import" in the Client side for using already created dll file*/
#define DLLEXPORT __declspec(dllimport)
#endif
#include <string>
#include <vector>
class Colors
{
private:
std::string myColor;
static DLLEXPORT std::vector<std::string> allColors;
public:
Colors(){};
Colors(std::string MyColor);
virtual DLLEXPORT std::string getMyColor();
virtual DLLEXPORT void addToColors(std::string color);
std::vector<std::string> getAllColors();
};
typedef Colors* (*create)(std::string);
DLLEXPORT create createColors();
Colors* createColors2(std::string color);
#endif
colors.cpp
#define MYDLL_EXPORTS
#include "Color.h"
std::vector<std::string> Colors::allColors;
Colors::Colors(std::string MyColor)
{
this->myColor = MyColor;
this->allColors.push_back(this->myColor);
}
std::vector<std::string> Colors::getAllColors()
{
return this->allColors;
}
std::string Colors::getMyColor()
{
return this->myColor;
}
Colors* createColors2(std::string color)
{
return new Colors(color);
}
DLLEXPORT void Colors::addToColors(std::string color)
{
this->allColors.push_back(color);
}
DLLEXPORT create createColors()
{
return &createColors2;
}
main.cpp
#define MYDLL_EXPORTS
#include <iostream>
#include <Windows.h>
#include "Color.h"
int main()
{
Colors red("red");
Colors blue("blue");
Colors* dlltest;
//Define the function prototype
typedef Colors* (*createNewColor)();
BOOL freeResult, runTimeLinkSuccess = FALSE;
HINSTANCE dllHandle = NULL;
createNewColor dllCreateNewColor = NULL;
//Load the dll and keep the handle to it
dllHandle = LoadLibrary(L"libs/testerdll.dll");
// If the handle is valid, try to get the function address.
if (NULL != dllHandle)
{
//Get pointer to our function using GetProcAddress:
dllCreateNewColor = (createNewColor)GetProcAddress(dllHandle,"createNewColor");
// If the function address is valid, call the function.
if (runTimeLinkSuccess = (NULL != dllCreateNewColor))
{
dlltest = dllCreateNewColor();
std::cout << "Color of dll class: " << dlltest->getMyColor() << std::endl;
}
else
{
std::cout << "Failed to locate function" << std::endl;
}
//Free the library:
//freeResult = FreeLibrary(dllHandle);
}
else
{
std::cout << "Failed to load library" << std::endl;
}
std::vector<std::string> colorslist = red.getAllColors();
for (std::string color : colorslist)
{
std::cout << color << std::endl;
}
return 0;
}
Dll project
dllmain.cpp
// testerdll.cpp : Defines the exported functions for the DLL application.
#include "stdafx.h"
#include "Color.h"
__declspec(dllexport) Colors* createNewColor()
{
create temp1 = createColors(); //seems to fail here
return nullptr;
}
Yes I know I have memory leaks etc. this was just a quick example code to replicate the problem.
To return the function, you need to get it's address, and return that
e.g.
__declspec(dllexport) create createNewColor()
{
create temp1 = createColors;
return temp1;
}
However, this system (using std::string as a return type, requires that both the .exe and the .dll use the same DLL based runtime library.
stackoverflow : passing reference to STL over function boundary
C++ does not define a calling convention between files. This means that different compilers may set up C++ objects slightly differently. Microsoft limited that with the definition of COM, but that still is a possibility.
Also for visual studio, there are separate heaps (new / delete) between runtime instances. When you link against the dynamic library, all dlls and exes in the process share this DLL. But then they all need to be updated together.
So this process can work, but be wary about :-
Sharing C++ types between binaries (DLL/EXE) - no ABI
Using new in DLL, and delete in EXE. (different heaps).
STL objects are also problematic, as they are a mixture of header implementation (compiled into the binary), and DLL implementation (compiled into C++ runtime).
I am trying to run a main.cpp which access 3 different classes. For some reason, I am getting a unresolved external symbol error. From what I've seen online, its clearly a linking error somewhere, but I cannot find it. I've listed the error below, but it's got a lot of info in it and im having trouble finding out exactly what it means.
The error: main.obj:-1: error: LNK2001: unresolved external symbol "public: __thiscall AtpReader::AtpReader(class std::basic_string,class std::allocator >)" (??0AtpReader##QAE#V?$basic_string#DU?$char_traits#D#std##V?$allocator#D#2##std###Z)
My code is:
main.cpp:
#include <iostream>
#include "atlasobject.h"
#include "atp.h"
#include "atpreader.h"
using namespace std;
int main()
{
AtpReader reader("E:/doc.txt");
return 0;
}
AtpReader.h:
#ifndef ATPREADER_H
#define ATPREADER_H
#include "atp.h"
class AtpReader
{
public:
AtpReader();
AtpReader(string filename);
void atpReadHeader();
void atpRead();
string decryptLine(string line);
ATP readerATP;
private:
string file;
};
#endif // ATPREADER_H
atp.h:
#ifndef ATP_H
#define ATP_H
#include "atlasobject.h"
#include "vector"
struct Image{
string Dim;
string Vox;
string Ori;
char* data;
};
class ATP
{
public:
ATP();
vector<AtlasObject> listOfMaps;
private:
Image referenceImage;
};
#endif // ATP_H
and AtlasObject.h:
#ifndef ATLASOBJECT_H
#define ATLASOBJECT_H
#include <string>
using namespace std;
class AtlasObject{
public:
//virtual void create();
AtlasObject();
void set_uid(string id);
void set_label(string l);
void set_offset(string o);
void set_mapInfo(string info);
void set_data(char* inData);
void set_isEncrypted(int encrypted);
string get_uid();
string get_label();
string get_offset();
string get_mapInfo();
char* get_data();
int get_isEncrypted();
protected:
string uid;
string label;
string offset;
string mapInfo;
char *data;
int isEncrypted;
};
#endif // ATLASOBJECT_H
my AtpReader.cpp is:
#include "atpreader.h"
#include <iostream>
#include <fstream>
#include <stdint.h>
#include <sstream>
AtpReader::AtpReader()
{
printf("AtpReader()\n");
}
AtpReader::AtpReader(string filename)
{
printf("AtpReader(%s)\n",filename.c_str());
}
I see you did not include AtpReader.h in AtpReader.cpp, but probably you just missed it when you made copy/paste, to insert it here, because if you didn't really include it, the error would have been different. Also, I see you're including in your main.cpp both "atlasobject.h"
and "atp.h" and you don't really need that.
Later edit: Your problem is in the atp.h...you constructor is declared but never defined. Do this: ATP(){};
Try using g++ in linux terminal
make the object files of each of the source codes and then link the object files and run the executable
g++ -c atp.cpp AtpReader.cpp AtlasObject.cpp
g++ -o exe atp.o AtpReader.o AtlasObject.o
./exe
AtpReader.cpp is not getting built or the its object file is not getting linked to final executable. Check if AtpReader.obj/.o is created in build directory.
Because of the linker error you are getting and assuming that this is some of your actual code. Since I can't see any function inlining, global constants or variables being used out of scope I think the problem is located in the AtpReader.cpp, are you missing an #include AtpReader.h there?
With just a function prototype, the compiler can continue without error, but the linker cannot resolve a call to an address because there is no function code or variable space reserved. You will not see this error until you create a call to the function that the linker must resolve.
I have a new problem with my C++ DLL... I have tried exporting the entire class instead of only one method. But the program doesn't want to compile now because of that the global scope has no GetUrl
Here is my "UrlConnector.h":
#define ConnectMe __declspec( dllexport )
namespace ConnectHttps
{
class ConnectMe
{
void GetUrl(char *url, unsigned int bufferLength);
};
}
and here is the part of my UrlConnector.cpp that isn't compiling:
#include "UrlConnector.h"
#include "MyConnectionClass.h"
#include
using namespace std;
namespace ConnectHttps
{
void ConnectMe::GetUrl(char* url, unsigned bufferLength)
{
MyConnectionClass initSec;
string response = initSec.GetResult();
strncpy_s(url, bufferLength, response.c_str(), response.length());
}
}
Now, I would like to be able to create an DLL from this, and I would like to make a test program to call the class and the method GetUrl from a dll. I'm using Visual Studio 2010 with Visual C++ DLL.
I've also managed to read this from the MSDN and this tutorial as well, but I just can't seem to get it to work!
I would really appreciate any help!
Unless I'm mistaken, you don't seem to be giving your class a name.
You made ConnectMe not a class name but a macro to export your class, but your class should have a name
Maybe try
#define EXPORT_IT __declspec( dllexport )
namespace ConnectHttps
{
class EXPORT_IT ConnectMe
{
void GetUrl(char *url, unsigned int bufferLength);
};
}
Also I'm not 100% sure of this because I don't have access to a compiler at the moment, but typing:
namespace ConnectHttps {
...
}
In your .cpp file isn't correct. Instead you should have:
void ConnectHttps::ConnectMe::GetUrl(char* url, unsigned bufferLength)
{
MyConnectionClass initSec;
string response = initSec.GetResult();
strncpy_s(url, bufferLength, response.c_str(), response.length());
}