C++语言中,int __cdecl main(int argc, char* argv[]) { int status = -1; char…
- User
C++语言中,int __cdecl main(int argc, char* argv[]) { int status = -1; char device_base_path[MAX_PATH + 1] = ""; DWORD num_devices = get_devices(GUID_DEVINTERFACE_XDMA, device_base_path, sizeof(device_base_path)); verbose_msg("Devices found: %d\n", num_devices); if (num_devices < 1) { goto Exit; } if (!parse(argc, argv)) { goto Exit; } verbose_msg("Device base path: %s\n", device_base_path); char device_path[MAX_PATH + 1] = ""; strcpy_s(device_path, sizeof device_path, device_base_path); strcat_s(device_path, sizeof device_path, "\"); strcat_s(device_path, sizeof device_path, options.device); verbose_msg("Device node: %s\n", options.device); HANDLE device = CreateFile(device_path, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (device == INVALID_HANDLE_VALUE) { fprintf(stderr, "Error opening device, win32 error code: %ld\n", GetLastError()); goto Exit; } if (INVALID_SET_FILE_POINTER == SetFilePointerEx(device, options.address, NULL, FILE_BEGIN)) { fprintf(stderr, "Error setting file pointer, win32 error code: %ld\n", GetLastError()); goto CleanupDevice; }
LARGE_INTEGER start; LARGE_INTEGER stop; LARGE_INTEGER freq; QueryPerformanceFrequency(&freq); if (options.direction == C2H) { verbose_msg("reading from device...\n"); // allocate and zero-initialize memory to hold read bytes options.data = allocate_buffer(options.size, options.alignment); if (!options.data) { fprintf(stderr, "Error allocating %ld bytes of memory, error code: %ld\n", options.size, GetLastError()); goto CleanupDevice;}memset(options.data, 0, options.size); QueryPerformanceCounter(&start); if (!ReadFile(device, options.data, options.size, &options.size, NULL)) { fprintf(stderr, "ReadFile from device %s failed with Win32 error code: %ld\n", device_path, GetLastError()); goto CleanupDevice; } QueryPerformanceCounter(&stop); if (options.binary == TRUE) { print_bytes_binary(options.data, options.size); } else { print_bytes(options.address.QuadPart, options.data, options.size); }
double time_sec = (unsigned long long)(stop.QuadPart - start.QuadPart) / (double)freq.QuadPart; printf("%ld bytes received in %fs\n", options.size, time_sec); } else { verbose_msg("writing to device...\n"); if (options.file) { FILE* inputFile; if (fopen_s(&inputFile, options.file, "rb") != 0) { fprintf(stderr, "Could not open file <%s>\n", options.file); goto CleanupDevice; } if (options.size == 0) { fseek(inputFile, 0, SEEK_END); fpos_t fpos; fgetpos(inputFile, &fpos); fseek(inputFile, 0, SEEK_SET); options.size = (DWORD)fpos; } options.data = allocate_buffer(options.size, options.alignment); if (!options.data) { fprintf(stderr, "Error allocating %ld bytes of memory, error code: %ld\n", options.size, GetLastError()); goto CleanupDevice; } options.size = (DWORD)fread(options.data, 1, options.size, inputFile); fclose(inputFile); printf("%ld bytes read from file %s\n", options.size, options.file); } if (options.data == NULL) { printf("Error! No valid data given!\n"); usage(argv[0]); goto CleanupDevice; } QueryPerformanceCounter(&start); if (!WriteFile(device, options.data, options.size, &options.size, NULL)) { fprintf(stderr, "WriteFile to device %s failed with Win32 error code: %d\n", device_path, GetLastError()); goto CleanupDevice; } QueryPerformanceCounter(&stop); if (options.verbose) { print_bytes(options.address.QuadPart, options.data, options.size); } double time_sec = (unsigned long long)(stop.QuadPart - start.QuadPart) / (double)freq.QuadPart; printf("%ld bytes written in %fs\n", options.size, time_sec); } status = 0;CleanupDevice: CloseHandle(device); Exit: if (options.device) free(options.device); if (options.file) free(options.file); if (options.data) _aligned_free(options.data); return status; } 的意思
- Assistant
这是一个C++程序的入口函数
main,以int类型作为返回值。它接受两个参数,argc表示命令行参数的数量,argv是一个指向字符指针数组的指针,表示命令行参数的值。函数开始时定义了一个名为
status的整型变量,初始值为 -1,用于记录程序的执行状态。接着定义了一个长度为
MAX_PATH + 1的字符数组device_base_path,并将其初始化为空字符串。MAX_PATH是一个宏定义,表示系统中文件路径的最大长度。调用函数
get_devices获取设备接口为GUID_DEVINTERFACE_XDMA的设备的数量,并将结果存储在变量num_devices中。然后通过调用verbose_msg函数打印出设备数量。如果设备数量小于 1,则跳转到标签
Exit处,即直接退出程序。接着调用函数
parse对命令行参数进行解析,如果解析失败,则跳转到标签Exit处,退出程序。通过调用
strcpy_s和strcat_s函数将设备路径拼接完成,并通过调用verbose_msg函数打印出设备路径。接下来调用
CreateFile函数打开设备,以进行读写操作。如果打开设备失败,则通过fprintf函数打印出错误信息,并跳转到标签Exit处,退出程序。如果成功打开设备,则通过调用
SetFilePointerEx函数设置文件指针的位置。如果设置文件指针失败,则通过fprintf函数打印出错误信息,并跳转到标签CleanupDevice处,执行设备清理操作。接下来定义了三个
LARGE_INTEGER类型的变量start、stop和freq,用于计算读写操作的时间。如果
options.direction的值为C2H,即从设备读取数据,则打印出提示信息。通过调用
allocate_buffer函数分配一块内存用于存储读取的数据,并通过memset函数将内存初始化为零。通过调用
QueryPerformanceCounter函数获取当前时钟周期的值,并将结果存储在start变量中。调用
ReadFile函数从设备中读取数据到options.data缓冲区中,如果读取失败,则通过fprintf函数打印出错误信息,并跳转到标签CleanupDevice处,执行设备清理操作。再次调用
QueryPerformanceCounter函数获取当前时钟周期的值,并将结果存储在stop变量中。根据
options.binary的值,选择是以二进制格式打印读取的数据,还是以正常的格式打印。计算读取数据的时间,并以秒为单位打印出读取的字节数和时间。
如果
options.direction的值为其他值,则打印出提示信息。如果指定了输入文件
options.file,则打开文件并从文件中读取数据到options.data缓冲区中。如果打开文件或读取数据失败,则通过fprintf函数打印出错误信息,并跳转到标签CleanupDevice处,执行设备清理操作。如果没有指定有效的数据,则打印出错误信息,并调用
usage函数打印出程序的用法。通过调用
QueryPerformanceCounter函数获取当前时钟周期的值,并将结果存储在start变量中。调用
WriteFile函数将options.data缓冲区中的数据写入设备。如果写入失败,则通过fprintf函数打印出错误信息,并跳转到标签CleanupDevice处,执行设备清理操作。再次调用
QueryPerformanceCounter函数获取当前时钟周期的值,并将结果存储在stop变量中。根据
options.verbose的值,选择是否以正常的格式打印写入的数据。计算写入数据的时间,并以秒为单位打印出写入的字节数和时间。
将
status的值设为0,表示程序执行成功。执行设备清理操作,即关闭设备的句柄。
最后通过
goto语句跳转到标签Exit处,在退出之前释放动态分配的内存,并返回status的值作为程序的退出码。 - User
QueryPerformanceFrequency(&freq);
- Assistant
QueryPerformanceFrequency是 Windows API 中的一个函数,用于获取计时器的频率。它接受一个LARGE_INTEGER类型的指针参数freq,用于存储计时器的频率。在这段代码中,
QueryPerformanceFrequency(&freq)的作用是获取计时器的频率,并将结果存储在freq变量中,以便后续计算时间的精确性。 - User
if (options.file) { //如果指定了输入文件options.file,则打开文件并从文件中读取数据到options.data缓冲区中。 FILE* inputFile; if (fopen_s(&inputFile, options.file, "rb") != 0) { fprintf(stderr, "Could not open file <%s>\n", options.file); goto CleanupDevice; }
/* determine file size */ if (options.size == 0) { fseek(inputFile, 0, SEEK_END); fpos_t fpos; fgetpos(inputFile, &fpos); fseek(inputFile, 0, SEEK_SET); options.size = (DWORD)fpos; } options.data = allocate_buffer(options.size, options.alignment); if (!options.data) { fprintf(stderr, "Error allocating %ld bytes of memory, error code: %ld\n", options.size, GetLastError()); goto CleanupDevice; } options.size = (DWORD)fread(options.data, 1, options.size, inputFile); fclose(inputFile); printf("%ld bytes read from file %s\n", options.size, options.file); } - Assistant
这段代码是用于处理指定了输入文件
options.file的情况。它打开文件并将文件中的数据读取到options.data缓冲区中。首先,代码定义了一个
FILE*类型的指针变量inputFile。然后,通过调用
fopen_s函数以只读二进制模式("rb")打开指定的文件options.file。如果打开文件失败(返回值不为零),则通过fprintf函数打印出错误信息,然后跳转到标签CleanupDevice处,执行设备清理操作。接下来,通过调用
fseek函数将文件指针移动到文件的末尾,然后通过fgetpos函数获取文件指针的位置,存储在fpos变量中。再次通过fseek函数将文件指针移动回文件的开头。如果之前没有指定
options.size的值,即默认为 0,则通过上述操作获取文件的大小,并将结果存储在options.size变量中。然后,调用
allocate_buffer函数分配一块大小为options.size的内存,并将其地址存储在options.data中。如果分配内存失败,则通过fprintf函数打印出错误信息,然后跳转到标签CleanupDevice处,执行设备清理操作。接下来,通过调用
fread函数从文件中读取options.size字节的数据到options.data缓冲区中,并将实际读取的字节数存储在options.size变量中。最后,调用
fclose函数关闭文件,并通过printf函数打印出从文件中读取的字节数以及文件的路径和名称。