WriteFile

Hello,

Does anyone see anything glarring as to why this writefile command gives me this error when compiling:

53 100 C:\EZInstaller\DevFiles\main.cpp [Error] invalid conversion from 'const void*' to 'HANDLE {aka void*}' [-fpermissive]

This is the code I am using it's from msdn website but does not allow me to compile:

1
2
3
4
char DataBuffer[] = "This is some test data to write to the file."; 
    DWORD dwBytesToWrite = (DWORD)strlen(DataBuffer);
    DWORD dwBytesWritten = 0;
    HANDLE hFile = WriteFile("E:\\StateFile.txt", DataBuffer, dwBytesToWrite, &dwBytesWritten, NULL);  
I've not used that function myself.
But from a glance at the MSDN page here;
http://msdn.microsoft.com/en-us/library/windows/desktop/bb540534%28v=vs.85%29.aspx
it looks as though your code has somehow mixed the CreateFile and WriteFile.


1
2
3
HANDLE hFile    = CreateFile( ... etc.

BOOL bErrorFlag = WriteFile(hFile, .... etc.


It looks as though you need to pass the handle from the first function as a parameter of the second.
Last edited on
That ended up working, tried that before with that but it still gave me the error, but then I realized through the code you supplied that I had HANDLE instead of BOOL for the WriteFile, Thanks!
Ah. I know the problem. You cannot make hFile equal to WriteFile. WriteFile is of BOOL datatype, and HANDLE hFile, obviously, is a handle.

To solve this, you must create the file before writing to it, such as this:
1
2
3
HANDLE hFile = CreateFileA("E://StateFile.txt", GENERIC_WRITE, FILE_SHARE_WRITE, NULL, OPEN_EXISTING,NULL, NULL); 

WriteFile(hFile, DataBuffer, dwBytesToWrite, &dwBytesWritten, NULL);


Anyway, the parameters I used may not be right depending on what you plan on doing with the file. Also I used CreateFileA instead of CreateFile, because personally I hate using wide strings.

Link to CreateFile - http://msdn.microsoft.com/en-us/library/windows/desktop/aa363858(v=vs.85).aspx
Not CreateFileA, but same thing except first parameter is a char

Link to WriteFile - http://msdn.microsoft.com/en-us/library/windows/desktop/aa365747(v=vs.85).aspx
Yeah I didn't include my createfile script in the origional post because I didn't think it would effect the compiling of the writefile haha. Any idea how I can point to the write file further in my code? I am trying to record software installations that have occured within that txt.
Topic archived. No new replies allowed.