启动遍历文件夹中的所有指定的文件 ( 执行exe文件 或者加载dll )

来源:互联网 发布:软件开发资讯 编辑:程序博客网 时间:2024/06/05 03:48

遍历文件夹中的文件  执行exe文件 或者加载dll

 

 HMODULE hModule = ::GetModuleHandle(NULL);

 char sFileName[256] = {0};
 CString sPath = _T("");

 GetModuleFileName(hModule, sFileName, 255);

 sPath.Format("%s", sFileName);
 int pos = sPath.ReverseFind('\\');
 if(pos != -1)
  sPath = sPath.Left(pos);
 else
  sPath = _T("");

 sPath += "\\plugin";
 TraverseFiles(sPath);

 

 

 

void TraverseFiles(CString strDelDir)
{
 CFileFind ff;
 CString strDir, strFile;

 strDir = strDelDir;

 if ( strDir.Right(1) != "//" )
  strDir += "//";

 strDir += "*.*";

 BOOL bFind = ff.FindFile(strDir);
 while ( bFind )
 {
  bFind = ff.FindNextFile();
  if ( ff.IsDots() )
   continue;
  CString strFileName = ff.GetFileName();

  strFile = strDelDir;
  if ( strFile.Right(1) != "\\" )
   strFile += "\\";
  strFile += strFileName;
  if ( ff.IsDirectory() )
   TraverseFiles(strFile);
  else
  {
   if ( ff.IsReadOnly() )
    SetFileAttributes(strFile, GetFileAttributes(strFile)&(~FILE_ATTRIBUTE_READONLY) );

   CString strExtend = strFile.Right(strFile.GetLength()-strFile.ReverseFind('.')-1);
   if (strExtend == "exe")//或者dll
   {
    STARTUPINFO si; //一些必备参数设置
    memset(&si, 0, sizeof(STARTUPINFO));
    si.cb = sizeof(STARTUPINFO);
    si.dwFlags = STARTF_USESHOWWINDOW;
    si.wShowWindow = TRUE;
    PROCESS_INFORMATION pi; //必备参数设置结束
    if(!CreateProcess(strFile,NULL,NULL,NULL,FALSE,CREATE_NEW_CONSOLE,NULL,NULL,&si,&pi))
    {
     cout<<"创建失败@"<<endl;
    }
    ::CloseHandle (pi.hThread);
    ::CloseHandle (pi.hProcess);
   }
  }
 }
 ff.Close();

}