VC++.单例1比较进程路径

2021-01-21 08:15

阅读:458

标签:rmi   other   bool   fir   命中   base   bug   ice   pid   

// ?.h : 头文件
#pragma once
#include 
#include 
#include 

// ?.cpp
//#include 
CString DosPathToNt(IN TCHAR *szDosPath)
{
	CString ret;
	try
	{
		TCHAR szDriver[MAX_PATH] = { 0 };
		TCHAR szTemp[3] = { 0 };
		TCHAR szDevName[MAX_PATH] = { 0 };
		INT iTemp = 0;
		if (!szDosPath)
		{
			return ret;
		}
		if (GetLogicalDriveStrings(MAX_PATH * sizeof(TCHAR), szDriver))
		{
			//c : \ \0
			//d : \ \0
			//...
			//\0 \0 \0 \0
			for (DWORD i = 0; szDriver[i]; i += 4)
			{
				//跳过盘符A:  B: 
				if (!lstrcmpi(&(szDriver[i]), _T("A:\\")) || !lstrcmpi(&(szDriver[i]), _T("B:\\")))
					continue;
				szTemp[0] = szDriver[i];
				szTemp[1] = szDriver[i + 1];
				szTemp[2] = _T(‘\0‘);
				if (QueryDosDevice(szTemp, szDevName, MAX_PATH * sizeof(TCHAR))) //查询 Dos 设备名
				{
					iTemp = lstrlen(szDevName);
					if (0 == _tcsnicmp(szDosPath, szDevName, lstrlen(szDevName))) //命中
					{
						ret = szTemp; //复制驱动器
						ret += (szDosPath + iTemp); //复制路径
					}
				}
			}
		}
		else
		{
			ret = szDosPath;
		}
	}
	catch (...)
	{
		OutputDebugStringA(__FUNCTION__);
	}
	return ret;
}
CString GetProcessPath(IN DWORD dwPid)
{
	CString ret;
	try
	{
		TCHAR szImagePath[MAX_PATH] = { 0 };
		HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, 0, dwPid);
		if (hProcess)
		{
			if (GetProcessImageFileName(hProcess, szImagePath, (MAX_PATH - 1) * sizeof(TCHAR)))
			{
				ret = DosPathToNt(szImagePath);
			}
			CloseHandle(hProcess);
		}
	}
	catch (...)
	{
		OutputDebugStringA(__FUNCTION__);
	}
	return ret;
}
//#include 
//#include 
BOOL TraversalProcess1(OUT std::vector &vec)
{
	/************************************************************************/
	/*
	typedef struct tagPROCESSENTRY32
	{
	DWORD   dwSize;
	DWORD   cntUsage;
	DWORD   th32ProcessID;          // this process
	ULONG_PTR th32DefaultHeapID;
	DWORD   th32ModuleID;           // associated exe
	DWORD   cntThreads;
	DWORD   th32ParentProcessID;    // this process‘s parent process
	LONG    pcPriClassBase;         // Base priority of process‘s threads
	DWORD   dwFlags;
	CHAR    szExeFile[MAX_PATH];    // Path 其实是进程名称
	} PROCESSENTRY32;
	*/
	/************************************************************************/
	vec.clear();
	try
	{
		HANDLE hSnap = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
		if (hSnap != INVALID_HANDLE_VALUE)
		{
			PROCESSENTRY32 item;
			item.dwSize = sizeof(item);
			if (Process32First(hSnap, &item))
			{
				do
				{
					vec.push_back(item);
				} while (Process32Next(hSnap, &item));
			}
			CloseHandle(hSnap);
		}
	}
	catch (...)
	{
		OutputDebugStringA(__FUNCTION__);
		return 0;
	}
	return vec.size() > 0;
}
BOOL ?::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// 单例1:取本进程全路径,遍历所有进程取路径,逐个比较,命中则本进程退出。
	CString strCurPath = GetProcessPath(GetCurrentProcessId());
	std::vector vec;
	TraversalProcess1(vec);
	for each (PROCESSENTRY32 var in vec)
	{
		CString strOtherPath = GetProcessPath(var.th32ProcessID);
		if (strCurPath.CompareNoCase(strOtherPath) == 0 && GetCurrentProcessId() != var.th32ProcessID)
		{
			TerminateProcess(GetCurrentProcess(), 0);
		}
	}

	return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
}

VC++.单例1比较进程路径

标签:rmi   other   bool   fir   命中   base   bug   ice   pid   

原文地址:https://www.cnblogs.com/dailycode/p/12896490.html

上一篇:java八大排序

下一篇:Python 目录


评论


亲,登录后才可以留言!