【转】VS 2005中实现对Python 2.5.2的模块扩展实验

来源:互联网 发布:数据库 migration 编辑:程序博客网 时间:2024/06/11 04:37
以下为实验步骤:
一、VS 2005:
1. 新建Win32 Application, Application type: DLL, Additional options: Empty project.
2. 在工程属性页 C/C++/附加包含目新增<Python>\include目录, 链接器/附加库目录添加<Python>\libs目录
3. 新增Source Files:

hello.c:

#include "stdafx.h"#include <Python.h>#include <string.h>#define PYC_API _declspec(dllexport)static PyObject *message(PyObject *self, PyObject *args){char *szFromPy;char szResult[100];ZeroMemory(szResult, sizeof(szResult));if (!PyArg_Parse(args, "(s)", &szFromPy)){return NULL;}else {sprintf(szResult, "Hello %s", szFromPy);MessageBoxA(NULL, szResult, "Python Call C", MB_OK);return Py_BuildValue("s", szResult);}}static struct PyMethodDef hello_methods[] = {{"message", message, 1},{NULL, NULL}};#ifdef __cplusplusextern "C" {#endifPYC_API void inithello() {(void)Py_InitModule("hello", hello_methods);}#ifdef __cplusplus}#endif


工程地址

4. 工程属性页/链接器/输出文件,修改输出文件名为hello.pyd
5. 以Release方式Build。


二、Python:
1. copy刚才生成的hello.pyd到项目目录下,或者可以import的lib目录。
2. 新建main.py:

import helloprint hello.message("hhahhah")

大功告成!

0 0
原创粉丝点击