GetProcAddress fails [duplicate] - c++

This question already has answers here:
Exporting functions from a DLL with dllexport
(4 answers)
Closed 4 years ago.
I have a fucntion in dynamic library which looks like:
namespace Dll {
int MyDll::getQ() {
srand(time(0));
int q = rand();
while (!isPrime(q) || q < 100) {
q = rand();
}
return q;
}
}
Function getQ() in .h file:
#ifdef _EXPORT
#define DLL_EXPORT __declspec(dllexport)
#else
#define DLL_EXPORT __declspec(dllimport)
#endif
namespace Dll
{
class MyDll
{
public:
static DLL_EXPORT int __stdcall getQ();
}
}
And finally LoadLibrary peace of code from another consoleApp:
typedef int(__stdcall *CUSTOM_FUNCTION)();
int main()
{
HINSTANCE hinstance;
CUSTOM_FUNCTION proccAddress;
BOOL freeLibrarySuccess;
BOOL proccAddressSuccess = FALSE;
int result;
hinstance = LoadLibrary(TEXT("Dll.dll"));
if (hinstance != NULL)
{
proccAddress = (CUSTOM_FUNCTION)GetProcAddress(hinstance, "getQ");
if (proccAddress != NULL)
{
proccAddressSuccess = TRUE;
result = (*proccAddress)();
printf("function called\n");
printf("%d", result);
}
freeLibrarySuccess = FreeLibrary(hinstance);
}
if (!hinstance)
printf("Unable to call the dll\n");
if (!proccAddressSuccess)
printf("Unable to call the function\n");
}
So I tried to fix this several times but I always get "Unable to call the function". The code connects to the library so the problem is somewhere near the function.
I'll appreciate if someone will point me on my mistake.

You are missing an Extern "C".
If you don't, names will be c++ mangled and you cannot find them with getQ name only. In addition, it will not be reliable to do so, because name mangling may change.
Another topic is here to: _stdcall vs _cdecl

Related

dflcn c++ script alternative for windows [duplicate]

I wrote a c-code designed for linux platform.
Now, I want to make it cross-platform so to use in Windows as-well.
In my code, I dlopen an so file and utilize the functions inside it.
Below is how my code looks like. But I just found out that in windows, the way to load and use dynamic library is quite different.
void *mydynlib
mydynlib= dlopen("/libpath/dynlib.so",RTLD_LAZY);
void (*dynfunc1)() = dlsym(mydynlib,"dynfunc1");
void (*dynfunc2)(char*, char*, double) = dlsym(mydynlib,"dynfunc2");
int (*dynfunc3)() = dlsym(mydynlib,"dynfunc3");
From what I found, I need to use LoadLibrary & GetProcAddress instead of dlopen & dlsym. However, I do not know how to convert above line for windows using those. I've tried to search some examples for hours but couldn't find exact solution. If someone had this kind of experience, please give me a tip.
Excuse me if this is too obvious problem. I'm quite new to C. I usually write my program in python.
Once in my youth I created something like this:
/* dlfcn.h */
#ifndef DLFCN_H
#define DLFCN_H
#define RTLD_GLOBAL 0x100 /* do not hide entries in this module */
#define RTLD_LOCAL 0x000 /* hide entries in this module */
#define RTLD_LAZY 0x000 /* accept unresolved externs */
#define RTLD_NOW 0x001 /* abort if module has unresolved externs */
/*
How to call in Windows:
void *h = dlopen ("path\\library.dll", flags)
void (*fun)() = dlsym (h, "entry")
*/
#ifdef __cplusplus
extern "C" {
#endif
void *dlopen (const char *filename, int flag);
int dlclose (void *handle);
void *dlsym (void *handle, const char *name);
const char *dlerror (void);
#ifdef __cplusplus
}
#endif
#endif
and dlfcn.c:
/* dlfcn.c */
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <windows.h>
static struct {
long lasterror;
const char *err_rutin;
} var = {
0,
NULL
};
void *dlopen (const char *filename, int flags)
{
HINSTANCE hInst;
hInst= LoadLibrary (filename);
if (hInst==NULL) {
var.lasterror = GetLastError ();
var.err_rutin = "dlopen";
}
return hInst;
}
int dlclose (void *handle)
{
BOOL ok;
int rc= 0;
ok= FreeLibrary ((HINSTANCE)handle);
if (! ok) {
var.lasterror = GetLastError ();
var.err_rutin = "dlclose";
rc= -1;
}
return rc;
}
void *dlsym (void *handle, const char *name)
{
FARPROC fp;
fp= GetProcAddress ((HINSTANCE)handle, name);
if (!fp) {
var.lasterror = GetLastError ();
var.err_rutin = "dlsym";
}
return (void *)(intptr_t)fp;
}
const char *dlerror (void)
{
static char errstr [88];
if (var.lasterror) {
sprintf (errstr, "%s error #%ld", var.err_rutin, var.lasterror);
return errstr;
} else {
return NULL;
}
}
You could use a set of macros that change depending on the OS you're on:
#ifdef __linux__
#define LIBTYPE void*
#define OPENLIB(libname) dlopen((libname), RTLD_LAZY)
#define LIBFUNC(lib, fn) dlsym((lib), (fn))
#elif defined(WINVER)
#define LIBTYPE HINSTANCE
#define OPENLIB(libname) LoadLibraryW(L ## libname)
#define LIBFUNC(lib, fn) GetProcAddress((lib), (fn))
#endif

I'm getting error 127 when trying to get function address

When I try and get a proc address for a function called print, it is able to load the ManualLinking.dll but not the function. The error code that windows gives is 127. The client app is almost a direct copy paste from windows.
DLL:
#include"pch.h"
#include<string>
#include<iostream>
__declspec(dllexport) void __stdcall print(std::string data) {
std::cout << data << std::endl;
}
CPP:
#include <windows.h>
#include<iostream>
#include"Header.h"
#include<string>
typedef void(__stdcall* MYPROC)(std::string data);
int main(void)
{
HINSTANCE hinstLib;
MYPROC ProcAdd;
BOOL fFreeResult, fRunTimeLinkSuccess = FALSE;
// Get a handle to the DLL module.
hinstLib = LoadLibrary(TEXT("ManualLinking.dll"));
// If the handle is valid, try to get the function address.
if (hinstLib != NULL)
{
ProcAdd = (MYPROC)GetProcAddress(hinstLib, "print");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd)("Message sent to the DLL function\n");
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
// If unable to call the DLL function, use an alternative.
if (!fRunTimeLinkSuccess) {
printf("Message printed from executable\n");
std::cout << GetLastError() << std::endl;
}
std::cin.get();
return 0;
}
You need to replace DLL code with:
#include"pch.h"
#include<string>
#include<iostream>
extern "C"{
__declspec(dllexport) void __stdcall print(std::string data) {
std::cout << data << std::endl;
}
}
If you still have same error, please, check if the executable can actually find the .dll
/*...*/
if (hinstLib != NULL)
{
ProcAdd = (MYPROC)GetProcAddress(hinstLib, "print");
// If the function address is valid, call the function.
if (NULL != ProcAdd)
{
fRunTimeLinkSuccess = TRUE;
(ProcAdd)("Message sent to the DLL function\n");
}
// Free the DLL module.
fFreeResult = FreeLibrary(hinstLib);
}
else
{
std::cout << "Cannot find dll" << std::endl;
}
/*...*/

32 bit WPF .NET app can't find its 32 bit dll

I use P/Invoke, not C++/CLI and am working on Visual Studio 2017.
My app is WPF.NET – 32 bit, written in C#. The app loads successfully its 32-bit DLL when I run it on my PCx64, but it crashes when I run it on a virtual machine (64 or 32). I think couldn't add the DLL...and don't know why.
My DLL is written on C++ and includes a class, with some functions written on Assembler. May be the problem comes from it?
MyClass.cpp:
int MyClass::Foo_1()
{
__asm
{
mov eax, 0
}
}
void MyClass::SetFoo_1()
{
resultEAX = new int;
int a = -1;
try
{
a = Foo_1();
}
catch (int e) { }
if (a == 0) {
*resultEAX = 0;
}
else {
*resultEAX = 1;
}
}
MyClass.h:
#ifndef MYCLASS_H
#define MYCLASS_H
class __declspec(dllexport) MyClass {
public:
int * resultEAX ;
int Foo1();
void SetFoo_1();
int GetEAX();
};
#endif
MyClassCaller.h:
extern "C" {
#endif
__declspec(dllexport) MyClass* Create();
__declspec(dllexport) void Dispose(MyClass* a_pObject);
__declspec(dllexport) void SetFoo_1(MyClass* a_pObject);
__eclspec(dllexport) int GetEAX(MyClass* a_pObject);
#ifdef __cplusplus
}
#endif
MyClassCaller.cpp:
Graphics* Create()
{
return new MyClass();
}
void Dispose(MyClass * a_pObject)
{
if (a_pObject != NULL)
{
delete a_pObject;
a_pObject = NULL;
}
}
void SetFoo_1(MyClass * a_pObject)
{
if (a_pObject != nullptr)
{
a_pObject->SetFoo_1();
}
}
int GetEAX(MyClass * a_pObject)
{
if (a_pObject != NULL)
{
return a_pObject->GetEAX();
}
return 0;
}
And I call the class from WPF.NET using managed C# code:
IntPtr pMyClass = MyClassHandling.Create();
Int32 a = 0;
Int64 b = 0;
long c = 0;
long rslt = 0;
try
{
MyClassHandling.SetFoo_1(pMyClass);
if (Environment.Is64BitOperatingSystem)
{
//"SysWOW64"
b = MyClassHandling.GetEAX(pMyClass);
//……
}
else
{
//"system32"
a = pMyClassHandling.GetEAX(pGraphics);
//…
}
MyClassHandling.Dispose (pGraphics);
pMyClass = IntPtr.Zero;
[DllImport("SomeAssemblerFunctions.dll")]
static public extern IntPtr Create();
[DllImport("SomeAssemblerFunctions.dll")]
static public extern void Dispose(IntPtr pGraphicsObject);
[DllImport("SomeAssemblerFunctions.dll", EntryPoint = "SetGraphicMode1")]
static public extern void SetFoo_1(IntPtr pGraphicsObject);
[DllImport("SomeAssemblerFunctions.dll", EntryPoint = "GetEAX")]
static public extern int GetEAX(IntPtr pGraphicsObject);

Object initialization differences in afxmem.cpp

I have an object in function z which I'm accessing from functions x and y. The problem is that it's initialized through different functions in the afxmem.cpp (VS 11.0 file).
Here's my code;
classA
{
public:
ADesign *a_d;
};
void classA::functionZ()
{
a_d = new ADesign;
}
//functionX and functionY both calls same function
void classB::functionX()
{
ca.functionZ();
}
void classB::functionY()
{
ca.functionZ();
}
//sample initializer for ADesign
ADesign::ADesign()
:doc_(0)
{
version_number = 7.82f;
d_file = "";
c_file = "";
angle = ID_ANGLE_MER;
machine_type = MACHINE_COMPRESS;
//...etc
}
When it's being initialized, it goes through these functions in afxmem.cpp for function x
void* PASCAL
CObject::operator new(size_t nSize, LPCSTR lpszFileName, int nLine)
{
return ::operator new(nSize, _AFX_CLIENT_BLOCK, lpszFileName, nLine);
}
void* __cdecl operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine)
{
#ifdef _AFX_NO_DEBUG_CRT
UNUSED_ALWAYS(nType);
UNUSED_ALWAYS(lpszFileName);
UNUSED_ALWAYS(nLine);
return ::operator new(nSize);
#else
void* pResult;
#ifdef _AFXDLL
_PNH pfnNewHandler = _pfnUninitialized;
#endif
for (;;)
{
pResult = _malloc_dbg(nSize, nType, lpszFileName, nLine);
if (pResult != NULL)
return pResult;
#ifdef _AFXDLL
if (pfnNewHandler == _pfnUninitialized)
{
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
pfnNewHandler = pState->m_pfnNewHandler;
}
if (pfnNewHandler == NULL || (*pfnNewHandler)(nSize) == 0)
break;
#else
if (_afxNewHandler == NULL || (*_afxNewHandler)(nSize) == 0)
break;
#endif
}
return pResult;
#endif
}
And this function in afxmem.cpp for function y;
void* __cdecl operator new(size_t nSize)
{
void* pResult;
#ifdef _AFXDLL
_PNH pfnNewHandler = _pfnUninitialized;
#endif
for (;;)
{
#if !defined(_AFX_NO_DEBUG_CRT) && defined(_DEBUG)
pResult = _malloc_dbg(nSize, _NORMAL_BLOCK, NULL, 0);
#else
pResult = malloc(nSize);
#endif
if (pResult != NULL)
return pResult;
#ifdef _AFXDLL
if (pfnNewHandler == _pfnUninitialized)
{
AFX_MODULE_THREAD_STATE* pState = AfxGetModuleThreadState();
pfnNewHandler = pState->m_pfnNewHandler;
}
if (pfnNewHandler == NULL || (*pfnNewHandler)(nSize) == 0)
break;
#else
if (_afxNewHandler == NULL || (*_afxNewHandler)(nSize) == 0)
break;
#endif
}
return pResult;
}
When it goes through function y, it doesn't get initialized properly. I'm accessing the program through the command line for function y.
Is there a reason as to why the initialization is happening in 2 different ways for the same object?
Thanks.
At the beginning of your .cpp files you may or may not have this line:
#define new DEBUG_NEW
If this line is present, then new will end up invoking this function:
void* __cdecl operator new(size_t nSize, int nType, LPCSTR lpszFileName, int nLine)
if it is not present then new will end up invoking this function:
void* __cdecl operator new(size_t nSize)
That's internal MFC debugging stuff.
But that doesn't explain why something doesn't get initialized properly. You need to elaborate your question, it's not clear enough.

Is there a windows concurrency_queue.h equivalent on linux?

Well I am tryng to have a queue which is concurrent, but concurrency_queue isn't standard C++ and it's for windows, linux doesn't have it. Is there anything for linux like this (with the same functions like in the windows equivalent?)?
Edit:
This is needed to port this windows code to linux:
#include <concurrent_queue.h>
#ifdef defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#define SLEEP(x) { Sleep(x); }
#include <windows.h>
#include <process.h>
#define OS_WINDOWS
#define EXIT_THREAD() { _endthread(); }
#define START_THREAD(a, b) { _beginthread( a, 0, (void *)( b ) ); }
#else
#include <pthread.h>
#define sscanf_s sscanf
#define sprintf_s sprintf
#define EXIT_THREAD() { pthread_exit( NULL ); }
#define START_THREAD(a, b) { pthread_t thread;\
pthread_create( &thread, NULL, a, (void *)( b ) ); }
#endif
using namespace std;
using namespace Concurrency;
struct QuedData
{
int start;
int end;
int extraid;
AMX * script;
QuedData(){start = 0;end = 0;extraid = 0;script = NULL;}
QuedData(int start_,int end_,int extraid_, AMX * script_){start = start_;end = end_;extraid = extraid_;script = script_;}
};
struct PassData //thanks to DeadMG for improvements.
{
std::vector<cell> Paths;
int extraid;
AMX * script;
cell MoveCost;
PassData(){extraid = 0;script = NULL;MoveCost = 0;Paths.clear();}
template<typename Iterator> PassData(Iterator begin, Iterator end, int extraid_, cell MoveCost_, AMX * script_)
: Paths(begin, end)
{extraid = extraid_;MoveCost = MoveCost_;script = script_;}
~PassData(){Paths.clear();}
};
concurrent_queue <QuedData> QueueVector;
concurrent_queue <PassData> PassVector;
PassData LocalPass;
void PLUGIN_CALL
ProcessTick()
{
if(PassVector.try_pop(LocalPass))
{
amx_Push(LocalPass.script, LocalPass.MoveCost);
//blabla
}
}
static cell AMX_NATIVE_CALL n_CalculatePath( AMX* amx, cell* params )
{
QueueVector.push(QuedData(params[1],params[2],params[3],amx));
return 1;
}
bool PLUGIN_CALL Load( void **ppData )
{
START_THREAD( Thread::BackgroundCalculator, 0);
return true;
}
QuedData RecievedData;
vector <cell>tbcway;
cell tbccostx;
#ifdef OS_WINDOWS
void Thread::BackgroundCalculator( void *unused )
#else
void *Thread::BackgroundCalculator( void *unused )
#endif
{
while( true ){
if(QueueVector.try_pop(RecievedData)){
dgraph->findPath_r(xNode[RecievedData.start].NodeID ,xNode[RecievedData.end].NodeID,tbcway,tbccostx);
PassVector.push(PassData(tbcway.begin(),tbcway.end(),RecievedData.extraid,tbccostx,RecievedData.script));
}
SLEEP(5);
}
EXIT_THREAD();
}
The Visual C++ concurrent_queue is actually based on the Intel Threading Building Block Library
(If you open concurrent_queue.h header file in VC++ you will see an acknowledgement)
You can get the library from
http://threadingbuildingblocks.org/
The library will run on Linux as well.
I think threadpool does this or an unofficial Boost enhancement called lockfree and should by now be part of Boost::Atomics. I haven't use both but let us know if you have any luck.
I would suggest looking at https://github.com/romanek-adam/boost_locking_queue for the code and the article that goes with it at Implementing a Thread-Safe Queue using Condition Variables