如何跟踪或检测 USB 设备的格式化以及如何使用 C++ 知道它已完成格式化?

how to track or detect the formatting of a USB device and how to know it is completed formatting using c++?

我想创建一个用户权限的 EXE,可以检测 USB 设备的格式化,并通知设备开始格式化,完成后显示格式化完成的消息。

使用以下示例代码创建了一个 C++ 控制台应用程序。 但是需要管理员权限。

#include "stdafx.h"
#include <fstream>
#include <windows.h>
#include <iostream>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
   {
char *Fpath="D:\$Extend\$RmMetadata\$TxfLog\$TxfLog.blf";
std::ifstream is;
while(true)
{
    is.open(Fpath);
    if(is.is_open())
    {
        std::cout<<"Waiting for format\n";
        is.close();
    }
    else
    {
        std::cout<<"formating device\n";
    }
    Sleep(1000);
}
getchar();
return 0;
}

如果这个进程需要管理员权限,它需要它,没有选项。您可以设置 NTFS 权限(我不确定如何对 USB 物理文件执行此操作),或者您需要设置本地安全策略(再次不确定哪个 - 使用 secpol.msc 打开)。但我想你对此无能为力。

为了让你的进程拥有更高的权限(elevated privilege),你需要设置链接器选项:

I got the answer


#include "stdafx.h"
#include <fstream>
#include <windows.h>
#include <iostream>
#include <stdio.h>
using namespace std;


int _tmain(int argc, _TCHAR* argv[])
{
    while(true)
    {
        WIN32_FIND_DATA data;
        HANDLE h = FindFirstFile(L"D:\*.*",&data); // specify the drive letter

        if( h!=INVALID_HANDLE_VALUE ) 
        {
            cout << "Waiting\n";
        } 
        else 
            cout << "Formatting\n";
        FindClose(h);
        Sleep(1000);
    }
    getchar();
    return 0;
}