Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 976325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:43:47+00:00 2026-05-16T03:43:47+00:00

I’m writing some python code to interact with a C DLL that uses structures

  • 0

I’m writing some python code to interact with a C DLL that uses structures extensively.

One of those structures contains nested structures. I know that this is not a problem for the ctypes module. The problem is that there is an often used structure that, in C, is defined via macro because it contains an “static” length array that can vary. That is confusing so here’s some code

struct VarHdr {
    int size;
}

#define VAR(size) \
    struct Var {
        VarHdr hdr;
        unsigned char Array[(size)];
    }

Then it is used in other structures like this

struct MySruct {
    int foo;
    VAR(20) stuffArray;
}

The question then becomes how can I emulate this in Python in a way that the resulting structure can be passed back and forth between my pythong script and the DLL.

BTW, I know that I can just hardcode the number in there but there are several instances of this “VAR” throughout that have different sizes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T03:43:48+00:00Added an answer on May 16, 2026 at 3:43 am

    Just use a factory to define the structure once the size is known.

    http://docs.python.org/library/ctypes.html#variable-sized-data-types:

    Another way to use variable-sized data
    types with ctypes is to use the
    dynamic nature of Python, and
    (re-)define the data type after the
    required size is already known, on a
    case by case basis.

    (untested) Example:

    def define_var_hdr(size):
       class Var(Structure):
           fields = [("size", c_int),
                     ("Array", c_ubyte * size)]
    
       return Var
    
    var_class_10 = define_var_hdr(10)
    var_class_20 = define_var_hdr(20)
    var_instance_10 = var_class_10()
    var_instance_20 = var_class_20()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm making a simple page using Google Maps API 3. My first. One marker
I have some data like this: 1 2 3 4 5 9 2 6
We are using XSLT to translate a RIXML file to XML. Our RIXML contains

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.