1.新建fun.c文件和fun.h文件

#include <stdio.h>
#include <stdlib.h>
#include <string.h> int fac(int n)
{
if(n < 2)
return 1;
return n * fac(n-1);
} char *reverse(char *s)
{
char t, *p = s,*q=(s+(strlen(s)-1)); while(s && (p<q)){
t = *p;
*p++ = *q;
*q-- = t;
}
return s;
} int test(void)
// int main(void)
{
char s[1024];
printf("4!==%d\n",fac(4));
printf("8!==%d\n",fac(8)); strcpy(s,"arthas");
printf("reversing 'arthas',we get '%s'\n",reverse(s));
return 0;
}
#ifndef FUN_H_
#define FUN_H_ int fac(int n);
char *reverse(char *s);
int test(void); #endif

2.新建funwrapper.c文件

#include "Python.h"
#include <stdlib.h>
#include <string.h>
#include "fun.h" static PyObject *fun_fac(PyObject *self,PyObject *args)
{
int num;
if(!PyArg_ParseTuple(args,"i",&num))
return NULL;
return (PyObject*)Py_BuildValue("i",fac(num));
} static PyObject *fun_reverse(PyObject *self,PyObject *args)
{
char *src;
char *mstr;
PyObject *retval;
if(!PyArg_ParseTuple(args,"s",&src))
return NULL;
mstr = malloc(strlen(src)+1);
strcpy(mstr,src);
reverse(mstr);
retval = (PyObject*)Py_BuildValue("ss",src,mstr);
free(mstr);
return retval;
} static PyObject *fun_test(PyObject *self,PyObject *args)
{
return (PyObject*)Py_BuildValue("i",test());
} static PyMethodDef funMethods[] = {
{"fac",fun_fac,METH_VARARGS},
{"reverse",fun_reverse,METH_VARARGS},
{"test",fun_test,METH_VARARGS},
{NULL,NULL},
}; void initfun(void)
{
Py_InitModule("fun",funMethods);
}

3.CMD使用指令

setup.py install build

这里我出错了python Unable to find vcvarsall.bat 错误,

参考上一篇日志python Unable to find vcvarsall.bat 错误解决方法。解决了。

然后生成出fun.pyd文件

4.复制到python的DLLS文件夹下

5.可以用python调用了

import fun

print fun.fac(4)
print fun.reverse("arthas")
fun.test()

6.输出的结果

···

24

('arthas', 'sahtra')

4!24

8!40320

reversing 'arthas',we get 'sahtra'

···

windows下python调用c文件流程的更多相关文章

  1. linux下python调用.so文件

    前言 使用python 调用Fanuc的动态链路库.so 文件读取数据 环境要求 环境 需求 ubuntu16.04 32位 python3.5 32位 配置 把so文件添加到默认路径 ln -s / ...

  2. windows下python检查文件是否被其它文件打开

    windows下python检查文件是否被其它文件打开.md 有时候我们需要能够判断一个文件是否正在被其它文件访问,几乎不可避免的要调用操作系统接口 from ctypes import cdll i ...

  3. Python Windows下打包成exe文件

    Python Windows 下打包成exe文件,使用PyInstaller 软件环境: 1.OS:Win10 64 位 2.Python 3.7 3.安装PyInstaller 先检查是否已安装Py ...

  4. windows下python web开发环境的搭建

    windows下python web开发环境: python2.7,django1.5.1,eclipse4.3.2,pydev3.4.1 一. python环境安装 https://www.pyth ...

  5. [转]Windows下Python多版本共存

    https://blog.csdn.net/dream_an/article/details/51248736 Windows下Python多版本共存 Python数据科学安装Numby,pandas ...

  6. Windows下Python多版本共存

    Windows下Python多版本共存 Python数据科学安装Numby,pandas,scipy,matpotlib等(IPython安装pandas) 0.0 因为公司项目,需要Python两个 ...

  7. Windows下python的配置

    Windows下python的配置 希望这是最后一次写关于python的配置博客了,已经被python的安装烦的不行了.一开始我希望安装python.手动配置pip并使用pip安装numpy,然而发现 ...

  8. Windows下Python读取GRIB数据

    之前写了一篇<基于Python的GRIB数据可视化>的文章,好多博友在评论里问我Windows系统下如何读取GRIB数据,在这里我做一下说明. 一.在Windows下Python为什么无法 ...

  9. Windows下Python安装numpy+mkl,Scipy和statsmodels

    最近做时间序列分析需要用到Python中的statsmodels,但是安装过程中遇到很头疼的问题,Google.Stackover各种都没有找到合适的解决办法,而且貌似还有很多同学也在吐槽Window ...

随机推荐

  1. atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerException  v2 q31

    atitit.解决struts2 SpringObjectFactory.getClassInstance NullPointerExceptionv2 q31 1. #--现象 java.lang. ...

  2. Atitit.atijson 类库的新特性设计与实现 v3 q31

    Atitit.atijson 类库的新特性设计与实现 v3 q31 1. V1版本---集成了多引擎1 2. V2版本新特性 --bsh脚本化2 3. V3版本新特性---循环引用解决使用fastjs ...

  3. Java平台调用.net开发的WebService报错处理

    1.报错:服务器未能识别 HTTP 头 SOAPAction 的值 : 解决办法:.net 开发的WebService文件中(.asmx)增加属性: [SoapDocumentService(Rout ...

  4. Vue 组件3 作用域插槽

    作用域插槽是一种特殊类型的插槽,用作使用一个(能够传递数据到)可重用模板替换已渲染元素. 在子组件中,只需将数据传递到插槽,就像你将props传递给组件一样: <div class=" ...

  5. 使用nmap 验证多种漏洞

    0x00 前言 大家都知道在平时的漏扫中,AWVS.APPSCAN.Netspark.webspectort等漏扫工具扫描出来的漏洞问题往往存在误报,这时我们就需要进行人工手动验证漏洞,这里我们有两种 ...

  6. Redis入门经典——The Little Redis Book (翻译)

    The Little Redis Book By Karl Seguin 关于本书:本书全然免费下载.你能够任意转载,复制.但请你注明作者.Karl Seguin.译者,WY. 以及不要用于商业用途. ...

  7. ptmalloc、tcmalloc和jemalloc

    内存优化总结:ptmalloc.tcmalloc和jemalloc 转载 2017年09月05日 18:57:12 3674 转载于:http://www.cnhalo.net/2016/06/13/ ...

  8. c 编译异常 switch 之a label can only be part of a statement and a declaration is not a statement

    client.c:996: error: a label can only be part of a statement and a declaration is not a statement sw ...

  9. 新MBP使用git命令时启用xcode的终端log

    Last login: Mon Oct 22 12:41:33 on consoleuser:~ me$ git Agreeing to the Xcode/iOS license requires ...

  10. html 模版

    使用后台开发语言的都很了解语言的动态性给开发带来的好处,PHP,aspx,jsp页面都可以直接使用相应的语法和变量,输出的事就交给解释器或编译器了,用起来方便快捷,但需要额外的解释工作: 例如php模 ...