如何在DLL中的exe中调用定义的函数? [英] How to call a function defined in my exe inside my DLL?

查看:219
本文介绍了如何在DLL中的exe中调用定义的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在exe文件中定义了一些通用功能,例如 Log(char * str)。此函数将字符串作为调用方的输入,并将其写入为该应用程序定义的日志文件。现在,我想编写DLL代码,其中包含将文件上传到服务器的功能。

I have some general purpose functions defines in my exe file like Log(char* str). This function takes a string as an input from the caller and writes it to the logfile defined for that application. Now I want to write DLL code which will include a function to upload a file to a server.

目标是将DLL的上传功能导入exe并使用它。但是,如果遇到任何错误,则上载函数必须调用 Log(char * str)函数将错误写入日志文件。

The objective is to import the upload function from the DLL into the exe and use it. However, if any error is encountered, then the upload function must call the Log(char* str) function to write the error into the log file.

问题在于此DLL需要在多个应用程序中使用,并且每个应用程序在不同的位置都有不同的日志文件。我想以一种这样的方式编写DLL,使其调用应用程序中定义的相应 Log(char * str)。同样,我还有其他一些特定于应用程序的功能,这些功能不能事先包含在DLL中。

The trouble is that this DLL needs to be used in multiple applications and each app will have a different logfile at a different location. I want to write the DLL in such a way that it calls the corresponding Log(char* str) defined in the application. Similarly I have some other functions that are application-specific and cannot be included in the DLL beforehand.

如何在只知道函数原型而不知道函数定义的DLL代码中编写该DLL代码?

How can I write such DLL code where it only knows the function prototype, but not the function definition, which resides inside the exe?

推荐答案

在DLL中具有.exe可以调用的函数,并传递指向 Log 功能。在DLL内,存储该函数指针,并在需要记录内容时使用它。

Have a function in your DLL that the .exe can call, passing in a function pointer to your Log function. Inside the DLL, store that function pointer and use it when you need to log things.

如果有多个此类函数需要传递给DLL,请考虑使用 struct 来存储所有相关的函数指针。

If there is more than one such function you need to pass to your DLL, consider using a struct to store all the relevant function pointers.

这里有一个很好的函数指针教程:函数指针教程

Here's a good tutorial on function pointers: The function pointer tutorial

要保留此功能界面简单,我强烈建议至少将所有这些可自定义功能都保留在 C 中。处理指向成员函数的指针并通过 C 包装器传递 C ++ 对象容易出错。 (但可行。)或者还是坚持使用 C ++

To keep this interface simple, I would highly recommend keeping all these "customizable" functions in plain C, at least as a starting point. Dealing with pointer-to-member functions and passing C++ objects around through C wrappers is error-prone. (But doable.) Or stick with C++ all round.

下面是一个简短的例子。在公共标头中,输入以下内容:

Here's a short example. In a common header, put something like this:

typedef void (*logfunc)(char const*);

简化自定义函数指针的传递。

to simplify passing around the customized function pointer.

然后在您的DLL代码中,您可以:

In your DLL code, you could then have:

logfunc cust_log;

void dll_init_logging(logfunc l)
{
    cust_log = l;
}

void dll_do_something()
{
    cust_log("hello");
}

进行修改,以使这些功能在您的平台上可导出。

Modify that to make these functions exportable if necessary on your platform.

然后从您的主代码中,您需要做的所有事情(假设您正在加载DLL并使用原始名称将导出的函数提供给您的.exe使用):

Then from your main code, all you need to do (assuming you're loading the DLL and making the exported functions available to your .exe with their original name) is:

void logit(char const* str)
{
    printf("Log: %s\n", str);
}

int main (int argc, char const *argv[])
{
    // load the DLL
    dll_init_logging(logit);
    ...
    dll_do_something();
    ...
    return 0;
}

这篇关于如何在DLL中的exe中调用定义的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆