使用writefile编写原始usb c++
use writefile to write raw usb c++
这是我的代码:
#include <string.h>
#include <malloc.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <Windows.h>
#include <stdio.h>
#include <iostream>
int main(int argc, CHAR* argv[]) {
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);
if (hFile == INVALID_HANDLE_VALUE) {
printf("Error %x", GetLastError());
return 1;
}
PVOID ToBe = "hello world from usb";
if (WriteFile(hFile,&ToBe,512,&dwBytesWrite, NULL) == 0)
{
printf("writeFile error: %x", GetLastError());
return 1;
}
while (ReadFile(hFile, &data, 512, &dwBytesRead, NULL) != 0) {
printf("%s", data);
}
if (ReadFile(hFile, &data, 512, &dwBytesRead, NULL) == 0) {
printf("ReadFile error: %x", GetLastError());
return 1;
}
return 0;
}
现在它看起来像在工作,但我看不到USB中的"你好世界"。。。我做错了什么?
(p.s.在我运行程序后,如果不格式化他,我就无法再次打开USB,所以确实写了一些东西,但在那之后,我无法用径向函数看到它…)
您写入512个字节,这将使文件指针前进512个字节。然后你从这一点开始阅读。相反,您需要将文件指针找回到开头。使用SetFilePointerEx
执行此操作。
相关文章:
- 是否可以使用winusb同时与多个相同的usb设备进行通信
- 插入或删除时获取usb的dos_name
- 使用 VID、PID、+SN 查找 USB 端口号
- USB传输的LibUSB C++格式不同
- 从 GUID 获取 USB 卷路径
- 如何在C++(VS2010)中设置超时读取USB端口?
- 如何在 MAC OS c/c++ 中阻止 USB 存储设备
- Android Java USB for native cpp
- mbed:使用 USB 文档库编译会导致错误
- 如何集成 HID USB 控制器?
- 在Windows(C++)中使用USB相机拍摄高分辨率照片
- 如何检测 USRP USB 类型?
- 我是否可以使用 win32 句柄以编程方式记录发送到/接收到 USB/COM 的内容
- 如何在Qt中使用Android上的USB OTG串行接口?
- 只需按一次按钮即可通过USB发送数据
- 适用于 macOS 的 Xcode 应用程序。这就是我设置从USB麦克风输入获取音频的方式。一年前工作,现在没有了。为什么
- 通过USB在PC和Arduino之间进行串行通信
- USB-HID 读/写(重叠)等待单个对象不返回C++
- 读取USB与操作系统、C++之间传输的数据
- 如何确定SSD驱动器是固定驱动器还是连接在USB上