<html><head><style type="text/css"><!-- DIV {margin:0px;} --></style></head><body><div style="font-family:times new roman,new york,times,serif;font-size:12pt"><pre class="jive-pre"><code class="jive-code jive-java">Hi,<br><br>I'm still working on a program that uses a .dll to read SPSS system files.<br>It's getting somewhere already, but I'm still struggling with one thing. <br>When using the C function spssGetVarNames I'm having trouble <br>translating C arrays to Python lists. I want to translate an <br>array that holds the variable names into a Python list. Same thing <br>for variable types.<br><br></code><code class="jive-code jive-java">The documentation of the SPSSIO dll says the following:<br>spssGetVarNames<br>int spssGetVarNames (int handle, int *numVars, char ***varNames, int **varTypes)<br>Parameter - Description<br>handle - Handle to the data file<br>numVars - Pointer to number of variables<br>varNames - Pointer to array of pointers to
 variable<br>varTypes - Pointer to array of variable types<br></code><code class="jive-code jive-java"><br><span>In the code below, which can also be found on <a target="_blank" href="http://pastebin.com/d7d0hpyV">http://pastebin.com/d7d0hpyV</a>, </span><br>the equivalent Python function is called getVarNamesAndTypes(). This is <br>the output I typically get:<br>retcode: 0<br>Varnames: ['\x80\xde\x10\x01P\xd8\x10\x01\xf0\xd0\x10\x01', None, None, None, None, None, None, None, None, None]<br>varTypes: [17885264, 0, 0, 0, 0, 0, 0, 0, 0, 0]<br><br>The first item of each list is clearly wrong, but what does it mean? If varType &gt; 0 it is<br>supposed to be a string var of that length. And of course the varNames are mostly 'None'.<br><br>Probably, something is wrong with the way the C arrays are initialized, <br>or with the two list comprehensions, but I can't find the solution.<br>varNames = [varNamesPtr[0][i] for i in range(numVars)] does not seem to
 work.<br><br>For those who are interested, the complete program can be found on <br><span><a target="_blank" href="http://pastebin.com/ff1b1Y9a">http://pastebin.com/ff1b1Y9a</a> (note that it's still work in progress)</span><br><br>Any hints? Thanks very much in advance!<br><br></code><code class="jive-code jive-java"><font color="navy"><b>import</b></font> os, </code><code class="jive-code jive-java">ctypes, datetime<br>&nbsp;<br># dll and py file should be in the same dir!<br>def loadSavFile(fn):<br>    os.environ[<font color="red">"PATH"</font>] += <font color="red">";"</font> + os.path.abspath(os.curdir)<br>    ctypes.cdll.LoadLibrary(<font color="red">"spssio32.dll"</font>)<br>    spssio = ctypes.windll.spssio32<br>    libc = ctypes.cdll.msvcrt<br>&nbsp;<br>    <font color="navy"><b>if</b></font> os.path.exists(fn):<br>        fh = libc._fdopen(fn, <font color="red">"rb"</font>)<br>        fhPtr = ctypes.pointer(ctypes.c_int(fh))<br>        retcode
 = spssio.spssOpenRead(ctypes.c_char_p(fn), fhPtr)<br>        <font color="navy"><b>return</b></font> retcode, spssio, fh<br>    <font color="navy"><b>else</b></font>:<br>        raise Exception, <font color="red">"File '%s' does not exist!"</font> % fn<br>&nbsp;<br>def getVarNamesAndTypes(fh, spssio):<br>    numVarsPtr = ctypes.pointer(ctypes.c_int())<br>    spssio.spssGetNumberofVariables(fh, numVarsPtr)<br>    numVars = numVarsPtr[0]<br>    <br>    varNamesArray = (ctypes.c_char_p * numVars)() <br>    varNamesPtr = ctypes.pointer(varNamesArray)<br>&nbsp;<br>    varTypesArray = (ctypes.c_int * numVars)()<br>    varTypesPtr = ctypes.pointer(varTypesArray)<br>    <br>    retcode = spssio.spssGetVarNames(fh, numVarsPtr, varNamesPtr, varTypesPtr)<br>&nbsp;<br>    varNames = [varNamesPtr[0][i] <font color="navy"><b>for</b></font> i in range(numVars)] # -- WRONG!!<br>    varTypes = [varTypesPtr[0][i] <font color="navy"><b>for</b></font> i in
 range(numVars)]<br>&nbsp;<br>    <font color="navy"><b>return</b></font> retcode, varNames, varTypes<br>&nbsp;<br>savFileName = r<font color="red">"C:\Program Files\SPSS\Employee data.sav"</font><br>retcode, spssio, fh = loadSavFile(savFileName)<br>retcode, varNames, varTypes = getVarNamesAndTypes(fh, spssio)<br>print <font color="red">"retcode:"</font>, retcode<br>print <font color="red">"Varnames:"</font>, varNames<br></code></pre><div><br></div>Cheers!!<br>Albert-Jan<br><br><div>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br>All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?<br>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<div><br></div></div>
</div><br>

      </body></html>