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系統 看來好像是你環境有問題 希望對你有幫助