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 7621423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T04:11:29+00:00 2026-05-31T04:11:29+00:00

I have am inside a callback that allows me to access the pointers passed

  • 0

I have am inside a callback that allows me to access the pointers passed to it as int (i.e. the Python type() function returns int).

What so I have to do if I want convert this into a pointer of a struct whose layout I know and can define as a ctypes class. Say I have this already:

class data_t(Structure):
  _fields = [ ("foo", c_int), ("bar", c_wchar_p), ("baz", c_char_p) ]

How can a variable x for which type(x) gives the output int now be cast into a pointer to the above struct type?

The gist: I have no influence on the declaration of the callback function or the declaration of the arguments I get to see, so I need some way to get from the Python int type to a ctypes pointer and access the struct members from there …

This is Python 2.6, but I reckon most 2.x will be similar enough at least. I can’t get rid of this version requirement as it is embedded into a product that requires this particular Python version.

  • 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-31T04:11:30+00:00Added an answer on May 31, 2026 at 4:11 am

    You can use ctypes.cast, or just declare the callback to return a POINTER(data_t). Examples of both below and tested on Python 2.7 and Python 3.2:

    test DLL code (Windows)

    typedef struct data {
        int foo;
        wchar_t* bar;
        char* baz;
    } data_t;
    
    typedef void (*CALLBACK)(data_t* p);
    
    CALLBACK g_callback = 0;
    
    extern "C" __declspec(dllexport) void set_callback(CALLBACK f)
    {
        g_callback = f;
    }
    
    extern "C" __declspec(dllexport) void call_callback()
    {
        data_t data;
        data.foo = 123;
        data.bar = L"马克";
        data.baz = "Mark";
        g_callback(&data);
    }
    

    Python

    from ctypes import *
    
    class data_t(Structure):
        _fields_ = [
            ("foo", c_int),
            ("bar", c_wchar_p),
            ("baz", c_char_p)]
    
    dll = CDLL('test')
    
    @CFUNCTYPE(None,c_int)
    def callback(n):
        p = cast(n,POINTER(data_t)).contents
        print(p.foo,p.bar,p.baz)
    
    @CFUNCTYPE(None,POINTER(data_t))
    def callback2(n):
        p = n.contents
        print(p.foo,p.bar,p.baz)
    
    dll.set_callback(callback)
    dll.call_callback()
    
    dll.set_callback(callback2)
    dll.call_callback()
    

    Output

    123 马克 b'Mark'
    123 马克 b'Mark'
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function inside a class that returns a reference to a member
INTRODUCTION : I have a function(callback) that receives a object as an argument, inside
I have a library-provided function that runs a callback after some processing. Inside that
I have a simple program that uses FindWindowEx & strncmp() inside a callback passed
I have a callback that is called through a delegate. Inside it I will
HI! I have a surfaceView inside a horizontal scrollview that I want to fill
I have a before_create callback that sets an error inside an object. class Animal
I have a function that I want to invoke every x seconds, but I
i have Linkbutton inside updatePanel and when i click on it it returns following
Some of the queries I have written inside MS-Access are getting deleted automatically. And

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.