简体   繁体   中英

How to write a unsigned char* array containing hex values to file in python

I have a C dll that returns a PDF file as a unsigned char* containing hex values. How do I use ctypes to retrieve this array and save it to a file 'unearthing' the PDF?

For example in CI do this:

unsigned char* manual = getPluginManualAsPDF(mPlugin);
long nrOfBytes        = getPluginManualNrOfBytes(mPlugin);

string tempFile(mtk::JoinPath(getTempFolder(getRRHandleFromPlugin(mPlugin)), "AddNoisePluginDoc.pdf"));
ofstream temp(tempFile.c_str(), std::ios::binary);

long byteNr = 0;
while (byteNr < nrOfBytes)
{
    temp<< manual[byteNr];
    byteNr++;
}

temp.close();

    //Spanw an external pdf reader
    ShellExecute(Handle, "open", tempFile.c_str(), NULL, NULL,SW_SHOWNORMAL) ;
}

I am trying to access the same functions (getPluginManualAsPDF and getPluginManualNrOfBytes) via CTypes, ie

rrpLib.getPluginManualAsPDF.restype = c_buffer
def getPluginManualAsPDF(pluginHandle):
    return rrpLib.getPluginManualAsPDF(pluginHandle)

def getPluginManualNrOfBytes(pluginHandle):
    return rrpLib.getPluginManualNrOfBytes(pluginHandle)

But have problem with the return of getPluginManualAsPDF() function.. Any help appreciated.

Assuming your getPluginManualAsPDF and getPluginManualNrOfBytes have the following prototype:

/* FIXME: the type of `mPlugin` isn't clear, is it void * ? */

unsigned char * getPluginManualAsPDF(void *mPlugin) {
  static unsigned char manual[] = {1, 2, 3};
  return manual;
}

long getPluginManualNrOfBytes(void *mPlugin) {
  return 12345;
}

you can call these two functions using:

from ctypes import *

rrpLib = cdll.rrpLib # or windll if it use STDCALL

getPluginManualAsPDF = rrpLib.getPluginManualAsPDF
getPluginManualAsPDF.restype = POINTER(c_ubyte)
getPluginManualAsPDF.argtypes = [c_void_p]

getPluginManualNrOfBytes = rrpLib.getPluginManualNrOfBytes
getPluginManualNrOfBytes.restype = c_long
getPluginManualNrOfBytes.argtypes = [c_void_p]

for example:

# gcc -Wall rrpLib.c -shared -o rrpLib.dll
# python
>>> from ctypes import *
>>>
>>> rrpLib = cdll.rrpLib # or windll if it uses STDCALL
>>>
>>> getPluginManualAsPDF = rrpLib.getPluginManualAsPDF
>>> getPluginManualAsPDF.restype = POINTER(c_ubyte)
>>> getPluginManualAsPDF.argtypes = [c_void_p]
>>>
>>> getPluginManualNrOfBytes = rrpLib.getPluginManualNrOfBytes
>>> getPluginManualNrOfBytes.restype = c_long
>>> getPluginManualNrOfBytes.argtypes = [c_void_p]
>>>
>>> getPluginManualAsPDF(None)[0]
1
>>> getPluginManualAsPDF(None)[1]
2
>>> getPluginManualAsPDF(None)[2]
3
>>>
>>>
>>> ptr = getPluginManualAsPDF(None)
>>> manual = cast(ptr, POINTER(c_ubyte * 3))[0]
>>> manual
<__main__.c_ubyte_Array_3 object at 0x017801C0>
>>> open('out.txt', 'wb').write(bytearray(manual))
>>> open('out.txt').read()
'\x01\x02\x03'
>>>
>>>
>>> getPluginManualNrOfBytes(None)
12345
>>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM