How can I pass an array to a function in the dynamic library and return a string through the passed array?
Here is an indirect method. I wonder if there is a more direct way?
1. function definition in the dynamic library:
Struct ss {
Char name [10];
Int age;
};
Void GetString (struct ss * p)
{
Strcpy (p-> name, "Hello dll .");
P-> age = 25;
}
Compile and generate dll. so: gcc-fPIC-O2-shared dll. c-o dll. so
2. Call an instance in python:
From ctypes import *
Class ss (Structure ):
_ Fields _ = [
("Name", c_byte * 10 ),
("Age", c_int ),
]
# Name is defined as c_byte * 10, and the storage is interger. It is troublesome to output it as a string.
FileName = "/home/primax/Desktop/Work/Test/python/dll. so"
Lib = cdll. LoadLibrary (fileName)
T = ss ()
Lib. GetString (pointer (t ))
Tt = create_string_buffer (11)
Memmove (tt, byref (t), 10)
Temp = tt. value. _ str _ () [2:-1]
Print (temp)
Print Output:
Hello dll.