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

  • Home
  • SEARCH
  • 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 6740779
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:38:49+00:00 2026-05-26T11:38:49+00:00

I have to define a callback function in Python, one that will be called

  • 0

I have to define a callback function in Python, one that will be called from a DLL.

BOOL setCallback (LONG nPort, void ( _stdcall *pFileRefDone) (DWORD nPort, DWORD nUser), DWORD nUser);

I tried this code, that seems to work in Python 2.5 but with Python 2.7 it crashes and I assume I did something wrong.

import ctypes, ctypes.wintypes
from ctypes.wintypes import DWORD

def cbFunc(port, user_data):
        print "Hurrah!"

CB_Func = ctypes.WINFUNCTYPE(None, DWORD, DWORD)

mydll.setCallback(ctypes.c_long(0), CB_Func(cbFunc), DWORD(0))

Where is the mistake?

Note: The platform is running on 32bit only (both Windows and Python). The DLL loads successfully and also other functions from inside do work just fine while called from Python.

A full sample code that reproduces this is available at https://github.com/ssbarnea/pyHikvision – just run Video.py under py25 and py27.

  • 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-26T11:38:49+00:00Added an answer on May 26, 2026 at 11:38 am

    In the context of Video class using a nested function:

    class Video(object):
    
        def __init__(self, ...):
            self.__cbFileRefDone = []
    
        def open(self, filename):
            @WINFUNCTYPE(None, DWORD, DWORD)
            def cbFileRefDone(port, user_data):
                print "File indexed.", filename
            self.__cbFileRefDone.append(cbFileRefDone) # save reference
    
            if not self.hsdk.PlayM4_SetFileRefCallBack(
                c_long(self.port), cbFileRefDone, DWORD(0)):
                logging.error("Unable to set callback for indexing")
                return False
    

    It is somewhat ugly but a more natural variant fails with TypeError during callback:

    #XXX this won't work
    @WINFUNCTYPE(None, DWORD, DWORD)
    def cbFileRefDone(self, port, user_data):
        print "File indexed."
    

    To fix that a special function descriptor could be created:

    def method(prototype):
        class MethodDescriptor(object):
            def __init__(self, func):
                self.func = func
                self.bound_funcs = {} # hold on to references to prevent gc
            def __get__(self, obj, type=None):
                assert obj is not None # always require an instance
                try: return self.bound_funcs[obj,type]
                except KeyError:
                    ret = self.bound_funcs[obj,type] = prototype(
                        self.func.__get__(obj, type))
                    return ret
        return MethodDescriptor
    

    Usage:

    class Video(object):
    
        @method(WINFUNCTYPE(None, DWORD, DWORD))
        def cbFileRefDone(self, port, user_data):
            print "File indexed."
    
        def open(self, filename):
            # ...
            self.hsdk.PlayM4_SetFileRefCallBack(
                c_long(self.port), self.cbFileRefDone, DWORD(0))
    

    Additionally MethodDescriptor saves references to C functions to prevent them being garbage collected.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a C library function that expects a function pointer for callback, and
I have a LRESULT CALLBACK function in a header file(hook.h) that I both forward
I have an object called ValueBox that I created like this: function ValueBox(params) {
I have to define the grammar of a file like the one shown below.
In my database I have tables that define types for example Table: Publication Types
I have the following situation: A user will define a certain filter on a
I have a function that takes a string object name and I need the
I am not so strong in javascript. I have a common function that I
I have this code: #include <stdio.h> #include <stdlib.h> #include <signal.h> // Define the function
I have a storage with models: Ext.define('App.Supplier.Store', { extend : 'Ext.data.Store', constructor : function(config)

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.