简体   繁体   English

在原始 USB C++ 中写入文件

[英]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 .我尝试获取一个文件(在这种情况下为 mp4),并逐块读取它(当时为 512 位),获取该块并将其写入 USB 等等,直到文件结束。

Now, the problem is:现在,问题是:

A the loop never ends. A 循环永无止境。

B that it don't write the file to the USB, it looks like its write on the same spot again and again... B它不将文件写入USB,看起来它一次又一次地在同一个地方写入......

How can I fix it?我该如何解决?

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

The first time round the loop you set the file pointer to 50688 and write there.第一次循环时,您将文件指针设置为50688并在那里写入。 Each subsequent time round the loop you set the file pointer to 50688+512 and write there.每次循环后,您将文件指针设置为50688+512并在那里写入。

It looks like it writes to the same spot again and again.看起来它一次又一次地写到同一个地方。

Yes indeed.确实是的。 That's exactly what your code specifies.这正是您的代码指定的内容。 Your should set the file pointer on aFile outside the loop, and let it advance naturally as the file is written.您应该在循环外的aFile上设置文件指针,并在写入文件时让它自然前进。 Something like this:像这样的东西:

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

Note that your calls to LockFile , and the use of a DWORD for dwPos , means that you cannot write a file larger than 4GB.请注意,您对LockFile的调用以及对dwPos使用DWORD意味着您不能写入大于 4GB 的文件。

It is also far from clear to me that the calls to LockFile are needed.我也不清楚是否需要调用LockFile Since your original code got the handling of dwPos wrong, it's clear that you weren't locking the parts of the file you intended to.由于您的原始代码错误地处理了dwPos ,很明显您没有锁定您想要锁定的文件部分。 It is my belief that you should simply remove them.我相信你应该简单地删除它们。 In which case the code will become:在这种情况下,代码将变为:

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

You have also omitted large amounts of error checking in this code.您还省略了此代码中的大量错误检查。 I would not be surprised to find that there are a number of other problems with it.我不会惊讶地发现它还有许多其他问题。 I don't particularly want to try to find every single error in your code, and hope that what I have written is enough to help you on your way.我并不特别想尝试找出您代码中的每一个错误,希望我写的内容足以在您的道路上为您提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM