📄 exefinder.cpp
字号:
#include <string>
#include <windows.h>
#include <fstream>
#include <vector>
using namespace std;
#include "ExeFinder.h"
#include "Globals.h"
#ifdef WRITE_PROGRESS_TO_CONSOLE
#include <iostream>
#endif
LPSTR ExeFinder::LPCWSTRtoLPSTR(LPCWSTR string)
/*
Description: Converts a constant unicode string to a windows format ascii string
*/
{
if (string==NULL) return NULL;
int cw=lstrlenW(string);
if (cw==0) {CHAR *psz=new CHAR[1];*psz='\0';return psz;}
int cc=WideCharToMultiByte(CP_ACP,0,string,cw,NULL,0,NULL,NULL);
if (cc==0) return NULL;
CHAR *psz=new CHAR[cc+1];
cc=WideCharToMultiByte(CP_ACP,0,string,cw,psz,cc,NULL,NULL);
if (cc==0) {delete[] psz;return NULL;}
psz[cc]='\0';
return psz;
}
LPWSTR ExeFinder::LPSTRtoLPWSTR(LPCSTR string)
/*
Description: Converts converts a windows format ascii string to a unicode string
*/
{
if (string==NULL) return NULL;
int cw=strlen(string);
if (cw==0) {TCHAR *psz=new TCHAR[1];*psz='\0';return psz;}
int cc=MultiByteToWideChar(CP_ACP, 0, string, -1, NULL, NULL);
if (cc==0) return NULL;
TCHAR *psz=new TCHAR[cc+1];
cc=MultiByteToWideChar(CP_ACP,0,string,cw,psz,cc);
if (cc==0) {delete[] psz;return NULL;}
psz[cc]='\0';
return psz;
}
string ExeFinder::capitalize(string s)
{
/*
Description: Capitalize a string
*/
for(unsigned int i=0;i<s.size();i++)
s[i] = toupper( s[i] );
return s;
}
int ExeFinder::findAllExecutables( string sDir, bool firstRun, bool findExe, bool findDll )
{
/*
Description: THIS FUNCTION IS RECURSIVE ( i.e. it calls itself ).
Finds all executables in a given directory. If the directory given is the root directory,
this function will recurse through the entire filesystem. The first parameter is the only one that changes
between recursions, as it is the directory currently being searched. The second parameter should always
be true on the first call to this function. Its truth will reset all the data structures which contain
information that the function collects. If firstRun is not true when the function is called, and the function
is called more than once, data that this function generates WILL BE CORRUPTED. The third parameter to the function
specifies whether the function will report findings of executable files. And the fourth parameter does the same
for dll files.
*/
WIN32_FIND_DATA findData;
LPWSTR wsDir = LPSTRtoLPWSTR( (sDir+"\\*.*").c_str() );
HANDLE hFind = FindFirstFile( wsDir, &findData );
string::size_type loc1, loc2;
static long exeCount;
if( firstRun ) //reset data
{
exeCount = 0;
FileNames.erase( FileNames.begin(), FileNames.end() );
}
if (hFind == INVALID_HANDLE_VALUE) {
return - 1;
}
do {
wstring swFileName(findData.cFileName);
string sFileName(LPCWSTRtoLPSTR(swFileName.c_str()));
if (findData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
if (sFileName != "." && sFileName != "..")
{
findAllExecutables(sDir+"\\"+sFileName, false, findExe, findDll);// recurse into subdirectory
}
}
else
{
loc1 = capitalize(sFileName).find(".EXE");
loc2 = capitalize(sFileName).find(".DLL");
if( (findExe && (loc1 != string::npos) && loc1 == (sFileName.size() - 4)) ||
(findDll && (loc2 != string::npos) && loc2 == (sFileName.size() - 4)) )
{
FileNames.push_back( sDir+"\\"+sFileName );
#ifdef WRITE_PROGRESS_TO_CONSOLE
cout << sDir+"\\"+sFileName << endl;
#endif
exeCount++;
}
}
} while (FindNextFile(hFind, &findData));
return exeCount;
}
vector<string> ExeFinder::getExecutableVector( void )
/*
Description: returns the vector with all the filenames in it
*/
{
return FileNames;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -