標籤:c style class blog code a
引子:話說老王的果園大豐收,老王心花怒放,帶著全家去美國阿拉斯加度假。阿拉斯加有很多東西琳琅滿目,都是中國沒有的,老王及家人都過了一把購物癮。但是有一次卻遇到了比較尷尬的事。怎麼回事呢?原來老王第一次出國,在買地攤上的東西時討價還價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,所以成功實現共用。