#include <iostream>
#include <fstream>
#include <windows.h>
#include <string.h>
#include <list>
#include <process.h>
#include <WinBase.h>
using namespace std;
list<string>* m_sList;
list<HANDLE>* m_hList;
string ChangeName(const char* pName)
{
//rename;
string tmp = pName;
int i = tmp.find("\\");
string tmpCut = tmp.substr(0, i);
int j = tmpCut.find("_");
string tmpDoubleCut = tmpCut.substr(j+1, i);
string reProName = "";
reProName = tmpDoubleCut.append(".exe");
string newName = tmpCut;
newName.append("\\\\");
newName.append(reProName);
return newName;
}
void MyCopyExe(const char* pName)
{
BOOL bSuccess;
//rename;
//string tmp = pName;
//int i = tmp.find("\\");
//string tmpCut = tmp.substr(0, i);
//int j = tmpCut.find("_");
//string tmpDoubleCut = tmpCut.substr(j+1, i);
//string reProName = "";
//reProName = tmpDoubleCut.append(".exe");
//string newName = tmpCut;
//newName.append("\\\\");
//newName.append(reProName);
string newName = ChangeName(pName);
//copy;
bSuccess = CopyFileA((LPCSTR)pName, (LPCSTR)(char*)newName.c_str(), FALSE);
if (!bSuccess)
{
cout<< " copy fail!";
exit(1);
}
}
void MyReadFile()
{
ifstream myfile("cfg.txt");
if (!myfile)
{
cout<< " Unable open";
exit(1);
}
while (!myfile.eof())
{
char line[1024] = {0};
myfile.getline(line, sizeof(line));
m_sList->push_back(line);
//copy file;
MyCopyExe(line);
}
}
void MyCreateProcess(const char* proName)
{
//rename;
string tmp = proName;
int i = tmp.find("\\");
string tmpCut = tmp.substr(0, i);
int j = tmpCut.find("_");
string tmpDoubleCut = tmpCut.substr(j+1, i);
string reProName = "";
reProName = tmpDoubleCut.append(".exe");
string newName = ChangeName(proName);
STARTUPINFOA si;
memset(&si, 0, sizeof(STARTUPINFO));
si.cb = sizeof(STARTUPINFO);
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = SW_SHOW;
//rename;
si.lpTitle = (LPSTR)(char*)reProName.c_str();
PROCESS_INFORMATION pi;
BOOL bCreate = CreateProcessA(
//(LPCSTR)(char*)reProName.c_str(),
NULL,
(LPSTR)(char*)newName.c_str(),
NULL,
NULL,
FALSE,
CREATE_NEW_CONSOLE,
NULL,
NULL,
&si,
&pi);
if (bCreate)
{
cout<< "Success!"<<endl;
m_hList->push_back(pi.hProcess);
}
else
{
cout<<"Create fail!"<<endl;
exit(1);
}
}
int main()
{
m_sList = new list<string>();
m_hList = new list<HANDLE>();
list<string>::iterator ls;
list<HANDLE>::iterator lh;
//!读文件中的路径F:\\work\\SVN\\apps\\Debug\\GTADebug.exe;
MyReadFile();
//char* str = "F:\\work\\SVN\\20120515\\GFdemo\\Debug\\DllLoadTest.exe";
//char* processName = "C:\\windows\\system32\\notepad.exe";
for (ls = m_sList->begin(); ls != m_sList->end(); ++ls)
{
char* processName = (char*)ls->c_str();
//!创建进程;
MyCreateProcess(processName);
}
char cmd[20];
cout<<"输入q终止进程!"<<endl;
while (TRUE)
{
cin>>cmd;
if (!strcmp(cmd,"q"))
{
for (lh = m_hList->begin(); lh != m_hList->end(); ++lh)
{
TerminateProcess((HANDLE)*lh, 1);
}
break;
}
}
//F:\\work\\SVN\\20120515\\GFdemo\\Debug\\DllLoadTest.exe
return 0;
}