Path: csiph.com!fu-berlin.de!uni-berlin.de!not-for-mail From: eryk sun Newsgroups: comp.lang.python Subject: Re: Problem on ctypes arguments in a DLL function Date: Fri, 18 Dec 2015 04:24:51 -0600 Lines: 43 Message-ID: References: <2331461b-cf4c-479c-8380-ddea3b7e7878@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-Trace: news.uni-berlin.de 9KjrNFDrkn3zMMS1lOZN2QD361ExD7ntdoopoZivoAlQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python,': 0.02; 'url:sourceforge': 0.03; 'debug': 0.04; 'caller': 0.07; 'constructor': 0.07; 'convention.': 0.07; 'valueerror:': 0.07; 'x86': 0.07; '[1]:': 0.09; 'subject:ctypes': 0.09; 'example:': 0.10; 'stack': 0.13; '.dll': 0.16; '[1].': 0.16; 'callee': 0.16; 'cdecl': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'stdcall': 0.16; 'subject:Problem': 0.16; 'wrote:': 0.16; 'memory': 0.17; 'bytes': 0.18; 'windows': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'extension': 0.20; '64-bit': 0.22; 'arguments': 0.22; 'ctypes': 0.22; 'struct': 0.22; 'pass': 0.22; 'cc:no real name:2**0': 0.22; 'am,': 0.23; 'dec': 0.23; 'header:In-Reply- To:1': 0.24; 'header': 0.24; 'skip:_ 20': 0.26; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'dll': 0.27; '32-bit': 0.29; 'declared': 0.29; 'path,': 0.29; 'convention': 0.30; 'mention': 0.30; 'waste': 0.30; 'probably': 0.31; "can't": 0.32; 'skip:_ 10': 0.32; 'class': 0.33; 'url:code': 0.34; 'file': 0.34; 'add': 0.34; 'received:google.com': 0.35; 'skip:* 20': 0.35; 'but': 0.36; 'too': 0.36; 'should': 0.36; 'received:209.85': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'there,': 0.37; 'received:209.85.213': 0.37; 'received:209': 0.38; 'wrong': 0.38; 'skip:p 20': 0.38; 'data': 0.39; 'to:addr:python.org': 0.40; 'called': 0.40; "you'll": 0.61; 'skip:u 10': 0.61; 'default': 0.61; 'you.': 0.64; "they're": 0.66; 'attention': 0.76; 'safety.': 0.84; 'stronger': 0.84; 'same,': 0.91 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc:content-type; bh=sP4z6xzwPSxmLutY2kURCkGBD/3E4ZLeJ5UoAt5b1rY=; b=JfNDkh/LBQek1wyZTMAn6GXV+GtefjskI/dwD5THyfNBJFMnAYLwXYHAIUN3yGpzSH ARzz7NIAsVW1UZEyX5o13MPmrLAsyKawnf3a+zMGtd5OZsbYz1BoAZmRTqJSE/rxrQqu eisvJV/mk00tF69m7wi0i2XcazjiFnmWBvtJ9D2K5jVcqIEMmDhpRl2ayZGCRpkuyadL masHtTh76WBei+nWH0b5x/jlIaVM0vKVZdGjuJXGcrNWFQvLck4wQBM/iIDY0xFLzlLG rgeoatX4w+pqh6CRqPZ2498UwZ97w4ysUXxxODoNbldQSWAt5hDT1GVS97q+6QLVrCN+ d/6g== X-Received: by 10.50.164.131 with SMTP id yq3mr1994185igb.74.1450434331456; Fri, 18 Dec 2015 02:25:31 -0800 (PST) In-Reply-To: <2331461b-cf4c-479c-8380-ddea3b7e7878@googlegroups.com> X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:100595 On Fri, Dec 18, 2015 at 2:41 AM, wrote: > ValueError: Procedure probably called with too many arguments (4 bytes in excess The function's calling convention is x86 cdecl (CDLL, caller stack cleanup), but you're using the x86 stdcall convention (WinDLL, callee stack cleanup). For a 64-bit process they're actually the same, but you're using 32-bit Python, so you have to pay attention to the convention. > _lib = windll.LoadLibrary("C:\\Windows\\System32\\libusb0.dll") It should simply be _lib = CDLL('libusb0') windll/WinDLL is the wrong calling convention. Everything else is just a waste of keystrokes. windll.LoadLibrary is an inferior way to call WinDLL, since it can't pass constructor arguments such as use_errno or use_last_error. The System32 directory is on the DLL search path, and Windows will add the .dll extension for you. The calling convention is declared in the header file lusb0_usb.h [1]. For example: struct usb_bus *usb_get_busses(void); Notice there's no mention of __stdcall there, so it's using the default cdecl convention. > _usb_dev_handle = c_void_p You'll be better off using class _usb_dev_handle(Structure): pass _usb_dev_handle_p = POINTER(_usb_dev_handle) This provides stronger type safety. c_void_p is too permissive. It's easier to debug a ctypes ArgumentError than a memory access violation or data corruption. [1]: http://sourceforge.net/p/libusb-win32/code/413/tree/trunk/libusb/src/lusb0_usb.h