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. redis2.8--c/s架构流程

  2. Android EditText email、数字验证

    在做Android注册登录模块的时候,经常需要在客户端就验证用户输入的信息的正确性,如填写邮箱需要验证是否是邮箱,填写手机.年龄等信息需要验证是否是数字.先介绍一下验证邮箱的代码: /** * met ...

  3. SharePoint 2013 开发——开发并部署第一个APP

    博客地址:http://blog.csdn.net/FoxDave 本篇我们开始对开发APP应用程序进行了解. 本篇基于本地SharePoint环境(如果是Office 365的话会方便许多),需 ...

  4. goldengate复制过程字符集处理一例

    源端是oracle, al32utf8,表里有乱码,目标端是sybase cp936,两端的DB都不能改字符集,而且源端是目标端的超集,当复制有乱码的数据(非中文或英文数字等),目标端replicat ...

  5. (转)html5开发之viewport使用

    原文:http://www.php100.com/html/webkaifa/HTML5/2012/0831/10979.html 随着高端手机(Andriod,Iphone,Ipod,WinPhon ...

  6. golang函数调用计时

    package main import ( "log" "time" ) func f() { defer timeoutCheck("f slow& ...

  7. Configuration for Python to run on Android

    It's exiting to learn to make app on Android with python. But when I check <Head First Python> ...

  8. Linux中的汇编简介

    GNU as汇编语法 GNU汇编语法使用的是AT&T汇编它和Intel汇编的语法主要有以下一些不同: AT&T汇编中的立即操作数前面要加上'$',寄存器操作数名前要加上百分号'%',绝 ...

  9. 【LeetCode OJ】Populating Next Right Pointers in Each Node

    Problem Link: http://oj.leetcode.com/problems/populating-next-right-pointers-in-each-node/ Just trav ...

  10. 解决Xcode7.1插件安装的办法

    现象一. 运行安装后,没有出现在菜单上. 1. 到githup上下载Alcatraz project https://github.com/supermarin/Alcatraz2. 打开终端 3. ...