Python ctypes passing string pointer / buffer

ozboss

I am trying to get information from a sensor through its DLL with Python.
I never worked with ctypes or C in general before so I lack some knowlege about this, hopefully somebody can help me.
Calling the functions work so far but I cannot get the functions to give back string information. What works so far:

from ctypes import *

libc = windll.LoadLibrary("MEDAQLib.dll")

version = create_string_buffer(15)
libc.GetDLLVersion(version, 15)
print(version.value.decode())

This returns the version number of the DLL and works correctly.
Now I try to get some information about the sensor in the same way.

# Create sensor instance and open connection
sensor = libc.CreateSensorInstance(42)
libc.OpenSensorRS232(sensor, b'COM4')

# Try to get information from sensor
name = create_string_buffer(32)
libc.ExecSCmdGetString(sensor, b'Get_Info', b'SA_Sensor', name, 32)
print(name.value.decode())

The ExecSCmdGetString function failes with OSError: exception: access violation reading 0x0000000000000020

Declaration for GetDLLVersion:
ERR_CODE GetDLLVersion (char *versionStr, uint32_t maxLen);
Parameter:
uint32_t maxLen
Description:
Length of string buffer. If the version info is longer as maxLen it is truncated. The string is null terminated.

Declaration for ExecSCmdGetString:
ERR_CODE ExecSCmdGetString (uint32_t instanceHandle, const char *sensorCommand, const char *paramName, char *paramValue, uint32_t *maxLen);
Parameter:
uint32_t * maxLen
Description:
The buffer must be allocated by the application. The size of the buffer is specified at maxLen. If the resulting string is larger than maxLen, it is truncated and ERR_NOMEMORY (-19) is returned. The real length of the string (maybe truncated) is returned in maxLen too. If paramValue is nullptr, the length of the containing string is returned in maxLen.

So obviously I cannot just specify the maxLen as an integer anymore.
But how do I have to do it then?

I also tried to just make a pointer:

name = c_char_p()
libc.ExecSCmdGetString(sensor, b'Get_Info', b'SA_Sensor', byref(name))

But then how do I specify maxLen?

Joseph Sible-Reinstate Monica

Like this:

paramValue = create_string_buffer(32)
maxLen = c_uint32(32)
errCode = libc.ExecSCmdGetString(sensor, b'Get_Info', b'SA_Sensor', paramValue, byref(maxLen))

Your first attempt didn't work because maxLen is a uint32_t *, but you just passed it a uint32_t. Wrapping it in byref() fixes this.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Python Ctypes passing pointer for data

From Dev

Pointer argument passing in python ctypes

From Dev

Passing pointer to first string element as buffer

From Dev

python ctypes how to write string to given buffer

From Dev

Python ctypes: how to define a callback having a buffer pointer and a length in argument?

From Dev

ctypes string buffer in scope

From Dev

Mallocing and Freeing in C, but passing the pointer through Python via ctypes

From Dev

Python -passing imgdata pointer to a function in C using ctypes

From Dev

ctypes: passing and reading an enum pointer

From Dev

ctypes from buffer - Python

From Dev

Python 3.5 - ctypes - create string buffer for Citect API

From Dev

ctypes - passing a struct with a pointer to another struct

From Dev

Passing a cupy pointer to a CUDA kernel using ctypes

From Dev

ctypes string pointer type error

From Dev

Python ctypes: Passing an array of strings

From Dev

Python ctypes usage passing pointers

From Dev

Passing objects by reference in python with ctypes

From Dev

Python ctypes pointer and allocated memory

From Dev

Python Bytes to CTypes Void Pointer

From Dev

python ctypes - wrap void pointer

From Dev

How to print contents of ctypes string buffer

From Dev

Pass a string as a char pointer array using ctypes

From Dev

Why do I get a calling convention mismatch when passing a pointer to a slice with a cdecl Rust function and Python's Ctypes?

From Dev

Ctypes, Passing pointer to enum, receiving uint_32

From Dev

passing string from python to c++ using ctypes - only the first character is sent

From Dev

Create a buffer and assign it a pointer in Python

From Dev

Passing python strings to Fortran subroutine using ctypes

From Dev

Passing struct from C to Python with ctypes

From Dev

Passing audio data from Python to C with ctypes

Related Related

  1. 1

    Python Ctypes passing pointer for data

  2. 2

    Pointer argument passing in python ctypes

  3. 3

    Passing pointer to first string element as buffer

  4. 4

    python ctypes how to write string to given buffer

  5. 5

    Python ctypes: how to define a callback having a buffer pointer and a length in argument?

  6. 6

    ctypes string buffer in scope

  7. 7

    Mallocing and Freeing in C, but passing the pointer through Python via ctypes

  8. 8

    Python -passing imgdata pointer to a function in C using ctypes

  9. 9

    ctypes: passing and reading an enum pointer

  10. 10

    ctypes from buffer - Python

  11. 11

    Python 3.5 - ctypes - create string buffer for Citect API

  12. 12

    ctypes - passing a struct with a pointer to another struct

  13. 13

    Passing a cupy pointer to a CUDA kernel using ctypes

  14. 14

    ctypes string pointer type error

  15. 15

    Python ctypes: Passing an array of strings

  16. 16

    Python ctypes usage passing pointers

  17. 17

    Passing objects by reference in python with ctypes

  18. 18

    Python ctypes pointer and allocated memory

  19. 19

    Python Bytes to CTypes Void Pointer

  20. 20

    python ctypes - wrap void pointer

  21. 21

    How to print contents of ctypes string buffer

  22. 22

    Pass a string as a char pointer array using ctypes

  23. 23

    Why do I get a calling convention mismatch when passing a pointer to a slice with a cdecl Rust function and Python's Ctypes?

  24. 24

    Ctypes, Passing pointer to enum, receiving uint_32

  25. 25

    passing string from python to c++ using ctypes - only the first character is sent

  26. 26

    Create a buffer and assign it a pointer in Python

  27. 27

    Passing python strings to Fortran subroutine using ctypes

  28. 28

    Passing struct from C to Python with ctypes

  29. 29

    Passing audio data from Python to C with ctypes

HotTag

Archive