其实大致的步骤和我在前面写的《使用Visual Studio.Net 2005开发Python扩展模块》 这里重写VC6.0环境下的方法是为了方便自己也为了熟悉VC6.0环境的朋友
少走弯路。
1、新建工程,Win32 Dynamic-Link Library,工程名:Winy(个人喜好)。
2、添加代码。清除Windy.cpp,拷贝代码如下:
//
Windy.cpp : Defines the entry point for the DLL application.
//
#include " stdafx.h "
#include < Python.h >
static PyObject *
Windy_hello(PyObject * self, PyObject * args)
... {
printf("Hello, world ");
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef Windy_methods[] =
... {
...{"hello", Windy_hello, METH_VARARGS, "hello() doc string"},
...{NULL, NULL}
} ;
/**/ /*PyMODINIT_FUNC*/
extern " C " _declspec(dllexport)
initWindy( void )
... {
Py_InitModule("Windy", Windy_methods);
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
... {
return TRUE;
}
//
#include " stdafx.h "
#include < Python.h >
static PyObject *
Windy_hello(PyObject * self, PyObject * args)
... {
printf("Hello, world ");
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef Windy_methods[] =
... {
...{"hello", Windy_hello, METH_VARARGS, "hello() doc string"},
...{NULL, NULL}
} ;
/**/ /*PyMODINIT_FUNC*/
extern " C " _declspec(dllexport)
initWindy( void )
... {
Py_InitModule("Windy", Windy_methods);
}
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
... {
return TRUE;
}
3、添加Python安装目录中的include,libs路径。Tools > Options > Directories
4、更改工程编译设置(Alt + F7), 添加python25.lib导入库(也可不设置,前面已经加入了Python的libs了)。
5、编译。如果你没有Python的调试库如python25_d.lib,请编译Release版本。
6、测试。请参考《使用Visual Studio.Net 2005开发Python扩展模块》。
7、发挥我们的创造力的时候到了 :)
ps1: 注意在
/**/
/*PyMODINIT_FUNC*/
extern " C " _declspec(dllexport) void
initWindy( void )
extern " C " _declspec(dllexport) void
initWindy( void )
中如果你使用PyMODINIT_FUNC而注释掉extern "C" _declspec(dllexport) void 请将最后生成的dll后缀改为pyd,
具体原因目前我还不是太清楚,等有时间看看源代码是什么原因(是不是我哪儿的设置有问题?)。
ps2: vckbase上的一篇《C++ 扩展和嵌入 Python》