我想从Python调用Windows TaskDialogIndirect函数。它需要TaskDialogConfig(相当大)结构作为指针传递。
这是我准备运行的示例。它给我“ -2147024809”(该参数不正确),我无法弄清楚出了什么问题。
Python 3.7.4 x32,Windows 7 x64
import ctypes
from ctypes.wintypes import *
TDF_ALLOW_DIALOG_CANCELLATION = 8
TDCBF_OK_BUTTON = 1
class TaskDialogConfig(ctypes.Structure):
class DUMMYUNIONNAME(ctypes.Union):
_fields_ = [
('hMainIcon', HICON)
, ('pszMainIcon', LPCWSTR)
]
class DUMMYUNIONNAME2(ctypes.Union):
_fields_ = [
('hFooterIcon', HICON)
, ('sFooterIcon', LPCWSTR)
]
class _TASKDIALOG_BUTTON(ctypes.Structure):
_fields_ = [
('nButtonID', INT)
, ('pszButtonText', LPCWSTR)
]
_fields_ = [
('cbSize', UINT)
, ('hwndParent', HWND)
, ('hInstance', HINSTANCE)
, ('dwFlags', UINT)
, ('dwCommonButtons', UINT)
, ('pszWindowTitle', LPCWSTR)
, ('DUMMYUNIONNAME', DUMMYUNIONNAME)
, ('pszMainInstruction', LPCWSTR)
, ('pszContent', LPCWSTR)
, ('cButtons', UINT)
, ('pButtons', _TASKDIALOG_BUTTON)
, ('nDefaultButton', INT)
, ('cRadioButtons', UINT)
, ('pRadioButtons', _TASKDIALOG_BUTTON)
, ('nDefaultRadioButton', INT)
, ('pszVerificationText', LPCWSTR)
, ('pszExpandedInformation', LPCWSTR)
, ('pszExpandedControlText', LPCWSTR)
, ('pszCollapsedControlText', LPCWSTR)
, ('DUMMYUNIONNAME2', DUMMYUNIONNAME2)
, ('pszFooter', LPCWSTR)
, ('pfCallBack', ctypes.POINTER(None))
, ('lpCallbackData', LPLONG)
, ('cxWidth', UINT)
]
def __init__(s):
s.cbSize = ctypes.sizeof(s)
tdi = ctypes.WinDLL('comctl32.dll').TaskDialogIndirect
tdc = TaskDialogConfig()
tdc.hwndParent = None
tdc.hInstance = None
tdc.dwFlags = TDF_ALLOW_DIALOG_CANCELLATION
tdc.dwCommonButtons = TDCBF_OK_BUTTON
tdc.pszWindowTitle = ctypes.c_wchar_p('Title')
tdc.pszMainInstruction = ctypes.c_wchar_p('Main instruction')
tdc.pszContent = ctypes.c_wchar_p('Content')
print( tdi(ctypes.byref(tdc), None, None, None) )
两个问题。
_pack_ = 1
定义之前添加_fields_
。_TASKDIALOG_BUTTON
字段应为ctypes.POINTER(_TASKDIALOG_BUTTON)
类型。