Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26309563
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Python/Ruby

2010-06-19 10:22:39

python调用C程序

时间:2010-6-19

1.     编写C语言程序生成dllso

#include

BOOL APIENTRY

DllMain(HANDLE hModule, DWORD dwReason, LPVOID lpReserved)

{

    return TRUE;

}

 

__declspec(dllexport) int

multiply(int num1, int num2)

{

    return num1 * num2;

}

保存为hello.c

切换到此目录下面在WIN平台使用:E:\webapp\myc\MYC>cl -LD hello.c -hello.dll

使用命令行编译生成DLL文件。

2.     Python调用

>>> from ctypes import *

>>> import os

>>> libtest = cdll.LoadLibrary(os.getcwd()+'/hello.dll')

>>> print libtest.multiply(2,2)

4

 

 

阅读(673) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~