0

I am totally new in C++ and starting with creating a simple dll and console app that tests the dll. The dll plugin afterwards should work on x86 machines (diag tools, ECU or PLC). The samples given to me that I fallow the same structure exports dll function as __sdcall. so my helloWorld project looks like:

plugin.dll
-----------
plugin.h
#pragma once
#include "types.h"
#define EXPORT extern "C" __declspec (dllexport)
EXPORT S32 WINAPI Greetings(string *str);

plugin.cpp
#include "plugin.h"
#include "types.h"
S32 __stdcall Greetings(string *str){*str = "Hello From Plugin!"; return -1;}

and the console app looks like: (both are in same solution, project/properties/cc++/adnvances/callingConvention = __cdecl (/Gd))

VS settings - Solution configuration=debug, Solution Platforms=x86

main.cpp
HMODULE DllHandler = ::LoadLibrary(L"plugin.dll");
string greetingText;
typedef U32(*Type)(string*);
Type greetings = reinterpret_cast<Type>(GetProcAddress(DllHandler, "Greetings"));
greetings(&greetingText);
cout << greetingText << endl;

Now, without plugin.def the GetProcAddress(DllHandler, "Greetings") returns null (0x000000) with the plugin.def (alongside with EXPORT) I get the Greetings with decoration plugin.dll!Greetings@4 the call will succeed but get

Run-Time Check Failure #0 - The value of ESP was not properly saved across a function call. This is usually a result of calling a function declared with one calling convention with a function pointer declared with a different calling convention.

also no casting with naming convention is allowed. I red many posts about __cdecl __stdcall but mostly explaining the assembly level that who cleans the stack, or using either .def or extern "C" with export.

I totally got lost in naming convention and mangling in C++. is related to local project setting or the dll will run on all environments? specially in this sample project how should I handle it?

Amir-Mousavi
  • 4,273
  • 12
  • 70
  • 123
  • 3
    Your exported function has a parameter of type `string`. Are you aware that clients of your dll must be compiled with the same compiler version. The different `std::string` implementations of different compiler and versions are not compatible. – Micha Wiedenmann Oct 24 '18 at 16:03
  • It is only abut using std::string? If I use i.e. const char* the exeption will be gone? – Amir-Mousavi Oct 24 '18 at 16:07
  • Name mangling (or actually the entire ABI) is *not* standardized. You need to build *everything* with the *exact same* compiler. There are no standard rules for this. – Jesper Juhl Oct 24 '18 at 16:09
  • I believe there is binary compatibility between VS 2015 and VS 2017 but no compatibility between VS and any other compiler version or vendor. https://learn.microsoft.com/en-us/cpp/porting/binary-compat-2015-2017?view=vs-2017 – drescherjm Oct 24 '18 at 16:15
  • 1
    @drescherjm Yeah. MS did make that guarantee. But that's just MS making a promise for *specific versions* of *their* tools. In *general*, nothing is guaranteed. – Jesper Juhl Oct 24 '18 at 16:17

0 Answers0