创建python文件pythonhello.py

# -*- encoding:utf-8 -*-
def hello():
    return "hello world!"

MFC中调用pythonhello.py:

#include "Python.h"


	Py_Initialize();
	PyObject *pModule = NULL;
	PyObject *pFunc = NULL;

    //指明python文件路径,否则PyImport_ImportModule返回值为空
	char *pyDir = "D:/";
	char tempPath[256] = {};
	PyRun_SimpleString("import sys");
	sprintf(tempPath, "sys.path.append('%s')", pyDir);
	PyRun_SimpleString(tempPath);
	
	pModule = PyImport_ImportModule("pythonhello");//调用的python文件名称
	pFunc = PyObject_GetAttrString(pModule, "hello");//调用函数名称
	PyObject *pArgs = NULL;
	PyObject* pRet = PyObject_CallObject(pFunc, pArgs);//调用函数
	char *res;//Y用于接收返回值(返回值类型为字符串,如果是整形则用int)
	PyArg_Parse(pRet, "s", &res);//转换返回类型
	printf("%s\n", res);
	Py_Finalize();