您的位置:

关于python中ctypes的信息

本文目录一览:

python怎么导入ctypes

1. 加载Windows系统自带的dll文件:

#加载cdecl调用约定的dll

msvcrt =cdll.msvcrt

#加载stdcall调用约定的dll

kernel32 =windll.kernel32

2. 加载自己dll文件,假如为addFuncDll,方式如下:

mydll =CDLL("addFuncDll.dll")

或者 mydll = cdll.addFuncDll

如果其中有函数add,计算两个整数的和,则使用方式如下:

result=mydll.add(4,5)

可以多一步指明add函数的参数类型(也可不指明):

mydll.add.argtypes= [c_int,c_int]

3. 结构体在python中定义为Structure的子类如下:

class POINT(Structure):

_fields_ = [("x", c_int),

("y",c_int)]

_fields中每一项为元组(成员名称,类型)

结构体还可以用于其他的结构体:

class RECT(Structure):

_fields_ = [("upperleft",POINT),

("lowerright",POINT)]

python ctypes 问题

我在交互式环境下使用没问题,你查一下环境吧。另外,库名可以不加后缀名,因为linux下可能是so后缀的,加了也没关系。

C:\Users\spython

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win

32

Type "help", "copyright", "credits" or "license" for more information.

from ctypes import *

dir(CDLL)

['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattr__',

'__getattribute__', '__getitem__', '__hash__', '__init__', '__module__', '__new

__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__

str__', '__subclasshook__', '__weakref__', '_func_flags_', '_func_restype_']

dll=CDLL("msvcrt")

dll

CDLL 'msvcrt', handle 75b30000 at 2624570

dll=CDLL("msvcrt.dll")

dll

CDLL 'msvcrt.dll', handle 75b30000 at 2608ed0

python用ctypes操作剪切板遇到问题!!

这边执行没有问题,版本如下

Python 3.5.0 (v3.5.0:374f501f4567, Sep 13 2015, 02:27:37) [MSC v.1900 64 bit (AMD64)] on win32

代码如下

import ctypes

def get(): 

    '''从剪切板中获得字符串'''

    h=ctypes.WinDLL('user32.dll')

    h.OpenClipboard(0) 

    aa=h.GetClipboardData(13) 

    ss=ctypes.c_wchar_p(aa) 

    h.CloseClipboard()

    return ss.value

def set(mystr):

    '''把字符串放到剪切板中,成功返回1,失败返回0'''

    u=ctypes.WinDLL('user32.dll')

    k=ctypes.WinDLL('kernel32.dll')

    s=mystr.encode('utf-16')

    s=s[2:]+b'\0\0'

    ss=ctypes.c_char_p(s)

    u.OpenClipboard(0)

    u.EmptyClipboard()

    k.GlobalAlloc.argtypes=[ctypes.c_uint32,ctypes.c_uint32]

    try:

        cb=k.GlobalAlloc(0,len(s))

        cb=ctypes.c_void_p(cb)

        print(type(cb))

        ctypes.memmove(cb,ss,len(s))

        rr=u.SetClipboardData(13,cb) # 13-unicode

    finally:

        u.CloseClipboard()

    if rr==0:

        return 0

    else:

        return 1

#-----

set("abcdefg")

程序返回

class 'ctypes.c_void_p'

python ctypes 怎么处理函数返回的一般指针

test.c(动态库源代码)

[cpp] view plain copy

// 编译生成动态库: gcc -g -fPIC -shared -o libtest.so test.c

#include stdio.h

#include string.h

#include stdlib.h

typedef struct StructPointerTest

{

char name[20];

int age;

}StructPointerTest, *StructPointer;

StructPointer test() // 返回结构体指针

{

StructPointer p = (StructPointer)malloc(sizeof(StructPointerTest));

strcpy(p-name, "Joe");

p-age = 20;

return p;

}

编译:gcc -g -fPIC -shared -o libtest.so test.c

call.py(python调用C语言生成的动态库):

[python] view plain copy

#!/bin/env python

# coding=UTF-8

from ctypes import *

#python中结构体定义

class StructPointer(Structure):

_fields_ = [("name", c_char * 20), ("age", c_int)]

if __name__ == "__main__":

lib = cdll.LoadLibrary("./libtest.so")

lib.test.restype = POINTER(StructPointer)

p = lib.test()

print "%s: %d" %(p.contents.name, p.contents.age)

最后运行结果:

[plain] view plain copy

[zcm@c_py #112]$make clean

rm -f *.o libtest.so

[zcm@c_py #113]$make

gcc -g -fPIC -shared -o libtest.so test.c

[zcm@c_py #114]$./call.py

Joe: 20

[zcm@c_py #115]$