Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WriteFile function C++

Im trying to use the WriteFile function. Ive been working off this example

http://msdn.microsoft.com/en-us/library/ms900134.aspx

Here the buffer that is passed to WriteFile is filled from ReadFile. But I dont want to do it that way. I just want to write a string like "Example text testing WriteFile" or something. But im not sure what values the parameters should have. Ive tried looking around on google but couldnt find anything. Anyone know how i do this?

like image 647
discodowney Avatar asked Jan 18 '26 04:01

discodowney


1 Answers

From MSDN:

BOOL WINAPI WriteFile(
  __in         HANDLE hFile,
  __in         LPCVOID lpBuffer,
  __in         DWORD nNumberOfBytesToWrite,
  __out_opt    LPDWORD lpNumberOfBytesWritten,
  __inout_opt  LPOVERLAPPED lpOverlapped
);
  • The first argument is the handle to the file.
  • The second argument is a pointer to the data you want to write. In your case it's the string.
  • The third argument is the length of the data you want to write. In your case it will be something like strlen(str).
  • The fourth argument is a pointer to a DWORD variable that will receive the number of bytes actually written.
  • The fifth and last parameter can be NULL for now.

You use it like this:

char str[] = "Example text testing WriteFile";
DWORD bytesWritten;

WriteFile(fileHandle, str, strlen(str), &bytesWritten, NULL);

If WriteFile returns FALSE then there was an error. Use the GetLastError function to find out the error code.

like image 107
Some programmer dude Avatar answered Jan 19 '26 18:01

Some programmer dude



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!