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 
#include 
#include 

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

你可能感兴趣的:(C++,c++,windows)