I want to use the WinAPI (CreateProcessA) in python with the help of ctypes.
In order to call the function correctly, I should make some structure (e.g. STARTUPINFO, PROCESSINFORMATION) beforehand.
I know that when you want to use a structure in a WinAPI, you should use ZeroMemory to fill the structure with zeros.
Should I do the same thing in python?
The book i am reading does not provide an answer to this question.
It depends. I don’t think there is any single blanket requirement. Let us consider the two examples you give, in the context of
CreateProcess.lpStartupInfoThis parameter is marked with the annotation
__in. This means that the information is being passed from the caller toCreateProcess. In that case it is the caller’s responsibility to ensure that all fields are suitably initialized. A simple and common way to do so is to initialize the entire structure to zero and then modify just the fields that you want to be different from zero.lpProcessInformationThis parameter is marked with the annotation
__out. This means that the information is being passed fromCreateProcessto the caller. Any initialization you perform before calling is ignored. The contract is thatCreateProcesswill not read the contents of the structure that you pass, but will fully initialize that structure. In such instances I believe it is pointless to zero initialize the structure before passing it toCreateProcess.Of course, if
CreateProcessfails then the values returned are undefined and you should not read them. But even if you have zero initialized the process information structure before calling, you have no guarantee thatCreateProcesswill not have partially written to it. In case an API function fails, you simply should not read anything returned by it, unless the documentation states otherwise.Summary
In summary I would advise you to initialize all the input values passed to a function, and not to initialize the output values.