Files
webcamservice/launcher/launcher.cpp
2024-12-15 16:18:16 +08:00

167 lines
4.9 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
// launcher.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//
#include <Windows.h>
#include <iostream>
#include <string>
#include <vector>
#include "Tlhelp32.h"
#pragma comment( linker, "/subsystem:windows /entry:mainCRTStartup" )
std::string UnicodeToAscii(const std::wstring& wstr) {
// 预算-缓冲区中多字节的长度
int ansiiLen = WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, nullptr, 0, nullptr, nullptr);
// 给指向缓冲区的指针变量分配内存
char* pAssii = (char*)malloc(sizeof(char) * ansiiLen);
// 开始向缓冲区转换字节
WideCharToMultiByte(CP_ACP, 0, wstr.c_str(), -1, pAssii, ansiiLen, nullptr, nullptr);
std::string ret_str = pAssii;
free(pAssii);
return ret_str;
}
// 通过进程ID终止进程
BOOL TerminateProcessFromID(DWORD dwID)
{
BOOL bRet = FALSE;
// 打开进程
HANDLE hProcess = ::OpenProcess(PROCESS_ALL_ACCESS, FALSE, dwID);
if (hProcess != NULL)
{
// 终止进程
bRet = ::TerminateProcess(hProcess, 0);
}
// 关闭进程句柄
::CloseHandle(hProcess);
return bRet;
}
BOOL ExecuteAppFromLocal(char* path)
{
STARTUPINFOA si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
ZeroMemory(&pi, sizeof(pi));
//创建一个新进程
if (CreateProcessA(
NULL, // 指向一个NULL结尾的、用来指定可执行模块的宽字节字符串
path, // 命令行字符串
NULL, // 指向一个SECURITY_ATTRIBUTES结构体这个结构体决定是否返回的句柄可以被子进程继承。
NULL, // 如果lpProcessAttributes参数为空NULL那么句柄不能被继承。<同上>
FALSE,// 指示新进程是否从调用进程处继承了句柄。
0, // 指定附加的、用来控制优先类和进程的创建的标
// CREATE_NEW_CONSOLE 新控制台打开子进程
// CREATE_SUSPENDED 子进程创建后挂起直到调用ResumeThread函数
NULL, // 指向一个新进程的环境块。如果此参数为空,新进程使用调用进程的环境
NULL, // 指定子进程的工作路径
&si, // 决定新进程的主窗体如何显示的STARTUPINFO结构体
&pi // 接收新进程的识别信息的PROCESS_INFORMATION结构体
)) {
//Sleep(1000);
}
return TRUE;
}
bool KillProcess(std::vector<std::string>& processNameVec)
{
bool result = false;
std::string strProcess;
HANDLE hSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
PROCESSENTRY32 pInfo;
pInfo.dwSize = sizeof(pInfo);
Process32First(hSnapShot, &pInfo);
do
{
std::wstring wstrTemp = pInfo.szExeFile;
strProcess = UnicodeToAscii(wstrTemp);
bool bIn = false;
for (int i = 0; i < processNameVec.size(); i++) {
if (processNameVec[i] == strProcess) {
bIn = true;
}
}
if (bIn)
{
result = true;
//std::string cmd;
//char cmdData[128] = { 0 };
//sprintf_s(cmdData, "taskkill /F /PID %d /T", pInfo.th32ProcessID);
//cmd = cmdData;
//WinExec(cmdData, SW_HIDE);
TerminateProcessFromID(pInfo.th32ProcessID);
break;
}
} while (Process32Next(hSnapShot, &pInfo));
return result;
}
void runApp()
{
char chpath[MAX_PATH];
GetModuleFileNameA(NULL, (LPSTR)chpath, sizeof(chpath));
std::string path = std::string(chpath);
int pos = path.find_last_of("\\");
if (pos != std::string::npos)
{
path = path.substr(0, pos);
SetCurrentDirectoryA(path.c_str());
}
std::cout << path << std::endl;
std::string cmd = path + std::string("\\WebCamService.exe ");
//std::string cmd = path + std::string("\\MaterialDesignColors.exe ");
//ShellExecuteA(NULL, "open", cmd.c_str(),
// "", "", SW_SHOW);
ExecuteAppFromLocal(const_cast<char*>(cmd.c_str()));
}
void killApp(){
std::vector <std::string> pnames;
pnames.push_back("WebCamService.exe");
//pnames.push_back("MaterialDesignColors.exe");
KillProcess(pnames);
}
int main(int argc, char* argv[])
{
//std::cout << " => "<< argc << std::endl;
if (argc < 2)
{
killApp();
runApp();
}
else
{
if (std::string(argv[1]).find("webcamservice:0") != std::string::npos)
{
killApp();
}
else if (std::string(argv[1]).find("webcamservice:1") != std::string::npos)
{
runApp();
}
else if (std::string(argv[1]).find("webcamservice:2") != std::string::npos)
{
killApp();
Sleep(500);
runApp();
}
else
{
killApp();
runApp();
}
}
//getchar();
return 0;
}