设置 C++ 包含目录
设置 链接器 附加库目录
.h
#include <python.h>
#pragma comment(lib, "python34.lib")
// #pragma comment(lib, "python3.lib");
// #pragma comment(lib, "select.lib");
// #pragma comment(lib, "unicodedata.lib");
// #pragma comment(lib, "winsound.lib");
class KPyProg
{
public:
KPyProg(void);
~KPyProg(void);
void run();
};
.cpp
#include "StdAfx.h"
#include "KPyProg.h"
KPyProg::KPyProg(void)
{
run();
}
KPyProg::~KPyProg(void)
{
}
void KPyProg::run()
{
Py_Initialize();
if (Py_IsInitialized())
{
AfxMessageBox(_T("python初始化成功"));
PyRun_SimpleString("import sys");
PyRun_SimpleString("print('---import sys---')");
PyRun_SimpleString("sys.path.append('E:\\project\\sc1\\cprog\\jt1\\py')");
//PyRun_SimpleString("");
//获取模块
PyObject* pModule = PyImport_ImportModule("curveFit");
if (!pModule) {
printf("Cant open python file!/n");
return;
}
PyObject* pFunc = PyObject_GetAttrString(pModule, "cacu");
PyObject *pReturn = NULL;
PyObject* pArgs = NULL;
pReturn = PyEval_CallObject(pFunc, pArgs);
//使用方法
unsigned long val = PyLong_AsUnsignedLong(pReturn);
Py_DECREF(pFunc);
//Py_DECREF(pArgs);
Py_DECREF(pModule);
Py_Finalize();
}
}