-2

I have a dll project (Server.dll) containing a Server.cpp

Server.cpp

#include "pch.h"
#include "Server.hpp"
extern "C" {
    _declspec(dllexport) int Server::Add(int a, int b)
    {
        return a + b;
    }
}
#define Function(  Y )  \
\
extern "C" __declspec( dllexport)\
    std::string Server::Y(std::string const& name) {\
    return name; \
}\

I use these two functions in another project client.exe

Here id the main

#include <Windows.h>
#include <iostream>
typedef int(*pAdd) (int a, int b);
int main()
{
    std::string path = "D:\\project\\Server.dll";
    std::wstring stemp = std::wstring(path.begin(), path.end());
    LPCWSTR sw = stemp.c_str();
    HINSTANCE hinstance = LoadLibrary(sw);
    if(!hinstance)
        std::cout << "canot load library\n";
    pAdd obj = (pAdd)GetProcAddress(hinstance, "Add");
    if (obj) {
        int result = obj(10, 20);
        std::cout << "result = " << result << std::endl;
    }
    std::string func = "Client";
    std::cout << "address = " << GetProcAddress(hinstance, "Y");
}

I can load the Add function but I can't load the Y function (address = 0000000000)

Any suggestions please ?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
SADL
  • 37
  • 5
  • Please don't post images of text! Copy-paste text *as text* into your questions. – Some programmer dude May 17 '22 at 10:24
  • 4
    If you expect macro to be called like a function, you don't understand what a macro is. –  May 17 '22 at 10:32
  • 3
    `Function` isn't a function, it's a macro. And I don't see you use it anywhere. Therefore it's not expanded and no coded added. I recommend you search for a C++ preprocessor and macros tutorial to learn more about macros, what they are and what they do (and don't do). – Some programmer dude May 17 '22 at 10:38
  • How i can use a macro defined in dll project in another exe project ? – SADL May 17 '22 at 10:40
  • 2
    You might want to learn about the concept of [*translation units*](https://en.wikipedia.org/wiki/Translation_unit_(programming)) as well. A compiler only deals in translation units. A macro doesn't exist outside the current translation unit. And macros don't lead to any code generation if it's not used. You can't "export" macros, and you can't "import" them in other unrelated files and definitely not in a process. – Some programmer dude May 17 '22 at 10:41
  • It is very unclear why you expect `#define Function( Y )` to define a function called "Y". `Function(SomeName)` expands to an exported definition of `std::string Server::SomeName(std::string const& name)`. I think you should get a [good book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – molbdnilo May 17 '22 at 12:46

1 Answers1

0

I want to post an example of the solution here maybe someone needs one day to create a macro function containing a function:

I have a dll project containing a class:

Here the code

#include "pch.h" 
#include <iostream>
#define Function(  Y )  \
\
extern "C" __declspec( dllexport)\
int Y(int a, int b) {\
return (a+b); \
}\
class TestMacro {
Function(Add);
};

In another exe project i loaded this function and use it here the code:

#include <Windows.h>
#include <iostream>
typedef int(*Y)(int a, int b);
int main()
{
std::string path = "D:\\project\\Server.dll";
std::wstring stemp = std::wstring(path.begin(), path.end());
LPCWSTR sw = stemp.c_str();
HINSTANCE hinstance = LoadLibrary(sw);
if(!hinstance)
std::cout << "canot load library\n";
std::cout << "address = " << GetProcAddress(hinstance, "Add")<< std::endl;
Y y = (Y)GetProcAddress(hinstance, "Add");
int result = y(2,3);
std::cout << "appel Y = " << result<< std::endl;
}

Here the ouput

address = 00007FFBF98C132F
appel Y = 5
SADL
  • 37
  • 5