Python returns and outputs strings from the dynamic library

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.