API Windows WriteFile: works but returns 0
Jul 4, 2018 at 7:54am Jul 4, 2018 at 7:54am UTC
Hello
I wrote a method that does nearly well it's job (except it returns an error) : The function write an image file to a USB key partition with the function WriteFile. The USB key is well formated with the image file but WriteFile returns 0 (false) and the written Bytes stay at 0,
I use QT for reading the file and because of the rest of the code was not made by me, I must use win32 lib .
the read file contains a lot of "\0"
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
boolean MyClass::writeFromFile(QString filepath)
{
Log *log = Log::getInstance();
qDebug() << "Openning file " << filepath;
QFile file(filepath);
if (!file.open(QIODevice::ReadOnly))
{
qDebug() << "Error: File " << filepath << " not found" ;
log->info("Error: File " + filepath + " not found" );
return false ;
}
qDebug() << "File " << filepath << " found" ;
log->info("File " + filepath + " found" );
QByteArray buffer = file.readAll();
file.close();
openBatDevice();
int retData = SetFilePointer(_batDevice, 0, NULL, FILE_BEGIN);
if (retData == 0xFFFFFFF)
{
qDebug() << "Error: SetFilePointer is 0xFFFFFFF" ;
log->info("Error: SetFilePointer is 0xFFFFFFF" );
return false ;
}
DWORD nbOfByteWr;
LPDWORD nNumberOfBytesToWrite;
BYTE* data = (BYTE*) buffer.data();
DWORD dwLen = strlen(buffer.data());
DWORD dwBytesRead = (DWORD) buffer.size();
qDebug() << "INFO buffer data: " << buffer.data();
qDebug() << "INFO buffer size: " << buffer.size();
BOOL out = WriteFile(_batDevice, data, dwBytesRead, &nbOfByteWr, NULL);
qDebug() << "INFO out: " << out;
qDebug() << "INFO nbOfByteWr: " << nbOfByteWr;
int errorId = (int ) GetLastError();
qDebug() << "Error WriteSector : " + QString::number(errorId);
qDebug() << "Error WriteSector : " + errorId;
}
Returns:
1 2 3 4 5 6 7 8
Openning file "C:/Users/user/Documents/Projet/Project/Outils_project/projectExe/debug/clef_XXX.img"
File "C:/Users/user/Documents/Projet/Project/Outils_project/projectExe/debug/clef_XXX.img" found
INFO buffer data:
INFO buffer size: 4198400
INFO out: 0
INFO nbOfByteWr: 0
Error WriteSector :
Error WriteSector :
ReadFile(_batDevice, buffer, toRead, &read, NULL)
News:
Here is how the Handle is made
_batDevice = CreateFile(devicePath, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0, NULL);
In the documentation [URL="
https://docs.microsoft.com/en-us/windows/desktop/api/fileapi/nf-fileapi-createfilea"]https://docs.microsoft.com/...[/URL] I read we need to use the flag FILE_FLAG_OVERLAPPED for beeing Asynchronous , so I anderstand it is synchronous, isn't it?
Thanks a lot
Last edited on Jul 4, 2018 at 9:46am Jul 4, 2018 at 9:46am UTC
Jul 4, 2018 at 8:28am Jul 4, 2018 at 8:28am UTC
The write didn't work, it returned FALSE.
Jul 4, 2018 at 8:37am Jul 4, 2018 at 8:37am UTC
Yes and no :
Yes: because the USB key is well written (the written data are good)
No: WriteFile returns FALSE, and I don't know why
Jul 4, 2018 at 12:41pm Jul 4, 2018 at 12:41pm UTC
That's a contradiction. 0 bytes written, return value false means clearly WriteFile didn't work.
Maybe the written data was there before.
Delete it and run the code again.
WriteFile returns FALSE, and I don't know why
To get an error msg you can use this code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
std::string GetLastWinError ()
{
LPVOID lpMsgBuf;
LPVOID lpDisplayBuf;
DWORD dw = GetLastError ();
FormatMessage (FORMAT_MESSAGE_ALLOCATE_BUFFER |FORMAT_MESSAGE_FROM_SYSTEM,
NULL, dw, MAKELANGID (LANG_NEUTRAL, SUBLANG_DEFAULT), (LPTSTR)&lpMsgBuf, 0, NULL);
std::ostringstream oss;
oss << "Windows Error " ;
oss << dw << " :" << (LPCSTR)lpMsgBuf;
LocalFree (lpMsgBuf);
return oss.str();
Jul 4, 2018 at 5:11pm Jul 4, 2018 at 5:11pm UTC
WriteFile returns FALSE, and I don't know why
That's what GetLastError() is for.
Topic archived. No new replies allowed.