Python Forum
use NULL as function parameter which is a pointer function?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
use NULL as function parameter which is a pointer function?
#1
Hi, I met a case in which NULL can be used as a C function's parameter which is actually a pointer function. I can't finger out how to do it, can anyone help. Thanks

1. the case

The C function in [LCUI](https://github.com/lc-soft/LCUI) is defined as
typedef void(*LCUI_WidgetEventFunc)(LCUI_Widget, LCUI_WidgetEvent, void*);

LCUI_API int Widget_BindEvent(LCUI_Widget widget, const char *event_name,
        LCUI_WidgetEventFunc func, void *data,
        void(*destroy_data)(void*));
which is called in [helloworld](https://github.com/lc-soft/LCUI/blob/dev...orld.c#L31) as

static void OnBtnClick(LCUI_Widget self, LCUI_WidgetEvent e, void *arg){
	do something();    
}

Widget_BindEvent(btn, "click", OnBtnClick, NULL, NULL);
Then I try to translate it into python

2. the 1st attempt

LCUI_WidgetEventFunc = ctypes.CFUNCTYPE(None, LCUI_Widget, LCUI_WidgetEvent, ctypes.c_void_p)

_Widget_BindEvent = dll.Widget_BindEvent
#~ _Widget_BindEvent.argtypes = LCUI_Widget, ctypes.c_char_p, LCUI_WidgetEventFunc, ctypes.c_void_p, ctypes.c_void_p
def Widget_BindEvent( widget, event_name, func, data, destroy_data,):
    event_name = event_name.encode('utf8')
    func = LCUI_WidgetEventFunc(func)

    return _Widget_BindEvent( widget, event_name, func, data, destroy_data)
then I call it as

def OnBtnClick(self, e, arg):
    do something()
    
Widget_BindEvent( btn, "click", LCUI_WidgetEventFunc(OnBtnClick), 
	None,
	ctypes.cast(None, ctypes.CFUNCTYPE(None, ctypes.c_void_p))
    );
But python fails to run this code and says
Output:
Traceback (most recent call last): File "helloworld.py", line 60, in <module> main() File "helloworld.py", line 54, in main ctypes.cast(None, ctypes.CFUNCTYPE(None, ctypes.c_void_p)) File "r:\lc\LCUI.py", line 88, in Widget_BindEvent destroy_data OSError: exception: access violation reading 0x0000000000000270
3. the 2nd try

_Widget_BindEvent = dll.Widget_BindEvent

prototype = ctypes.CFUNCTYPE(ctypes.c_int,
	LCUI_Widget,
	ctypes.c_char_p,
	LCUI_WidgetEventFunc,
	ctypes.c_void_p,
	ctypes.CFUNCTYPE(None, ctypes.c_void_p)
)

Widget_BindEvent = prototype(_Widget_BindEvent)

Widget_BindEvent( btn,
    "click".encode(),
    LCUI_WidgetEventFunc(OnBtnClick),
    None,
    ctypes.cast(None, ctypes.CFUNCTYPE(None, ctypes.c_void_p))
);
this time python runs the code but says

Output:
Traceback (most recent call last): File "_ctypes/callbacks.c", line 234, in 'calling callback function' OSError: exception: access violation reading 0x0000000000000270
So what is the proper way to wrap Widget_BindEvent so that we can use real function or NULL in it?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  The function of double underscore back and front in a class function name? Pedroski55 9 561 Feb-19-2024, 03:51 PM
Last Post: deanhystad
  determine parameter type in definition function akbarza 1 550 Aug-24-2023, 01:46 PM
Last Post: deanhystad
  Function parameter not writing to variable Karp 5 892 Aug-07-2023, 05:58 PM
Last Post: Karp
  How to express null value klatlap 3 815 Mar-25-2023, 10:40 AM
Last Post: klatlap
  [ERROR] ParamValidationError: Parameter validation failed: Invalid type for parameter gdbengo 3 10,646 Dec-26-2022, 08:48 AM
Last Post: ibreeden
  function with 'self' input parameter errors out with and without 'self' called dford 12 2,995 Jan-15-2022, 06:07 PM
Last Post: deanhystad
  value null when update in json file 3lnyn0 6 3,074 Dec-30-2021, 05:52 PM
Last Post: ndc85430
  Exit function from nested function based on user input Turtle 5 2,858 Oct-10-2021, 12:55 AM
Last Post: Turtle
Question Stopping a parent function from a nested function? wallgraffiti 1 3,618 May-02-2021, 12:21 PM
Last Post: Gribouillis
Question exiting the outer function from the inner function banidjamali 3 3,463 Feb-27-2021, 09:47 AM
Last Post: banidjamali

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020