我正在使用一些标准库为某些设备开发通用接口(interface)。 在我的某些代码部分
//header.h
#ifndef _M_X64
# define GC_CALLTYPE __stdcall
#else
# define GC_CALLTYPE /* default */ //this is my case
#endif
...
typedef int32_t GC_ERROR;
...
/* typedefs for dynamic loading */
#define GC_API_P(function) typedef GC_ERROR( GC_CALLTYPE *function )
GC_API_P(PTLOpen)( TL_HANDLE *phTL );
在我的源文件中有
//source1.cpp
TLOpen(&hTl)
//source2.cpp
#define FUNCTION_POINTER(function, ptype, hModule) \
((function) = reinterpret_cast<ptype>(GetProcAddress((hModule), #function))) // What is this #?
GC::PTLOpen TLOpen = 0;
FUNCTION_POINTER(TLOpen, GC::PTLOpen, hModule);
我想知道:
TLopen()
声明是什么?我替换了宏并得到了这个:
typedef int32_t( GC_CALLTYPE *PTLOpen )( TL_HANDLE *phTL );
但是我学习函数指针的方式不同,我期望是这样的:
typedef int32_t( *PTLOpen )( TL_HANDLE *phTL );
上面的声明还是函数指针吗? GC_CALLTYPE
怎么样?
在定义
FUNCTION_POINTER
宏时,function
之前的#
符号是什么?TLopen()
函数的主体在哪里?我要包含一些.lib
和.dll
文件。正文是否可以编译形式存在于这些文件中?
最佳答案
GC_CALLTYPE
位于 calling convention 所在的位置说明符可以是(如__stdcall
)。 可以是因为不要忘记GC_CALLTYPE
也可以很容易地扩展为空字符串。快速搜索给出了一个问题,在此处讨论此语法的位置:How to declare an __stdcall function pointer这称为字符串化:例如,如果相应的宏参数是
xyz
,则#function
将扩展为"xyz"
。更多 here .是的,实际上可以。您的 API 将告诉您如何进行编译,以便程序可以找到该函数。这就是拥有图书馆和 linker 的全部目的。 .
关于c++ - 这是否仍然为一种指针函数声明别名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41077194/