将函数指针作为参数传递给dll函数是安全的,并从dll内部调用它们吗? [英] Is it safe to pass function pointers as arguments to dll functions and invoke them from inside of the dll?

查看:209
本文介绍了将函数指针作为参数传递给dll函数是安全的,并从dll内部调用它们吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一些(dll或者不是)函数指针作为参数传递给一些dll函数,并从dll的内部调用它们。我想知道是否安全,因为我已经找到了关于 http://publib.boulder.ibm.com/infocenter/zos/v1r10/index.jsp?topic=/com.ibm.zos.r10.cbcpx01/fpref .htm



在DLL代码中,假定函数指针指向一个函数描述符。通过首先通过取消引用指针来获取函数地址来进行函数指针调用;然后分支到功能项。当非DLL函数指针传递给DLL代码时,它直接指向函数条目。尝试通过这样的指针取消引用会产生未定义的函数地址。后续分支到未定义的地址可能会导致异常。



此规则是否也适用于Visual Studio和其他编译器?



我正在努力做的是解决各种DLL和非DLL函数之间的内存分配和释放问题。我的想法是在一些初始化(例如Initialize(& malloc,& free))中传递两个函数指针 - 用于公共分配和释放功能 - 到每个dll,然后使用这些常见的,从而始终兼容的功能进行所有的内存管理。

解决方案

这不是真的。 DLL代码以与非DLL代码完全相同的方式处理函数指针。如果不是这种情况,则不能在DLL中使用标准库函数,如qsort()(期望函数指针参数)。


I would like to pass some (dll or not) function pointers as arguments to some dll functions and call them from inside of the dll. I wonder if it is safe because I have found an information on http://publib.boulder.ibm.com/infocenter/zos/v1r10/index.jsp?topic=/com.ibm.zos.r10.cbcpx01/fpref.htm that:

In DLL code, it is assumed that a function pointer points to a function descriptor. A function pointer call is made by first obtaining the function address through dereferencing the pointer; and then, branching to the function entry. When a non-DLL function pointer is passed to DLL code, it points directly to the function entry. An attempt to dereference through such a pointer produces an undefined function address. The subsequent branching to the undefined address may result in an exception.

Does this rule apply to Visual Studio and other compilers as well?

What precisely I am trying to do is to solve the problem of memory allocation and deallocation between various dll and non-dll functions. My idea is to pass two function pointers - for common allocation and deallocation functions - to every dll in some initialization (e.g. Initialize(&malloc, &free)) and then do all memory management using these common and thus always compatible functions.

解决方案

It's not true. DLL code treats function pointers in exactly the same way as non-DLL code. If that were not the case, one could not use standard library functions like qsort() (which expects a function pointer argument) within a DLL.

这篇关于将函数指针作为参数传递给dll函数是安全的,并从dll内部调用它们吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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