Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Because library_version requires a char**, they don't want you to allocate the characters (as you're doing with create_string_buffer. Instead, they just want you to pass in a reference to a pointer so they can return the address of where to find the version string.</p> <p>So all you need to do is allocate the pointer, and then pass in a reference to that pointer.</p> <p>The following code should work, although I don't have obo.dll (or know of a suitable replacement) to test it.</p> <pre><code>from ctypes import * _OBO_C_DLL = 'obo.dll' STRING = c_char_p _stdcall_libraries = dict() _stdcall_libraries[_OBO_C_DLL] = WinDLL(_OBO_C_DLL) OBO_VERSION = _stdcall_libraries[_OBO_C_DLL].OBO_VERSION OBO_VERSION.restype = c_int OBO_VERSION.argtypes = [POINTER(STRING)] def library_version(): s_res = c_char_p() res = OBO_VERSION(byref(s_res)) if res != 0: raise Error("OBO error %r" % res) return s_res.value library_version() </code></pre> <p>[Edit]</p> <p>I've gone a step further and written my own DLL that implements a possible implementation of OBO_VERSION that does not require an allocated character buffer, and is not subject to any memory leaks.</p> <pre><code>int OBO_VERSION(char **pp_version) { static char result[] = "Version 2.0"; *pp_version = result; return 0; // success } </code></pre> <p>As you can see, OBO_VERSION simply sets the value of *pp_version to a pointer to a null-terminated character array. This is likely how the real OBO_VERSION works. I've tested this against my originally suggested technique above, and it works as prescribed.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload