Reputation: 27544
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.
Upvotes: 0
Views: 276
Reputation: 612794
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
.
lpStartupInfo
This parameter is marked with the annotation __in
. This means that the information is being passed from the caller to CreateProcess
. 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.
lpProcessInformation
This parameter is marked with the annotation __out
. This means that the information is being passed from CreateProcess
to the caller. Any initialization you perform before calling is ignored. The contract is that CreateProcess
will 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 to CreateProcess
.
Of course, if CreateProcess
fails 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 that CreateProcess
will 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.
Upvotes: 3
Reputation: 80
Yes. A practice of filling structures with zeros is a part of wide-spreaded data preparing practice.
Upvotes: 0