11.添加TestMyPython.py如下,在_MyPython.pyd的文件夹目录下运行TestMyPython.py.
import MyPython import os o = MyPython.CMyDll() print(o.SayHello("World")) class MyPyDll(MyPython.CMyDll): def __init__(self): MyPython.CMyDll.__init__(self) def location(self): return "Python" o1 = MyPyDll(); print(o1.SayHello("World")) os.system("pause") |
运行结果如下:
Hello World. I'm from C++.
Hello World. I'm from Python.
Press any key to continue . . .
12.Run python in MyApp。
修改MyApp的工程属性,添加python头文件的文件夹,和lib文件的文件夹。
修改MyApp.cpp的代码如下:
#include "stdafx.h" #include <Python.h> int _tmain(int argc, _TCHAR* argv[]) { Py_Initialize(); PyObject * pModule = PyImport_ImportModule("TestMyPython"); Py_Finalize(); return 0; } |
编译运行MyApp.exe,结果如下:
Hello World. I'm from C++.
Hello World. I'm from Python.
Press any key to continue . . .