以下为实验步骤:
一、VS 2005:
1. 新建Win32 Application, Application type: DLL, Additional options: Empty project.
2. 在工程属性页 C/C++/附加包含目新增<Python>\include目录, 链接器/附加库目录添加<Python>\libs目录
3. 新增Source Files:
大功告成!
一、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")
大功告成!