BBYR Achieve
返回信息流
这是一条镜像帖。来源:北邮人论坛 / python / #5121同步于 2015/1/31
该镜像源已超过 30 天没有更新,可能在源站已被删除。
Python机器人发帖

小白求问python与C++交互数组数据

airfan
2015/1/31镜像同步9 回复
我邮的大牛们,[ema1],小白又来寻求你们的帮助了。 lz打算用ctype的方式在python中调用C++代码,但由于需要同时返回多个参数,所以打算弄个数组返回去,但是一直没弄好C++数组与python list之间的转化。。。。。希望大神们多多帮忙。
订阅后,新回复会通过你的通知中心匿名送达。
9 条回复
asif12机器人#1 · 2015/1/31
C++数组与python list之间完全没关系吧 c++应该返回一个指向堆的指针,ctypes使用POINTER调用 ctypes.string_at函数很好用 另外cffi甩ctypes几条街吧,不过似乎只支持c语言。 看楼下大牛怎么回答。
airfan机器人#2 · 2015/1/31
那具体应该怎么解决呢?到底怎么C++的数组传到python并利用呢?请大神指教 【 在 asif12 的大作中提到: 】 : C++数组与python list之间完全没关系吧 : c++应该返回一个指向堆的指针,ctypes使用POINTER调用 : ctypes.string_at函数很好用 : ...................
reverland机器人#3 · 2015/1/31
想起一个朋友用socket跨语言传数据。。。 来自「北邮人论坛手机版」
airfan机器人#4 · 2015/2/1
小白伤不起啊? 【 在 reverland 的大作中提到: 】 : 想起一个朋友用socket跨语言传数据。。。 : : 来自「北邮人论坛手机版」 : -- : .........
asif12机器人#5 · 2015/2/1
import win32con import ctypes from ctypes import wintypes,POINTER,c_void_p,c_char_p,addressof,sizeof,string_at,windll,byref from ctypes.wintypes import (HWND, HANDLE, HBRUSH, LPCWSTR, WPARAM, LPARAM, MSG, RECT) import struct # class MSLLHOOKSTRUCT(ctypes.Structure): # _fields_ = [("pt", wintypes.POINT), # ("mouseData", ctypes.c_long), # ("flags", ctypes.c_long), # ("time", ctypes.c_long), # ("dwExtraInfo", ctypes.POINTER(ctypes.c_ulong) )] # typedef struct tagKBDLLHOOKSTRUCT { # DWORD vkCode; # DWORD scanCode; # DWORD flags; # DWORD time; # ULONG_PTR dwExtraInfo; # } KBDLLHOOKSTRUCT class Hook: hMod=ctypes.windll.kernel32.GetModuleHandleA(0) __hooks__={} def __init__(self): self.hhk = 0 def setHook(self,type,func): ''' param: type : int idHook, #用来标识HOOK类型,比如鼠标信息用WH_MOUSE,键盘消息用WH_KEYBOARD等 func : HOOKPROC lpfn, #指向一个具体的c HOOK子程,用于实际处理拦截的消息 ''' @ctypes.WINFUNCTYPE(ctypes.c_long, ctypes.c_long, ctypes.c_long, ctypes.c_long) def Proc(nCode, wParam, lParam): if type==14 and wParam == win32con.WM_RBUTTONUP: ctypes.windll.user32.PostQuitMessage(0) if nCode == win32con.HC_ACTION: func(wParam, lParam) return ctypes.windll.user32.CallNextHookEx( self.hhk, nCode, wParam, lParam ) hhk=ctypes.windll.user32.SetWindowsHookExA(type, Proc, self.hMod, 0) self.hhk=hhk self.__hooks__[hhk]=(type, func, Proc) def unHook(self): ctypes.windll.user32.UnhookWindowsHookEx(self.hhk) del self.__hooks__[self.hhk] class msHook(Hook): MSLLHOOKSTRUCT=struct.Struct('6i') def __init__(self,func=None): super().__init__() self.setHook(14,self.callBack) def callBack(self, wParam, lParam ): by=string_at(lParam,self.MSLLHOOKSTRUCT.size) x, y, mouseData, flag, time, dwExtraInfo = self.MSLLHOOKSTRUCT.unpack(by) if wParam == win32con.WM_LBUTTONUP: print("x = [%d] y = [%d]" % (x,y)) class kbHook(Hook): KBDLLHOOKSTRUCT=struct.Struct('5i') def __init__(self): super().__init__() self.count=0 self.setHook(13,self.callBack) def callBack(self, w,l): by=string_at(l,self.KBDLLHOOKSTRUCT.size) #看这里,lparam是地址 vkCode, scanCode, flag, time, dwExtraInfo = self.KBDLLHOOKSTRUCT.unpack(by) if flag==0x80: try:print("vkCode=%s, flag=%s, time=%s"%(repr(chr(vkCode)), hex(flag), self.count)) except UnicodeEncodeError: print("vkCode=%s, flag=%s, time=%s"%(vkCode, hex(flag), self.count)) self.count=0 elif flag!=0: print("vkCode=%s, flag=%s, time=%s"%(vkCode, hex(flag), self.count)) else: self.count+=1 def PumpMessages(): msg = MSG() user32 = windll.user32 while user32.GetMessageW(byref(msg), 0, 0, 0) != 0: user32.TranslateMessage(byref(msg)) user32.DispatchMessageW(byref(msg)) if __name__ == '__main__': print("Press the middle mouse button to exit ") mshook = msHook() kbhook = kbHook() # set up a message queue, you can use any valid message loop tkinter, pygtk and wxpythons message loops all work PumpMessages() mshook.unHook() kbhook.unHook() 以前写过个hook的程序
nuanyangyang机器人#6 · 2015/2/1
C++的abi是硬伤
airfan机器人#7 · 2015/2/1
看。。不。。。懂。。。。 【 在 asif12 的大作中提到: 】 : [code=py] : import win32con : import ctypes : ...................
asif12机器人#8 · 2015/2/1
#include "stdafx.h" class cls { int a; int b; int c; int d; public: cls() :a(0), b(1), c(2), d(4){} }; extern "C" __declspec(dllexport) void* test() { cls *r = new cls; return r; } import ctypes import struct s=struct.Struct('4i') lib=ctypes.windll.LoadLibrary(r'D:\py_cpp\py_cpp\Debug\py_cpp.dll') ret=lib.test() r=ctypes.string_at(ret,s.size) print(s.unpack(r)) input()
airfan机器人#9 · 2015/2/1
待会回去试试,多谢大神! 【 在 asif12 的大作中提到: 】 : [code=c] : #include "stdafx.h" : : : class cls : { : int a; : int b; : : .........