mercredi 4 novembre 2015

write file in raw usb c++

this is my code:

int main(int argc, CHAR* argv[]) {

using namespace std;
PVOID data[1024];
DWORD dwBytesRead = 0;
DWORD dwBytesWrite = 512;

HANDLE hFile = CreateFile(L"\\\\.\\E:", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0);//open usb 
if (hFile == INVALID_HANDLE_VALUE) {
    printf("Error %x", GetLastError());
    return 1;
}
printf ("created usb hendle\n");
LARGE_INTEGER a = { 50688 };
SetFilePointerEx(hFile, a,NULL,0); //set the pointer to c600 
printf("got usb pointer set\n");
PVOID ToBe = ":) hello this is our file -> ";
if (WriteFile(hFile,ToBe,512 ,&dwBytesWrite,    NULL) == 0)
{
    printf("writeFile error: %x", GetLastError());
    CloseHandle(hFile);
    return 1;
}
printf("write the first string in isb\n");

HANDLE aFile = CreateFile(L"C:\\Users\\h7080y_dxlq\\Downloads\\Video\\88250.mp4", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, 0, OPEN_EXISTING, 0, 0); //open the file handle

printf("created mp4 hendle\n");
if (aFile == INVALID_HANDLE_VALUE) {
    printf("Error %x", GetLastError());
    return 1;
}
if (ReadFile(aFile, &data, 512, &dwBytesRead, NULL) == 0) {
    printf("ReadFile error: %x", GetLastError());
    return 1;
}
DWORD dwPos;
printf("checked for read errors in mp4 passed o.k.\n");
while (ReadFile(aFile, data,512, &dwBytesRead, NULL) && dwBytesRead > 0) //read file
{
    dwPos = SetFilePointerEx(hFile, a, NULL, 0); 
    LockFile(hFile, dwPos, 0, dwBytesRead, 0);
    WriteFile(hFile, data, 512, &dwBytesWrite, NULL); // write 512 bit chunk at the time to usb 
    UnlockFile(hFile, dwPos, 0, dwBytesRead, 0);
    a = { 50688+512 }; // promot 
}
printf("write all mp4 to the usb directtly\n");

ToBe = "<- this is the end of file , see you soon :)";
if (WriteFile(hFile, ToBe, 512, &dwBytesWrite, NULL) == 0)
{
    printf("writeFile error: %x", GetLastError());
    CloseHandle(hFile);
    return 1;
}

printf("after end massage \n");



CloseHandle(hFile);
system("pause");
return 0;
}

I try to take a file (mp4 in this case) , and read it chunk by chunk (512 bit at the time) , take the chunk and write it to usb and so on till end of file .

Now, the problem is:

A the loop never ends.

B that it don't write the file to the USB, it looks like its write on the same spot again and again...

How can I fix it?

Aucun commentaire:

Enregistrer un commentaire