ctypes赋予了python类似于C语言一样的底层操作能力,通过ctypes模块可以调用动态链接库中的导出函数、构建复杂的c数据类型。

  ctypes提供了三种不同的动态链接库加载方式:cdll(),windll(),oledll()。

  HelloWorld.py:

 import ctypes   #导入ctypes模块    

 NULL = 0
m_string = "Hello World!!!"
m_title = "Ctype Dlg" user32 = ctypes.cdll.user32 #加载user32.dll
user32.MessageBoxW(NULL,m_string,m_title,NULL) #调用user32中的MessageBoxW函数

构建C语言数据类型:

 ctypes基本数据类型映射表

参数类型预先设定好,或者在调用函数时再把参数转成相应的c_***类型。ctypes的类型对应如下:

ctypes type C type Python Type
c_char char 1-character string
c_wchar wchar_t 1-character unicode string
c_byte char int/long
c_ubyte unsigned char int/long
c_bool bool bool
c_short short int/long
c_ushort unsigned short int/long
c_int int int/long
c_uint unsigned int int/long
c_long long int/long
c_ulong unsigned long int/long
c_longlong __int64 or longlong int/long
c_ulonglong unsigned __int64 or unsigned long long int/long
c_float float float
c_double double float
c_longdouble long double float float
c_char_p char * string or None
c_wchar_p wchar_t * unicode or None
c_void_p void * int/long or None
     

对应的指针类型是在后面加上"_p",如int*是c_int_p等等。在python中要实现c语言中的结构,需要用到类。

构建C结构体:

  

 //c语言结构体

 struct test
{
int num1;
int num2;
}; //python ctypes 结构体
from ctypes import *
class test(Structure):
_fields_ = [
("num1",c_int),
("num2",c_int),
]

ctypes 模块的更多相关文章

  1. ctypes模块与pywin32模块

    ctypes模块: 主要用于调用c动态链接库. 1.聊聊Python ctypes模块 2.ctypes模块管理 相关网址: pywin32模块: 用于访问win32API函数(win32api模块) ...

  2. 通过实例简介python使用ctypes模块调用C语言动态库

    看介绍python语言时,说它是胶水语言,可以调用其他语言.通过使用ctypes模块就可以调用C语言的动态库.下面先放上官方文档和几个比较好的博文. 1.官方文档:http://python.net/ ...

  3. platform模块和ctypes模块

    一.ctypes模块 Python 的 ctypes 要使用 C 函数,需要先将 C 编译成动态链接库的形式,即 Windows 下的 .dll 文件,或者 Linux 下的 .so 文件.先来看一下 ...

  4. 聊聊Python ctypes 模块(转载)

    https://zhuanlan.zhihu.com/p/20152309?columnSlug=python-dev 作者:Jerry Jho链接:https://zhuanlan.zhihu.co ...

  5. python使用ctypes模块下的windll.LoadLibrary报OSError: [WinError 193] % 不是有效的 Win32 应用程序

    原因:python是64位的python,而windll.LoadLibrary只能由32位的python使用 参考: 64位Python调用32位DLL方法(一) 解决方法:使用32位的python ...

  6. Python使用ctypes访问C代码

    工具:CodeBlocks 新建一个分享库工程( Shared library ),随便编写一个C代码的函数 // test.c #include <stdio.h> int fib(in ...

  7. CTypes

    参考:http://docs.pythontab.com/interpy/c_extensions/ctypes/ Python中的ctypes模块可能是Python调用C方法中最简单的一种.ctyp ...

  8. python中使用 C 类型的数组以及ctypes 的用法

    Python 在 ctypes 中为我们提供了类似C语言的数据类型, 它的用途(我理解的)可能是: (1) 与 其他语言(如 C.Delphi 等)写的动态连接库DLL 进行交换数据,因为 pytho ...

  9. 使用 ctypes 进行 Python 和 C 的混合编程

    Python 和 C 的混合编程工具有很多,这里介绍 Python 标准库自带的 ctypes 模块的使用方法. 初识 Python 的 ctypes 要使用 C 函数,需要先将 C 编译成动态链接库 ...

随机推荐

  1. Service相关--读书笔记

    2013-12-30 18:16:11 1. Service和Activty都是从Context里面派生出来的,因此都可以直接调用getResource(),getContentResolver()等 ...

  2. Windows下为64位的python3.4.3安装numpy

    貌似现在没有python3.x的numpy 64位.exe安装包只有.whl的(也可能是我没找到)只能在终端下安装 1.到官网https://www.python.org/downloads/下载py ...

  3. POJ 1845 求a^b的约数和

    题目大意就是给定a和b,求a^b的约数和 f(n) = sigma(d) [d|n] 这个学过莫比乌斯反演之后很容易看出这是一个积性函数 那么f(a*b) = f(a)*f(b)  (gcd(a,b) ...

  4. fwrite错误

    使用fwrite出错 f:\dd\vctools\crt_bld\self_x86\crt\srt\write.cline:69expression:_osfile(fh)&FOPEN 使用w ...

  5. Ch2.Making Reconmmendation in PCI

    做<Programing Collective Intelligence>中chapter 2.Making Recommendation的实例,有3个问题花了好长时间: 1. 遇到报错& ...

  6. IOS创建目录接口createDirectoryAtPath:withIntermediateDirectories:中参数attributes的设置

    在应用程序执行时,经常需要本地化保存一些重要的数据,这时就有可能需要创建一些目录.Objective-C提供了一个非常强大的创建目录的接口: - (BOOL)createDirectoryAtPath ...

  7. UIkit框架介绍

    UIKit Framework The UIKit framework (UIKit.framework) provides crucial infrastructure for implementi ...

  8. Python的图形化界面

    导入easygui模块有很多种方法 , 这里只介绍一种简单使用的 . import easygui as g 将easygui 简称为g 然后开始调用她的函数就行. import easygui as ...

  9. ERP联系人查询和修改(十六)

    查看和修改是同一个界面: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="L ...

  10. Oracle存储过程基本语法

    一.形式 1 CREATE OR REPLACE PROCEDURE 存储过程名  //是一个SQL语句通知Oracle数据库去创建一个叫做skeleton存储过程, 如果存在就覆盖它; 2 IS   ...