Click here to Skip to main content
15,868,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have read that lpNumberOfBytesWritten (the PDWORD) as it is used by

WINBASEAPI BOOL WINAPI WriteFile(HANDLE,PCVOID,DWORD,PDWORD,LPOVERLAPPED);

is automatically adjusted per value by the WriteFile process.

In the following code I would like to know if the CloseHandle(hDFile); tells the program to clear &NumberOfBytesWritten and remove it from memory automatically. Or, should I clear it before or after closing the file handle?

I create it with DWORD NumberOfBytesWritten;. Is it still there? Is it taking up space holding the number of bytes written. Should I put this entire bit of code into a function to ensure that it is cleared and gone?

Please explain with your answer.

What I have tried:

HANDLE hDFile = CreateFile(L"utf8_UsingByteOrderMark_C_天堂.txt", GENERIC_READ | GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);

DWORD NumberOfBytesWritten;

BOOL bErr2 = 0;
unsigned char BOM2[3]{ 0xef, 0xbb, 0xbf };

bErr2 = WriteFile(hFile, (LPCVOID)BOM2, (DWORD)sizeof(BOM2), &dwBytesWritten, NULL);

bErr2 = WriteFile(hDFile, L"hello - J - こんにちは - abcdefghijklmnopqrstuvwxyz", 90, &NumberOfBytesWritten, NULL);

CloseHandle(hDFile);
Posted
Updated 29-Jun-22 20:28pm
v3

1 solution

In your code, the WriteFile call sets the value of NumberOfBytesWritten.
Afterwards, NumberOfBytesWritten maintains its value, unless you explicitely change it (or call again WriteFile).
The CloseHandle (as you may easily infer from its signature), has nothing to do with it.
 
Share this answer
 
Comments
Member 15078716 30-Jun-22 13:08pm    
@CPallini, Thank you.

I think that I shall place the CreateFile, with the WriteFile, etc. all in a function so that NumberOfBytesWritten is gone when the function closes. That is what I thought to do, but I wanted to see what others thought of that. Thank you.


Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900