用raw usb c ++写文件

时间:2015-11-04 12:30:31

标签: c++ file winapi usb

这是我的代码:

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;
}

我尝试获取一个文件(在这种情况下为mp4),然后按块(当时为512位)读取它,取出块并将其写入usb,依此类推,直到文件结束。

现在,问题是:

循环永远不会结束。

B它没有将文件写入USB,看起来它一次又一次写在同一个地方......

我该如何解决?

1 个答案:

答案 0 :(得分:1)

LARGE_INTEGER a = { 50688 };
while (ReadFile(aFile, data,512, &dwBytesRead, NULL) && dwBytesRead > 0) 
{
    dwPos = SetFilePointerEx(hFile, a, NULL, 0); 
    LockFile(hFile, dwPos, 0, dwBytesRead, 0);
    WriteFile(hFile, data, 512, &dwBytesWrite, NULL);
    UnlockFile(hFile, dwPos, 0, dwBytesRead, 0);
    a = { 50688+512 };
}

第一次循环时,将文件指针设置为50688并写入。在循环的每个后续循环中,您将文件指针设置为50688+512并写入。

  

看起来它一次又一次地写到同一个地方。

确实是的。这正是您的代码指定的内容。您应该将文件指针设置在循环外的aFile上,并在文件写入时自然前进。像这样:

dwPos = 50688;
LARGE_INTEGER a = { dwPos };
if (!SetFilePointerEx(hFile, a, NULL, 0))
{
    // handle error
}
while (ReadFile(aFile, data, 512, &dwBytesRead, NULL) && dwBytesRead > 0) 
{
    LockFile(hFile, dwPos, 0, dwBytesRead, 0);
    WriteFile(hFile, data, 512, &dwBytesWrite, NULL);
    UnlockFile(hFile, dwPos, 0, dwBytesRead, 0);
    dwPos += 512;
}

请注意,您对LockFile的来电以及DWORDdwPos的使用,意味着您无法写入大于4GB的文件。

我还很清楚,需要拨打LockFile。由于您的原始代码处理了dwPos错误,因此您很清楚您没有锁定您想要的文件部分。我相信你应该简单地删除它们。在这种情况下,代码将变为:

LARGE_INTEGER a = { 50688 };
if (!SetFilePointerEx(hFile, a, NULL, 0))
{
    // handle error
}
while (ReadFile(aFile, data, 512, &dwBytesRead, NULL) && dwBytesRead > 0) 
{
    if (!WriteFile(hFile, data, 512, &dwBytesWrite, NULL))
    {
        // handle error
    }
}

您还在此代码中省略了大量错误检查。我发现它还存在许多其他问题,我不会感到惊讶。我并不特别想尝试查找代码中的每一个错误,并希望我写的内容足以帮助您。