62 lines
1.6 KiB
C++
62 lines
1.6 KiB
C++
#include "CanProtocol.h" /* CAN接收任务 */
|
|
#include "SocketApiProtocol.h"
|
|
|
|
#define LISTEN_PORT 12345 //监听端口
|
|
|
|
int SearchPreProcess(void);
|
|
void KillPreProcess(void);
|
|
|
|
int main() {
|
|
|
|
//此程序必须只有一个进程在运行
|
|
while (SearchPreProcess() == ENABLE)
|
|
{
|
|
//杀死上一个进程
|
|
KillPreProcess();
|
|
std::cout<< "Kill Pre Process" <<std::endl;
|
|
//等待进程里面的socket和内存被释放
|
|
Sleep(500);
|
|
}
|
|
|
|
//系统参数和回调初始化
|
|
SystemCallInit();
|
|
|
|
//监听SOCKET端口
|
|
SocketApiProtocolInit(LISTEN_PORT);
|
|
|
|
//打印一下
|
|
std::cout << "Program Run Success" << std::endl;
|
|
|
|
//阻塞一下
|
|
getchar();
|
|
|
|
return 0;
|
|
}
|
|
|
|
//查找是否有本程序的进程在运行
|
|
int SearchPreProcess(void)
|
|
{
|
|
char cmd[512];
|
|
DWORD currentPID = GetCurrentProcessId();
|
|
|
|
// 查找同名进程,但排除自己
|
|
snprintf(cmd, sizeof(cmd),
|
|
"tasklist | findstr \"UniversalCanCore.exe\" | findstr /v \"%lu\" > nul",
|
|
currentPID);
|
|
|
|
return system(cmd) == 0 ? ENABLE : DISABLE;
|
|
}
|
|
|
|
//杀死同名进程以及上一个进程
|
|
void KillPreProcess(void)
|
|
{
|
|
char cmd[256];
|
|
DWORD currentPID = GetCurrentProcessId();
|
|
|
|
// 查找并杀死除了自己以外的同名进程
|
|
snprintf(cmd, sizeof(cmd),
|
|
"for /f \"tokens=2\" %%a in ('tasklist ^| findstr \"UniversalCanCore.exe\"') do if not %%a==%lu taskkill /F /PID %%a",
|
|
currentPID);
|
|
|
|
system(cmd);
|
|
} |