2016-06-09 20 views
2

我想创建一个用户特权EXE,它可以检测USB设备的格式化,并通知设备已开始格式化,并且完成时它会显示消息格式化完成。如何跟踪或检测USB设备的格式以及如何使用C++完成格式化?

使用下面的示例代码创建了一个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; 
} 

回答

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

如果这个过程需要管理员权限,它需要它,没有选项。您可以设置NTFS权限(我不知道如何处理USB物理文件),或者您需要设置本地安全策略(再次确认哪一个 - 使用secpol.msc打开)。但我认为你不能做太多的事情。

为了使您的过程有更高的权限(提升的权限),你需要设置链接器选项:

enter image description here

+0

有它的任何其他的解决方案,而无需使用**管理员权限* *? –

+0

可能是。从哪里得到'D:\\ $ Extend \\ $ RmMetadata..'的想法? – Ajay

+0

http://answers.microsoft.com/en-us/windows/forum/windows_7-hardware/windows-system-process-locking-external-hdd/76170b41-c5e5-4cad-a20e-793d2a8b2b16 while formatting a drive该文件将被删除并在其后自动生成。 –