python/C API接口

include

bool ParseEmail(char FilePath,char *sender){
PyObject * g_pModule = NULL;
PyObject
pArg = NULL;
PyObject* pRetVal = NULL;
int iRetVal = 0;
Py_Initialize();
if (!Py_IsInitialized())
{
printf("Error:Init Mode Error\n");
return FALSE;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("sys.path.append('./ParseEmail')");
//这里是要调用的文件名,我们这里是当前目录下
//导入模块
g_pModule =PyImport_ImportModule("ParseMail");
if (!g_pModule) {
printf("Error:Import Mode Failed!\n");
return FALSE;
}
//查找函数
PyObject * Email = PyObject_GetAttrString(g_pModule,"ParseSent");
if((Email) == NULL){
printf("Error:Retrieve an attribute named Failed\n");
}
//这里是要调用的函数名
//参数
pArg=PyTuple_New(2);
PyTuple_SetItem(pArg,0,Py_BuildValue("s",FilePath));
PyTuple_SetItem(pArg,1,Py_BuildValue("s",sender));
PyObject *objResult = PyObject_CallObject(Email,pArg);
//调用函数
if (!objResult){
printf("Error:Invoke Function Fail\n");
return FALSE;
}
Py_Finalize();//调用Py_Finalize,这个跟Py_Initialize相对应的.
return TRUE;
}

你可能感兴趣的:(python-c)