how to access character pointer value using ctypes?

user6730734

I have a written a DLL in which I'm getting one of the paths:

//demo.h
__declspec(dllexport) void pathinfo(char * path);

Something is being done in the code to get this path. And now, the python script that I have written to retrieve this path from the DLL is as shown:

//demo.py
import sys 
import ctypes
from ctypes import *
class demo(object):
  def __init__(self):
     self.demoDLL=CDLL("demo.dll")

  def pathinfo(self):
     path=c_char()
     self.demoDLL.pathinfo.argtypes(POINTER(c_char))
     self.demoDLL.pathinfo.result=None
     self.demoDLL.pathinfo(byref(path))
     return path.value

if __name__=='__main__':
    abc=demo()
    path_info=abc.pathinfo()
    print "information of the path:",path_info

But the value that I'm able to see is just the first character of the path instead of the whole string.

Can anybody help me with this problem?

code_onkel

The reason you see only the first character is that by calling c_char() you create a single char value that Python treats like a str (Python 2) object or bytes (Python 3) object of length 1. You are probably lucky that you do not get a segmentation fault. By writing more than 1 byte or a NULL-terminated string of length > 0 (e.g. with strcpy) in the C code, you actually produce an undetected buffer overflow. ctypes does not know how many bytes you have written at the pointer's memory location. path.value is still a str / bytes of length 1.

It would be better to change the C pathinfo function into someting like

size_t pathinfo(char* path, size_t bsize);

Use ctypes.create_string_buffer() to allocate memory in your Python code and let pathinfo return the length of the result. Of course you have to check, whether char* path is large enough using bsize in your C-Code.

The Python-code would look like this:

buf = ctypes.create_string_buffer(256)
result_len = pathinfo(buf, len(buf))
if result_len == len(buf):
    # buffer may have been too short,
    # try again with larger buffer
    ...
restlt_str = buf[0:result_len].decode('utf-8') # or another encoding

Also be aware of NULL-termination in the C domain, character encodings when converting python strings to char* and back, the changes regarding str / bytes in ctypes regarding Python 2 and Python 3.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to access character inside char * using pointer

How to produce a character array in ctypes and pass its pointer as a function argument

Access the value using pointer in Ada

How do I access a typedef struct pointer to an internal object with ctypes?

Print Integer value using character pointer in c

How to call ctypes functions that use pointer to return value in Numba @jit

How to get value of char pointer from Python code wtih ctypes

How to access string as character value

How to pass a Pointer of String-Array in a function using Ctypes?

OCaml Ctypes - How to make the pointer to pointer

How to access a pointer's value that is a char array

Get the value of a ctypes.c_ulong pointer?

Toggle a character using a pointer

How to access position of an array of struct using pointer

How to access nested structure using pointer and array?

How to access a pointer of struct in a struct using a function?

Pass a string as a char pointer array using ctypes

Using pointer to date (DATE * pValnDate) in ctypes

Passing a cupy pointer to a CUDA kernel using ctypes

How to understand the pointer when assign value in integer and single character in C?

how to specify a JNR Pointer like that of python ctypes

How to cast a ctypes pointer to an instance of a Python class

Pass parameter of type pointer to pointer to a C function using ctypes

Change value of character pointed by a pointer

taking integer value in character pointer

How can I find the length of a character pointer using strlen() in C?

How to change value of array using pointer?

Using pointer to character as the argument of strtok

Store integer using character pointer