DuplicateHandle伪句柄与实句柄的应用二

//扫描进程列表,获得进程名为 ConsoleApplication2.exe的进程句柄B,把当前进程A的伪
//句柄传递给B,在B进程中关闭它
#include "stdafx.h" 
#include <iostream> 
#include <windows.h> 
#include <tlhelp32.h> 
#include <process.h> 
using namespace std; 
#pragma warning(disable:4996) 
HANDLE Get(wstring tcExeName) 
{ 
	HANDLE hTool = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 
		GetCurrentProcessId()); 
	PROCESSENTRY32 pe; 
	memset(&pe, 0, sizeof(pe)); 
	pe.dwSize = sizeof(pe); 
	Process32First(hTool, &pe); 
	while (Process32Next(hTool, &pe)){ 
		if(wcsicmp(pe.szExeFile , tcExeName.c_str()) == 0){ 
			return OpenProcess(PROCESS_ALL_ACCESS, 0, pe.th32ProcessID); 
		} 
	} 
	return NULL; 
} 
int _tmain() 
{ 
	HANDLE hProcess = NULL; 
	DuplicateHandle(GetCurrentProcess(), GetCurrentProcess(), 
		Get(L"ConsoleApplication2.exe"), &hProcess, 0, 0, DUPLICATE_SAME_ACCESS); 
	while(1){//一直运行,等待被关闭
	} 
	return 0; 
} 
进程B:
#include "stdafx.h" 
#include <windows.h> 
#include <iostream> 
	using namespace std; 
int _tmain(int argc, _TCHAR* argv[]) 
{ 


	cout<<"请输入原始句柄"<<endl; 
	HANDLE hProcess = NULL; 
	cin>>hProcess; 
	DWORD dwExitCode = 0; 
	GetExitCodeProcess(hProcess, &dwExitCode); 
	TerminateProcess(hProcess, dwExitCode); 
	return 0; 
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

 

posted @ 2015-01-16 11:54  QQ76211822  阅读(271)  评论(0编辑  收藏  举报