Listing:
[MS.Learn]: WriteFile function (fileapi.h)
BOOL WriteFile(
[in] HANDLE hFile,
[in] LPCVOID lpBuffer,
[in] DWORD nNumberOfBytesToWrite,
[out, optional] LPDWORD lpNumberOfBytesWritten,
[in, out, optional] LPOVERLAPPED lpOverlapped
);
[GitHub.MHammond]: win32file.WriteFile
int, int = WriteFile(hFile, data , ol)
2nd variant is a wrapper over the 1st.
2 differences between wrapped and wrapper:
Lack of nNumberOfBytesToWrite: in C, data is passed via a (void*) pointer whose size is unknown, that's why the argument is needed, while in Python, the buffer (char* - or bytes) size is already known (embedded into it if you will), so the argument would simply be redundant
Lack of lpNumberOfBytesWritten: in C, the return value is the function result status (success / failure) and the written bytes count is placed in an (output) argument, while in Python both of those values are returned by the function
So, the 2 functions are practically equivalent, the latter is simplified as it takes advantage of Python language features.
Here's a trivial example.
code00.py:
#!/usr/bin/env python
import sys
import win32con as wcon
import win32file as wfile
def main(*argv):
data = b"dummy text\n"
print("Data length:", len(data))
hf = wfile.CreateFile("out.txt", wcon.GENERIC_WRITE, 0, None, wcon.CREATE_ALWAYS, 0, None)
print("Handle:", hf)
rc, bwr = wfile.WriteFile(hf, data, None)
print("Return code: {:d}\nBytes Written: {:d}".format(rc, bwr))
wfile.CloseHandle(hf)
if __name__ == "__main__":
print("Python {:s} {:03d}bit on {:s}\n".format(" ".join(elem.strip() for elem in sys.version.split("\n")),
64 if sys.maxsize > 0x100000000 else 32, sys.platform))
rc = main(*sys.argv[1:])
print("\nDone.\n")
sys.exit(rc)
Output:
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q072945994]> sopr.bat
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###
[prompt]> dir /b
code00.py
[prompt]> "e:\Work\Dev\VEnvs\py_pc064_03.09_test0\Scripts\python.exe" ./code00.py
Python 3.9.9 (tags/v3.9.9:ccb0e6a, Nov 15 2021, 18:08:50) [MSC v.1929 64 bit (AMD64)] 064bit on win32
Data length: 11
Handle: <PyHANDLE:580>
Return code: 0
Bytes Written: 11
Done.
[prompt]> dir /b
code00.py
out.txt
[prompt]> type out.txt
dummy text