结构体
/*PHP扩展中函数参数结构*/
typedef struct _zend_arg_info {
const char *name; //参数名
zend_uint name_len; //参数名长度
const char *class_name; //类名
zend_uint class_name_len; //类名长度
zend_bool array_type_hint;
zend_bool allow_null;
zend_bool pass_by_reference;
zend_bool return_reference;
int required_num_args;
} zend_arg_info;
操作这个结构的宏:
#define ZEND_ARG_INFO(pass_by_ref, name) \
{ #name, sizeof(#name)-1, NULL, 0, 0, 0, pass_by_ref, 0, 0 },
#define ZEND_BEGIN_ARG_INFO(name, pass_rest_by_reference) \
ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, ZEND_RETURN_VALUE, -1)
#define ZEND_BEGIN_ARG_INFO_EX(name, pass_rest_by_reference, return_reference, required_num_args) \
static const zend_arg_info name[] = { \
{ NULL, 0, NULL, 0, 0, 0, pass_rest_by_reference, return_reference, required_num_args },
#define ZEND_END_ARG_INFO() };
随便找一段参数定义代码:
ZEND_BEGIN_ARG_INFO_EX(arginfo_dir_openFile, 0, 0, 0)
ZEND_ARG_INFO(0, open_mode)
ZEND_ARG_INFO(0, use_include_path)
ZEND_ARG_INFO(0, context)
ZEND_END_ARG_INFO();
那么这段代码,经过上面宏替换之后就变成了,注意,在宏里面#表示把后面的代码字符串化:
static const zend_arg_info arginfo_dir_openFile[]={
{NULL, 0, NULL, 0, 0, 0, 0, 0, 0),
{"open_mode", sizeof("openmode")-1, NULL, 0, 0, 0, 0, 0, 0},
{"use_include_path", sizeof("use_include_path")-1, NULL, 0, 0, 0, 0, 0, 0},
{"context", sizeof("context")-1, NULL, 0, 0, 0, 0, 0, 0}
};
好了,现在写段C代码来测试下吧。一下代码测试环境WIN7 64bit professional +VS2010 ultimate:
#include <iostream>
using namespace std;
//参数定义结构
#define ZEND_ARG_INFO(pass_by_ref, name) \
{ #name, sizeof(#name)-1, NULL, 0, 0, 0, pass_by_ref, 0, 0 },
//参数结构
typedef struct _zend_arg_info {
const char *name; //参数名
unsigned int name_len; //参数名长度
const char *class_name; //类名
unsigned int class_name_len; //类名长度
bool array_type_hint;
bool allow_null;
bool pass_by_reference;
bool return_reference;
int required_num_args;
} zend_arg_info;
int main()
{
static const zend_arg_info arginfo_dir_openFile[]={
{NULL, 0, NULL, 0, 0, 0, 0, 0, 0},
ZEND_ARG_INFO(0, open_mode)
ZEND_ARG_INFO(0, use_include_path)
ZEND_ARG_INFO(0, context)
};
_asm int 3; //断点
return 1;
}
在中断处截图看一下,发现:

看到了吧,字符串和字符串长度都有的。
遗忘的C语言细节:
1.#define XXXXX(a,) #a 即表示 把a表现为字符串。 XXXXXX(shyandsy) ==== "shyandsy"
2.sizeof()可以计算字符串长度。结果是字符个数+1,因为有\0结尾。
sizeof("shyandsy")-1 = 8