Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Here's an example of what we were discussing. This code expects the caller to allocate the buffer.</p> <h3>Test code (Windows)</h3> <pre><code>// IN: test=pre-allocated buffer. // IN: size=size of pre-allocated buffer. // RETURNS: 0 and size set to required size. // 1 and size set to size used. __declspec(dllexport) int testout(void *pdata, int *psize) { unsigned char * p = (unsigned char *)pdata; if (*psize &lt; 5) { *psize = 5; // set size required return 0; // fail } p[0] = 123; p[1] = 255; p[2] = 237; p[3] = 12; p[4] = 222; *psize = 5; // indicate size used return 1; // pass } </code></pre> <h3>Python</h3> <pre><code>from ctypes import * test = CDLL('test') size = c_int(0) test.testout.argtypes=[c_void_p,POINTER(c_int)] print "Result of NULL pointer and zero size:",test.testout(None,byref(size)) print "Returned size:",size.value mem = (c_ubyte * 2)() size = c_int(sizeof(mem)) print "sizeof(mem):",size.value print "Result of small buffer:",test.testout(byref(mem),byref(size)) print "Returned size:",size.value mem = (c_ubyte * size.value)() print "Result of exact size buffer:",test.testout(byref(mem),byref(size)) print "Returned size:",size.value for i in range(size.value): print mem[i] mem = (c_ubyte * 20)() size = c_int(20) print "Result of bigger buffer:",test.testout(byref(mem),byref(size)) print "Returned size:",size.value for i in range(size.value): print mem[i] </code></pre> <h3>Output</h3> <pre><code>Result of NULL pointer and zero size: 0 Returned size: 5 sizeof(mem): 2 Result of small buffer: 0 Returned size: 5 Result of exact size buffer: 1 Returned size: 5 123 255 237 12 222 Result of bigger buffer: 1 Returned size: 5 123 255 237 12 222 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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