I have a solution with two projects, one DLL and one console app. The console app is a client to call and test functions in the DLL.
For the first function greetings
, I have faced a problem. I should mention that I am totally new to C and C++.
The DLL project (called plugin) looks like this:
plugin.h
#include "types.h" // which contains S32 and #include<string>
#define EXPORT extern "C" __declspec (dllexport)
EXPORT S32 WINAPI _3Greetings(string *str);
plugin.cpp
#include "plugin.h"
S32 __stdcall _3Greetings(string *str)
{
*str = "Hello From Plugin!";
return -1;
}
All DLL functions should return -1
on success or [1-255]
on failure. Also, the project has plugin.def
to solve name decorating of the __stdcall
calling convention.
The console app looks like this:
typedef U32(*GetGreetings)(string);
HMODULE DllHandler = ::LoadLibrary(L"plugin.dll");
if (DllHandler != NULL) {
string greetingText;
GetGreetings greetings = reinterpret_cast<GetGreetings>(GetProcAddress(DllHandler2, "_3Greetings"));
greetings(&greetingText); // THE PROBLEM HERE
cout << greetings << endl;
}
The problem is if I add &
to greetingText
, I get an error:
E0415 no suitable constructor exists to convert from "std::string *" to "std::basic_string, std::allocator>"
And also:
C2664 'U32 (std::string)': cannot convert argument 1 from 'std::string *' to 'std::string'
If I do not put the &
, I get a runtime exception:
Exception thrown at 0x0FA65A72 (plugin.dll) in ConsoleApp.exe: 0xC0000005: Access violation writing location 0xCCCCCC00.
The typedef for GetGreeting is wrong, it misses a *
User contributions licensed under CC BY-SA 3.0