473,386 Members | 1,654 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

ctypes and using c_char_p without NULL terminated string

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

Feb 26 '07 #1
0 2393

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call...
3
by: Lenard Lindstrom | last post by:
Posted in a previous thread was some Python code for accessing Window's Simple MAPI api using the ctypes module. http://groups-beta.google.com/group/comp.lang.python/msg/56fa74cdba9b7be9 This...
1
by: Srijit Kumar Bhadra | last post by:
Hello, Here are code snippets to create and access shared memory in Python with and without ctypes module. With regards, Srijit Filename : SharedMemCreate.py import msvcrt, mmap
0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
12
by: p.lavarre | last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes how? A: We are of course supposed to write something like: def c_not_null(pv): return (ctypes.cast(pv,...
1
by: sjdevnull | last post by:
Hey, I'm trying to wrap GNU readline with ctypes (the Python readline library doesn't support the callback interface), but I can't figure out how to set values to a variable inside the library. ...
3
by: Chris AtLee | last post by:
Sorry for the repeat post...I'm not sure if my first post (on May 30th) went through or not. I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the...
6
by: Jack | last post by:
I'm not able to build IP2Location's Python interface so I'm trying to use ctypes to call its C interface. The functions return a pointer to the struct below. I haven't been able to figure out how...
3
by: waldek | last post by:
Hi, I'm using C dll with py module and wanna read value (buffer of bytes) returned in py callback as parameter passed to dll function. -------------------------------------------------- def ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.