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

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

Python3.5.0(v3.5.0:374f501f4567,Sep132015,02:27:37)[MSCv.190064bit(AMD64)]onwin32

代码如下

importctypes
defget():
'''从剪切板中获得字符串'''
h=ctypes.WinDLL('user32.dll')
h.OpenClipboard(0)
aa=h.GetClipboardData(13)
ss=ctypes.c_wchar_p(aa)
h.CloseClipboard()
returnss.value
defset(mystr):
'''把字符串放到剪切板中,成功返回1,失败返回0'''
u=ctypes.WinDLL('user32.dll')
k=ctypes.WinDLL('kernel32.dll')
s=mystr.encode('utf-16')
s=s[2:]+b''
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()
ifrr==0:
return0
else:
return1
#-----
set("abcdefg")

程序返回

<class'ctypes.c_void_p'>

㈡ python ctypes 问题

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

C:\Users\s>python
Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel)] on win
32
Type "help", "right", "credits" or "license" for more information.
>>> from ctypes import *
>>> dir(CDLL)
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattr__',
'__getattribute__', '__getitem__', '__hash__', '__init__', '__mole__', '__new
__', '__rece__', '__rece_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中print type是什么意思

python中type() 函数返回对象的类来型源,print函数为打印结果,

验证如下,

1、WIN+R快捷键,打开运行窗口,准备进入python环境,

㈣ 如何通过python ctypes 在64位平台 取得模块handle 和函数地址

我用的是 64位的、
原因,我找到了:
GetMoleHandleA 和 GetProcAddress 在调用前需要 把 argtypes 和 restypes 类型进行定义就可以。原因可能是默认的是32位的返回值。

㈤ 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 type 有哪些

查看类型的,你可以输出一些代码试试,比如: import mathprint type(3)print type(3.4)print type(math)

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

test.c(动态库源代码)

[cpp] view plain
// 编译生成动态库: 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
#!/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
[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]$

㈧ Python使用Ctypes调用lib,怎么使用指针类型参数接收输出参数

本文演示了在python中调用C语言生成的动态库,返回结构体指针,并进行输出!

test.c(动态库源代码)
// 编译生成动态库: 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语言生成的动态库):
#!/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)

最后运行结果:
[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]$

㈨ python ctypes调用shell32打开外部程序2.7版可以3.4就不行

Python3.x字符串都是UNICODE的,所以应该用WIN32 API的UNICODE版本
windll.shell32.ShellExecuteW(None, 'open', r'"C:\WINDOWS\NOTEPAD.EXE"', '', None, 1)

㈩ python ctypes 的printf只能打印字符串长度

我的xp系统 看来好像是你环境有问题 希望对你有帮助