#include "stdafx.h"#include <stdlib.h>#include <iostream>#include <io.h>#include <windows.h> #include <string>#include <vector>#include <iomanip>#define MY_NOT_CHECK 1;#define MY_CHECKED 0;using namespace std;int main(){ string path("c:\\Windows\\system32"), exd("exe"); vector<WIN32_FIND_DATA> files; void getFiles(string path, string exd, vector<WIN32_FIND_DATA>& files); getFiles(path, exd, files); sort(files); show(files);}/*遍历文件夹,得到所有exe文件*/void getFiles(string path, string exd, vector<WIN32_FIND_DATA>& files){ /************************************************************************/ /* 获取文件夹下所有文件名 输入: path : 文件夹路径 exd : 所要获取的文件名后缀,如jpg、png等; 文件名, exd = "" 输出: files : 获取的文件名列表 */ /************************************************************************/ BOOL SetPrivilege(HANDLE hToken, LPCTSTR lpszPrivilege, BOOL bEnablePrivilege); //文件句柄 HANDLE hFile = INVALID_HANDLE_VALUE,hExe = INVALID_HANDLE_VALUE; //文件信息 WIN32_FIND_DATA fileinfo,exeinfo; string pathName, exdName = "\\*"; PVOID OldValue = NULL; // 关闭系统重定向 if (Wow64DisableWow64FsRedirection(&OldValue)) { // 查找指定路径 hFile = FindFirstFile(pathName.assign(path).append(exdName).c_str(), &fileinfo); if (FALSE == Wow64RevertWow64FsRedirection(OldValue)) {return; } } // 查找指定路径 // hFile = FindFirstFile(pathName.assign(path).append(exdName).c_str(), &fileinfo); // 是否查找失败 if (hFile == INVALID_HANDLE_VALUE) { // 是否因为权限不足,若不足,则提升权限 if (GetLastError() == 5) {HANDLE hToken;BOOL bRet = OpenProcessToken( GetCurrentProcess(), // 进程句柄(当前进程) TOKEN_ALL_ACCESS, // 全权访问令牌 &hToken // 返回的参数 进程令牌句柄 (就是AdjustTokenPrivileges的第一个参数) ); // 获取进程的令牌句柄if (bRet != TRUE){ cout << "获取令牌句柄失败!" << endl; return;}BOOL set = SetPrivilege(hToken, SE_DEBUG_NAME, TRUE);if (!set || GetLastError() != ERROR_SUCCESS) { // 设置权限失败 cout << "提升权限失败 error:" << GetLastError() << endl; cout << "此文件夹缺少权限访问: " << pathName.assign(path).append("\\").c_str() << endl; return;}// 权限设置成功,继续执行hFile = FindFirstFile(pathName.assign(path).append(exdName).c_str(), &fileinfo);cout << "权限设置完成" << endl;cout << GetLastError()<<endl; } else {// 不是权限问题cout << "FindFirstFile failed " << GetLastError() << endl;system("pause");return; } } int flag = MY_NOT_CHECK; int lastError = 0; // 遍历 do { //如果是文件夹,迭代之 if ((fileinfo.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {if (strcmp(fileinfo.cFileName, ".") != 0 && strcmp(fileinfo.cFileName, "..") != 0) getFiles(pathName.assign(path).append("\\").append(fileinfo.cFileName), exd, files); } else {//如果不是文件夹/*if (strcmp(fileinfo.cFileName, ".") != 0 && strcmp(fileinfo.cFileName, "..") != 0){ for (int i = 0; fileinfo.cFileName[i + 3] != '\0'; i++) { // 判断是否exe if (fileinfo.cFileName[i] == '.'&& (fileinfo.cFileName[i + 1] == 'e' || fileinfo.cFileName[i + 1] == 'E')&& (fileinfo.cFileName[i + 2] == 'x' || fileinfo.cFileName[i + 2] == 'X')&& (fileinfo.cFileName[i + 1] == 'e' || fileinfo.cFileName[i + 1] == 'E')&& fileinfo.cFileName[i + 4] == '\0') {files.push_back(fileinfo);break; } }}*/// 如果当前目录还未查找过,查找当前目录的exe文件if (flag) { // 关闭系统重定向 if (Wow64DisableWow64FsRedirection(&OldValue)) { // 查找指定路径 hExe = FindFirstFile(pathName.assign(path).append("\\*." + exd).c_str(), &exeinfo); if (FALSE == Wow64RevertWow64FsRedirection(OldValue)) {return; } } // hExe = FindFirstFile(pathName.assign(path).append("\\*.exe").c_str(), &exeinfo); if (hExe == INVALID_HANDLE_VALUE) { lastError = GetLastError(); if (lastError == 2) {//cout << setiosflags(ios::left) << setw(50) << path << " 此目录下没有exe " << endl; } else {cout << " 查找exe失败 " << lastError << endl;return; } } // 遍历所有本文件夹下的exe文件 if (lastError != 2) { do {files.push_back(exeinfo); } while (FindNextFile(hExe, &exeinfo)); // 查找完成,此目录已不用遍历,跳出 flag = MY_CHECKED; FindClose(hExe); }} } } while (FindNextFile(hFile, &fileinfo)); FindClose(hFile); return;}