-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 ?

6
  • Please don't post images of text! Copy-paste text as text into your questions. Commented May 17, 2022 at 10:24
  • 4
    If you expect macro to be called like a function, you don't understand what a macro is. Commented May 17, 2022 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). Commented May 17, 2022 at 10:38
  • How i can use a macro defined in dll project in another exe project ? Commented May 17, 2022 at 10:40
  • 2
    You might want to learn about the concept of translation units 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. Commented May 17, 2022 at 10:41

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.