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. ModelSim 使用笔记1

    ModelSim 使用笔记1   ModelSim提供了简单仿真方式,还有一种要建立project,目前这种方式暂时够我用了. 总结了以下,做了一个简单的<modelsim quick star ...

  2. ZooKeeper_基础知识学习

    ZooKeeper是Hadoop的开源子项目(Google Chubby的开源实现),它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.命名服务.分布式同步.组服务等. Zookee ...

  3. Android JNI和NDK学习(06)--JNI的数据类型(转)

    本文转自:http://www.cnblogs.com/skywang12345/archive/2013/05/23/3094037.html 本文介绍JNI的数据类型.NDK中关于JNI数据类型的 ...

  4. 21. Merge Two Sorted Lists【easy】

    21. Merge Two Sorted Lists[easy] Merge two sorted linked lists and return it as a new list. The new ...

  5. 基于windows的resin配置

    Resin 与 Eclipse for JavaEE 的整合方法: 1.新建一个项目,将web application配置到resin.conf中 附上resin_struts2-111.conf文件 ...

  6. 第二百零三节,jQuery EasyUI,Window(窗口)组件

    jQuery EasyUI,Window(窗口)组件 学习要点: 1.加载方式 2.属性列表 3.事件列表 4.方法列表 本节课重点了解 EasyUI 中 Window(窗口)组件的使用方法,这个组件 ...

  7. execute,executeQuery和executeUpdate的区别

    在jdbc中有3种执行sql的语句分别是execute,executeQuery和executeUpdate execute执行增删改查操作 execute返回的结果是个boolean型,当返回的是t ...

  8. hadoop2.2集群部署教程连接

    完全分布式部署:http://blog.csdn.net/licongcong_0224/article/details/12972889 伪分布式部署:http://www.kankanews.co ...

  9. 电脑出现“损坏的图像”窗口提示dll没有被指定在Windows上运行如何解决

    电脑中出现了无法运行应用程序的情况,弹出一个“***.exe - 损坏的图像”的窗口,上面提示“***.dll没有被指定在Windows上运行……”,如果我们遇到这样的问题,应该要如何解决呢? 1.我 ...

  10. json responseJson

    private void doResoponseJson(HttpServletResponse resp,String jsonString){ Trace.logError(Trace.COMPO ...