c++ win32 启动进程并传命令行参数,杀死进程

获取当前dll路径

static HMODULE GetSelfModuleHandle()
{
	MEMORY_BASIC_INFORMATION mbi;
	return ((::VirtualQuery(GetSelfModuleHandle, &mbi, sizeof(mbi)) != 0) ? (HMODULE)mbi.AllocationBase : NULL);
}

std::string GetCurDllPath()
{
	WCHAR szModuleFileName[MAX_PATH];
	GetModuleFileName(GetSelfModuleHandle(), szModuleFileName, sizeof(szModuleFileName)/sizeof(WCHAR));
	printf("Dll path:%ws\n",szModuleFileName);

	char tempStr[MAX_PATH] = {0};
	sprintf(tempStr,"%ws",szModuleFileName);

	std::string fullPath(tempStr);
	size_t pos = fullPath.find_last_of("\\");
	std::string dllpath(fullPath.begin(),fullPath.begin()+pos);
	return dllpath;
}

启动进程,并把新进程的句柄保存下来

HWND g_winProcessHandle = NULL;

void CreateWinProcess(HWND handle)
{
	char cmdLine[1024]={0};

	sprintf(cmdLine,"%s\\WinWindow.exe -handle %lld",GetCurDllPath().c_str(),handle);

	WCHAR *wCmdLine=NULL;
	int wLen=0;

	ctow(cmdLine,wCmdLine,wLen);

	printf("wCmdLine wLen:%d,wCmdLine:%ws",wLen,wCmdLine);

	PROCESS_INFORMATION pro_info; //进程信息  
	STARTUPINFO sti; //启动信息  
	//......  
	// 初始化两个结构体  
	ZeroMemory(&pro_info, sizeof(PROCESS_INFORMATION));  
	ZeroMemory(&sti, sizeof(STARTUPINFO));  

	bool ret = CreateProcess(NULL,wCmdLine,NULL,NULL,false,NULL,NULL,NULL,&sti,&pro_info);
	if(!ret)
	{
		printf("CreateProcess failed,cmdLine:%s,error:%lld",cmdLine,GetLastError());
	}

	if(wCmdLine!=NULL)
		delete[] wCmdLine;

	g_winrtProcessHandle = (HWND)pro_info.hProcess;
}

根据子进程句柄杀死子进程

DWORD exitCode; //退出码  
  
GetExitCodeProcess(g_winProcessHandle,&exitCode); //获取退出码  
int ret = TerminateProcess(g_winProcessHandle, exitCode);
printf("TerminateProcess:ret:%d,last error:%lld",ret,GetLastError());

根据进程名称杀死进程

#include <windows.h>
#include <TlHelp32.h>
#include <iostream>

std::string WstringToString(const std::wstring wstr)
{
	std::string result;
	int len = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), NULL, 0, NULL, NULL);
	if( len <= 0 )
		return result;

	char* buffer = new char[len + 1];
	if(buffer == NULL )
		return result;

	WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), wstr.size(), buffer, len, NULL, NULL);
	buffer[len] = '\0';
	result.append(buffer);
	delete[] buffer;

	return result;
}

bool KillProcessEx(std::string processName)
{
	HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);

	PROCESSENTRY32 pe;
	pe.dwSize = sizeof(PROCESSENTRY32);

	if (!Process32First(hSnapShot, &pe))
	{
		return false;
	}

	while (Process32Next(hSnapShot, &pe))
	{
		std::wstring strTemp = pe.szExeFile;
		std::string strProcessTemp = WstringToString(strTemp);

		if(strcmp(processName.c_str(),strProcessTemp.c_str())==0)
		{
			DWORD dwProcessID = pe.th32ProcessID;
			HANDLE hProcess = ::OpenProcess(PROCESS_TERMINATE, FALSE, dwProcessID);
			::TerminateProcess(hProcess, 0);
			CloseHandle(hProcess);
		}
	}

	return true;
}

参考博客:https://blog.csdn.net/baidu_29198395/article/details/82926348?utm_medium=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.essearch_pc_relevant&depth_1-utm_source=distribute.pc_relevant_t0.none-task-blog-2%7Edefault%7EBlogCommendFromMachineLearnPai2%7Edefault-1.essearch_pc_relevant

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值