开发者

Python Structures

开发者 https://www.devze.com 2023-02-21 22:05 出处:网络
I am try to convert a C Structure to Python using Ctypes. The Structure I am trying to convert is : typedef struct LibraryInfo

I am try to convert a C Structure to Python using Ctypes.

The Structure I am trying to convert is :

typedef struct LibraryInfo
    {
        uint32_t    size;                                // Size of the structure
        char        libName[MAX_LIBRARY_NAME+1];                        // Library name
        char        provider[MAX_LIBRARY_PROVIDER_NAME+1];                  // Provider
        uint32_t    version;                                                    // Library version, i.e: 0x01030005 --> v.01.03.0005  
    } LibraryInfo;  

The equivalent Python Code is:

class LibraryInfo(Structure):  
    _fields_=[("size",c_uint),  
              ("libName",c_char * MAX_LIBRARY_NAME ),  
              ("provider",c_char * MAX_LIBRARY_PROVIDER_NAME),  
              ("version",c_uint)]  

The functions which takes this structure as argument is resCode = QueryLibraryInfo(&libraryInfo);

The error I am getting is invalid parameters passed.开发者_如何学JAVA This is a library function call. I am using this in python HPDRLGL_MAX_LIBRARY_NAME=200 HPDRLGL_MAX_LIBRARY_PROVIDER_NAME=200 class HPDRLGL_LibraryInfo(Structure): fields=[("size",c_uint), ("libName",c_char *(HPDRLGL_MAX_LIBRARY_NAME+1)), ("provider",c_char * (HPDRLGL_MAX_LIBRARY_PROVIDER_NAME+1)), ("version",c_uint)] Still I am getting the same error, INVALID PARAMETERS . I have passes a pointer to the structure as you said .


From the function signature, it seems that you need to pass a pointer to the structure:

libraryInfo = LibraryInfo()
resCode = QueryLibraryInfo(byref(libraryInfo))

And you should really keep the + 1 in your structure definition.

0

精彩评论

暂无评论...
验证码 换一张
取 消