Vernacular Windows kernel object share copy object handle

Source: Internet
Author: User
Tags sprintf

Introduction : The Old King's Orchard Big Harvest, Lao Wang elated, with the family to the United States Alaska holiday. Alaska have a lot of things, are not in China, Lao Wang and his family have been a shopping addiction. But one time, I met a rather awkward thing. What's going on here? The original Lao Wang for the first time to go abroad, in the purchase of things on the street bargain 100 yuan, but to others is 100 yuan, others naturally do not do, you 100 yuan only how many dollars ah, Lao Wang had to reluctantly to 600 yuan.

Why is there such an embarrassment? Because the currency conversion of two countries is not the same. China's 100 yuan and the US $100 are not equivalent, how can it be equivalent? Must be converted according to the current exchange rate. The copy kernel object handle that I want to talk about today is also true. A process cannot directly use a kernel object in a B process, it must call the relevant function to copy and convert it to the handle value of the process.

The following code is given:

A process:

#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.szexef Ile, 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);p rintf ("%d doing something now in Process Id%d\n", GetCurrentThreadID (), GetCurrentProcessId ()); Sleep (+);p rintf ("%d have finished in Process Id%D\n ", GetCurrentThreadID (), GetCurrentProcessId ()); Char strtime[100]; Getcurtime (strtime);p rintf ("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;D uplicatehandle (getcurrentprocess (), G_hmutext, Getprocesshandle (_t ("DuplicateHandle2.exe") ), &AMP;HANDTOCVT, 0, FALSE, duplicate_same_access);p rintf ("The raw and DUPLICATE handle value is%d,%d\n", G_hmutext, H ANDTOCVT); Sleep (2000); CreateThread (null, 0, fun, NULL, 0, NULL);p rintf ("The value was%d\n", HANDTOCVT), while (1) {}return 0;}
B Process:

 #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);p rintf ("The current time was%s\n", Strtime);p rintf ("%d doing something now in Process Id%d\n", GetC Urrentthreadid (), GetCurrentProcessId ()); Sleep (+);p rintf ("%d have 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), and while (1) {}return 0;} 
The following analysis is given:

A process creates a mutex variable g_hmutext, and then calls DuplicateHandle to copy the record entry for the handle table to the B process (b process must first run, A process can get the handle of the B process through Getprocesshandle (a) to the record entry in the handle table, and gives the corresponding index in the B process. Please see the effect:

A process copies the created G_hmutext (the handle value is 48) to the B process, and the handle value in the B process is also 48 (I don't know what the connection is, please tell the big God). This gives this value to the G_hmutext handle in process B. After the thread of a process is finished, the line Cheng Lima waiting for G_hmutext in the B process starts executing, and the time is 14:14:36, so the share is successfully implemented.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.