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

以下为实验步骤:
一、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 __cplusplus
extern "C" {
#endif

PYC_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 hello

print hello.message("hhahhah")

大功告成!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值