现在的位置: 首页 > 综合 > 正文

白话windows内核对象共享之复制对象句柄

2018年02月24日 ⁄ 综合 ⁄ 共 2775字 ⁄ 字号 评论关闭

   引子:话说老王的果园大丰收,老王心花怒放,带着全家去美国阿拉斯加度假。阿拉斯加有很多东西琳琅满目,都是中国没有的,老王及家人都过了一把购物瘾。但是有一次却遇到了比较尴尬的事。怎么回事呢?原来老王第一次出国,在买地摊上的东西时讨价还价100元,但是给人家的却是100元人民币,人家自然不干撒,你100元才多少美元呀,老王只好忍痛割爱给了600元人民币。

为什么会出现这样的尴尬呢?因为两个国家的货币换算不是一样的。中国的100元和美国的100元不是等价的,如何才能等价呢?必须根据当前汇率来换算。今天要讲的复制内核对象句柄也是这个道理。A进程不能直接用B进程中的内核对象,必须调用相关的函数进行复制并转换成该进程的句柄值。

下面给出代码:

A进程:

#include "stdafx.h"
#include <Windows.h>
#include <process.h>
#include <TlHelp32.h>
#include <time.h>

HANDLE g_hMutext = NULL ;

HANDLE GetProcessHandle(LPCTSTR szName)
{
	HANDLE hSanpshot;
	hSanpshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
	if ( INVALID_HANDLE_VALUE == hSanpshot ){
		return NULL;
	}

	PROCESSENTRY32 pe;
	BOOL bOk;
	pe.dwSize = sizeof(pe);

	bOk = Process32First (hSanpshot, &pe);
	if (!bOk)
		return NULL;
	do {
		if ( !wcscmp (pe.szExeFile, szName) ){
			return OpenProcess (PROCESS_ALL_ACCESS, FALSE, pe.th32ProcessID);
		}
		bOk = Process32Next (hSanpshot, &pe);
	}while (bOk);

	return NULL;
}

void GetCurTime(char* str)
{
	time_t ct ;
	tm *ctm ;
	time(&ct) ;
	ctm = localtime(&ct) ;
	sprintf(str, "%02d:%02d:%02d", ctm->tm_hour, ctm->tm_min, ctm->tm_sec) ;
}

DWORD WINAPI Fun(LPVOID lp)
{
	WaitForSingleObject(g_hMutext, INFINITE) ;
	printf("%d doing something now in Process Id %d\n", GetCurrentThreadId(), GetCurrentProcessId()) ;
	Sleep(1000 * 10) ;
	printf("%d has Finished in Process Id %d\n", GetCurrentThreadId(), GetCurrentProcessId()) ;
	char strTime[100] ;
	GetCurTime(strTime) ;
	printf("The Current time is %s\n", strTime) ;
	ReleaseMutex(g_hMutext) ;
	return 0 ;
}


int _tmain(int argc, _TCHAR* argv[])
{
	g_hMutext = CreateMutex(NULL, FALSE, NULL) ;

	HANDLE handToCvt = NULL ;
	DuplicateHandle(GetCurrentProcess(), g_hMutext, GetProcessHandle(_T("DuplicateHandle2.exe")), &handToCvt, 0, FALSE, DUPLICATE_SAME_ACCESS) ;
	printf("the raw and duplicate handle value is %d, %d\n", g_hMutext, handToCvt) ;
	Sleep(2000) ;
	CreateThread(NULL, 0, Fun, NULL, 0, NULL) ;

	printf("the value is %d\n", handToCvt) ;
	
	while(1){}
	return 0;
}

B进程:

#include "stdafx.h"
#include <Windows.h>
#include <process.h>
#include <time.h>

HANDLE g_hMutext = NULL ;

void GetCurTime(char* str)
{
	time_t ct ;
	tm *ctm ;
	time(&ct) ;
	ctm = localtime(&ct) ;
	sprintf(str, "%02d:%02d:%02d", ctm->tm_hour, ctm->tm_min, ctm->tm_sec) ;
}

DWORD WINAPI Fun(LPVOID lp)
{
	WaitForSingleObject(g_hMutext, INFINITE) ;
	char strTime[100] ;
	GetCurTime(strTime) ;
	printf("The Current time is %s\n", strTime) ;
	printf("%d doing something now in Process Id %d\n", GetCurrentThreadId(), GetCurrentProcessId()) ;
	Sleep(1000 * 10) ;
	printf("%d has Finished in Process Id %d\n", GetCurrentThreadId(), GetCurrentProcessId()) ;
	ReleaseMutex(g_hMutext) ;
	return 0 ;
}

int _tmain(int argc, _TCHAR* argv[])
{
	printf("please enter the mutext handle value:") ;
	scanf("%d", &g_hMutext) ;
	CreateThread(NULL, 0, Fun, NULL, 0, NULL) ;

	while(1){}
	return 0;
}

下面给出分析:

A进程创建了一个互斥变量g_hMutext,然后调用DuplicateHandle将这个句柄表的记录项复制到B进程(B进程必须首先运行,A进程才能通过GetProcessHandle(A)获得B进程的句柄)句柄表的记录项中,并给出在B进程中对应的索引。请看效果:

A进程把创建的g_hMutext(句柄值是48)复制到B进程,得到B进程中该句柄值也是48(不知道有什么联系,还请大神告知)。这样将这个值给进程B中的g_hMutext句柄。A进程中线程结束之后,B进程中等待g_hMutext的线程立马开始执行,时间都是14:14:36,所以成功实现共享。

抱歉!评论已关闭.