How to use optional CALLBACK parameters in ctypes

Hi all,
I am trying to use ‘SetTimer’ function from windows api. This is my declaration in python.

SetTimer = windll.user32.SetTimer
SetTimer.argtypes = [HWND, UINT_PTR, UINT, TIMERPROC]
SetTimer.restype = UINT_PTR

And this is TIMERPROC function declaration.

TIMERPROC = WINFUNCTYPE(UINT_PTR, HWND, UINT, UINT_PTR, DWORD)

This is the signature of SetTimer function in MSDN

UINT_PTR SetTimer(
  [in, optional] HWND      hWnd,
  [in]           UINT_PTR  nIDEvent,
  [in]           UINT      uElapse,
  [in, optional] TIMERPROC lpTimerFunc
);

See, the 4th parameter is optional. I know that there is no concept of ‘optional’ in ctypes. So I used ‘None’ for 4th parameter. But python shows this error message.
ctypes.ArgumentError: argument 4: <class 'TypeError'>: expected WinFunctionType instance instead of NoneType
How to fix this ?

Create a corresponding NULL pointer by initializating the function prototype with 0: TIMERPROC(0)

1 Like

Thank you @ Cornelius Krupp
Let me try that.

Edit:
Wow! It worked like a charm.