comtypes
comtypes copied to clipboard
Date arguments raise ctypes.ArgumentError when a datetime value is used
I'm trying to call a COM method defined as follows (copy-pasted from the API docs, as I don't have access to the IDL):
Function ExportCalibrationToDCM(
ExportFileName As String,
[ExportChangedOnly As Boolean = False],
[ExportFullDataItemNames As Boolean = False],
[UseDisplayFormat As Boolean = False],
[ExportDCM2Format As Boolean = False],
[ExportComments As Boolean = False],
[ExportFunctions As Boolean = False],
[ExportUnits As Boolean = False],
[FilterFileName As String],
[ModificationSource As String],
[ModificationFromDateTime As Date],
[ModificationToDateTime As Date],
[FilterMatchOption As VISION_EXPORT_FILTER_MATCH_OPTION = VISION_EXPORT_FILTER_MATCH_AND_AXIS_ITEMS]
) As VISION_STATUS_CODES
The code that comtypes generates for this method looks like this:
COMMETHOD([dispid(1610743868), helpstring('Exports a DCM calibration file (*.dcm)')], HRESULT, 'ExportCalibrationToDCM',
( ['in'], BSTR, 'ExportFileName' ),
( ['in', 'optional'], VARIANT_BOOL, 'ExportChangedOnly', False ),
( ['in', 'optional'], VARIANT_BOOL, 'ExportFullDataItemNames', False ),
( ['in', 'optional'], VARIANT_BOOL, 'UseDisplayFormat', False ),
( ['in', 'optional'], VARIANT_BOOL, 'ExportDCM2Format', False ),
( ['in', 'optional'], VARIANT_BOOL, 'ExportComments', False ),
( ['in', 'optional'], VARIANT_BOOL, 'ExportFunctions', False ),
( ['in', 'optional'], VARIANT_BOOL, 'ExportUnits', False ),
( ['in', 'optional'], BSTR, 'FilterFileName', '' ),
( ['in', 'optional'], BSTR, 'ModificationSource', '' ),
( ['in', 'optional'], c_double, 'ModificationFromDateTime', datetime.datetime(1899, 12, 30, 0, 0) ),
( ['in', 'optional'], c_double, 'ModificationToDateTime', datetime.datetime(1899, 12, 30, 0, 0) ),
( ['in', 'optional'], VISION_EXPORT_FILTER_MATCH_OPTION, 'FilterMatchOption', 1 ),
( ['out', 'retval'], POINTER(VISION_STATUS_CODES), 'Status' )),
When I try to call this with just the first argument, or explicitly call it with a datetime object as ModificationFromDateTime, I get the following exception:
ctypes.ArgumentError: argument 11: <class 'TypeError'>: wrong type
I have to explicitly set the two date arguments to 0 in order to make the call succeed:
s.ExportCalibrationToDCM('c:\a.dcm', ModificationFromDateTime=0, ModificationToDateTime=0)