[英]How can i export a macro function and use it in a project .exe
I have a dll project (Server.dll) containing a Server.cpp我有一个包含 Server.cpp 的 dll 项目 (Server.dll)
Server.cpp服务器.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 an other project client.exe我在另一个项目client.exe中使用这两个函数
Here 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 Add function but i can't load Y function (address = 0000000000)我可以加载添加函数但我无法加载 Y 函数(地址 = 0000000000)
Any suggestions please ?请问有什么建议吗?
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:我有一个包含类的 dll 项目:
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:在另一个 exe 项目中,我加载了这个函数并在这里使用它的代码:
#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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.