返回DLL函数的函数指针 [英] Return function pointer of DLL functions

查看:80
本文介绍了返回DLL函数的函数指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!

我在MFC中编写自己的dll并希望在我的基于MFC对话的应用程序中调用dll的函数。在DLL中有多个函数返回Handle,int等。我想在对话应用程序上调用不同事件的函数。

所以我的问题是如何全局声明该函数的函数指针并调用该函数的那个​​函数?

请给出解决方案!!!

Hello!
I write my own dll in MFC and want to call the functions of dll in my MFC dialogue based application.There are multiple functions in DLL which are return Handle,int etc. and I would like to call that functions on different events happened on dialogue application.
so my question is how to declare function pointer of that functions globally and call that function with respect to that event?
Please give solution!!!

推荐答案

通过示例和解释看一下这篇文章: DYNAMIC LINK LIBRARY - DLL [ ^ ]



基本上你可以使用两种方法将公共符号导入应用程序或从DLL导出函数:

- 使用构建DLL时的模块定义(.DEF)文件。



最小.DEF文件必须包含fo llowing模块定义语句:

文件中的第一个语句必须是LIBRARY语句。此语句将.DEF文件标识为属于DLL。 LIBRARY语句后跟DLL的名称。链接器将此名称放在DLL的导入库中。



EXPORTS语句列出DLL导出的函数的名称和(可选)序数值。您可以通过使用带符号(@)和数字的函数名称来为函数指定序数值。当您指定序数值时,它们必须在1到N的范围内,其中N是DLL导出的函数的数量。



例如,一个DLL包含实现二叉搜索树的代码可能如下所示:

Take a look at this article with example and explanation: DYNAMIC LINK LIBRARY - DLL[^]

Basically you can import public symbols into an application or export functions from a DLL using two methods:
- Use a module definition (.DEF) file when building the DLL.

A minimal .DEF file must contain the following module-definition statements:
The first statement in the file must be the LIBRARY statement. This statement identifies the .DEF file as belonging to a DLL. The LIBRARY statement is followed by the name of the DLL. The linker places this name in the DLL''s import library.

The EXPORTS statement lists the names and, optionally, the ordinal values of the functions exported by the DLL. You assign the function an ordinal value by following the function''s name with an at sign (@) and a number. When you specify ordinal values, they must be in the range 1 through N, where N is the number of functions exported by the DLL.

For example, a DLL that contains the code to implement a binary search tree might look like the following:
LIBRARY   BTREE
EXPORTS
   Insert   @1
   Delete   @2
   Member   @3
   Min   @4





- 使用关键字__declspec(dllimport )或主应用程序中函数定义中的__declspec(dllexport):





- Use the keywords __declspec(dllimport) or __declspec(dllexport) in a function definition in the main application:

__declspec(dllexport) int mydll(LPTSTR lpszMsg)



这里有一个示例如何使用显式链接在运行时加载DLL:


And here an example how to use explicit linking to load the DLL at run time:

typedef UINT (CALLBACK* LPFNDLLFUNC1)(DWORD, UINT);


HINSTANCE hDLL;               // Handle to DLL
LPFNDLLFUNC1 lpfnDllFunc1;    // Function pointer
DWORD dwParam1;
UINT  uParam2, uReturnVal;
hDLL = LoadLibrary("MyDLL");

if (hDLL != NULL)
{
   lpfnDllFunc1 = (LPFNDLLFUNC1)GetProcAddress(hDLL, "DLLFunc1");
   if (!lpfnDllFunc1)
   {
      // handle the error
      FreeLibrary(hDLL);

      return SOME_ERROR_CODE;
   }
   else
   {
      // call the function
      uReturnVal = lpfnDllFunc1(dwParam1, uParam2);
   }
}


这篇关于返回DLL函数的函数指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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