从文件中读取要启动的进程,调用API启动该进程

本文介绍了一个C++程序,该程序能够从配置文件中读取多个程序路径,并为每个程序创建新的副本然后启动它们。通过字符串处理和Windows API函数实现了程序复制与进程创建的功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#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;
}

转载于:https://www.cnblogs.com/TeaWater/archive/2012/06/26/2563310.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值