ctypes and using c_char_p without NULL terminated string

K

kpoman

Hi to all,
I am trying to use some dll which needs some data types that I can't
find in python. From the dll documentation, I am trying to use this:

HRESULT IMKWsq::Compress ( [in] VARIANT rawImage,
[in] short sizeX,
[in] short sizeY,
[out] VARIANT * wsqImage,
[out, retval] short * result
)


I then tried using the following python code to achieve this:


# CODE START

import os
import sys
from ctypes import *
import win32con

MKWSQ_OK = 0
MKWSQ_MEMORY_ALLOC_ERROR = (MKWSQ_OK+200) #// Memory allocation error
MKWSQ_MEMORY_FREE_ERROR = (MKWSQ_OK+499) #// Memory disallocation
MKWSQ_INPUT_FORMAT_ERROR = (MKWSQ_OK+700) #// Error in the format of
the compressed data
MKWSQ_NULL_INPUT_ERROR = (MKWSQ_OK+5000) #// input buffer is NULL
MKWSQ_ROWSIZE_ERROR = (MKWSQ_OK+5003) #// Number of rows in the
image must be betwen 64 and 4000
MKWSQ_COLSIZE_ERROR = (MKWSQ_OK+5004) #// Number of columns in the
image must be between 64 and 4000
MKWSQ_RATIO_ERROR = (MKWSQ_OK+5005) #// The minimum compression
ratio value is 1.6 and the maximum 80, usually 12 for 15 real
MKWSQ_INPUT_PTR_ERROR = (MKWSQ_OK+5006) #// compress_buffer must be
the address of a char pointer
MKWSQ_OUTPUTSIZE_ERROR = (MKWSQ_OK+5007) #// compressed_filesize must
be the address of a long
MKWSQ_RATIO_PTR_ERROR = (MKWSQ_OK+5008) #// ratio_achieved must be
the address of a float
MKWSQ_NULL_OUTPUT_ERROR = (MKWSQ_OK+6000) #// compress_buffer has a
NULL value
MKWSQ_OUTPUT_ROWSIZE_ERROR = (MKWSQ_OK+6002) #// rows must be the
adress of an int
MKWSQ_OUTPUT_COLSIZE_ERROR = (MKWSQ_OK+6003) #// cols must be the
adress of an int
MKWSQ_OUTPUT_SIZE_ERROR = (MKWSQ_OK+6006) #// output_filesize must be
the adress of a long
MKWSQ_OUTPUT_PTR_ERROR = (MKWSQ_OK+6007) #// output_buffer must be
the adress of a char*
MKWSQ_DONGLE_ERROR = (MKWSQ_OK+9045) #// dongle or dongle driver is
not present
MKWSQ_DONGLE_TYPE_ERROR = (MKWSQ_OK+9046) #// the dongle licence does
not authorize to use the function
MKWSQ_LICENSE_ERROR = (MKWSQ_OK+133) #// invalid or nonexistent
software license

#Cargo la libreria e memoria
_wsq = cdll.mkwsq

def MKWsq_compress( iBuffer, iFreeBufferOption, iRows, iCols, iRatio,
oCompressedBuffer, oCompressedFileSize, oRatioAchieved):
''' Esta es la funcion de compresion '''
return _wsq.MKWsq_compress(iBuffer, iFreeBufferOption, iRows, iCols,
iRatio, 0, 0, byref(oCompressedBuffer), byref(oCompressedFileSize),
byref(oRatioAchieved))

def MKWsq_decompress(iCompressedBuffer, oRows, oCols, oFileSize,
oBuffer):
'''Funcion que se encarga de descomprimir la huella'''
return _wsq.MKWsq_decompress(iCompressedBuffer, 0, oRows, oCols,
0, 0, oFileSize, c_char_p(oBuffer))

def MKWsq_free(iCompressedBuffer):
''' Funcion para liberar la memoria alocada '''
return _wsq.MKWsq_free(iCompressedBuffer)


if __name__ == '__main__':
'''test del modulo'''
#Prueba de compresion desde RAW
fh=open("test.raw","r")
imRaw=fh.read()
fh.close()

fSize=c_long()
ratio=c_float(12.0)
ratioAchieved=c_float()

#imWSQ=c_ubyte(win32con.NULL) #declara la variable como NULL

imWSQ=c_char_p('\x00'*(20*1024))

status=MKWsq_compress(imRaw,
1,416,416,ratio,imWSQ,fSize,ratioAchieved)

print "Status=%d\tSize=%d bytes Ratio=%f"%
(status,fSize.value,ratioAchieved.value)

print repr(imWSQ)
print len(imWSQ.value[:10])

filito=open("file.wsq","wb")
filito.write(imWSQ[:fSize.value])
filito.close()

# CODE END

which gives me the following result:

c:\>python MKWsq_new.py
Status=0 Size=12735 bytes Ratio=13.589006
c_char_p('\xff\xa0\xff\xa8')
4
Traceback (most recent call last):
File "MKWsq_new.py", line 65, in ?
filito.write(imWSQ[:fSize.value])
TypeError: unsubscriptable object

c:\>




the problem is on this result line:
c_char_p('\xff\xa0\xff\xa8')

because of the c_char_p spec, it is a \x00 (null) character terminated
string, but my result has some null characters on it. Which ctype
should I then use to be able to retrieve the result from python ?

Thanks in advance,

Patricio
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,967
Messages
2,570,148
Members
46,694
Latest member
LetaCadwal

Latest Threads

Top