DuplicateHandle, why duplicate, and not just acquire?

Why does a process want to call DuplicateHandle from Win32API and get it from another process instead of just getting a handle on some object?

Is there any advantage to calling DuplicateHandle or something else?

+3
source share
4 answers

You can find the answer in Chapter 6.8, "Application Programming for Microsoft Windows."

Getting a sense of self
Sometimes you may need to get a real handle for the flow instead of a pseudo descriptor. By "real" is meant a descriptor that uniquely identifies a unique stream. Examine the following code:
DWORD WINAPI ParentThread(PVOID pvParam) {
   HANDLE hThreadParent = GetCurrentThread();
   CreateThread(NULL, 0, ChildThread, (PVOID) hThreadParent, 0, NULL);
   // Function continues...
}

DWORD WINAPI ChildThread(PVOID pvParam) {
   HANDLE hThreadParent = (HANDLE) pvParam;
   FILETIME ftCreationTime, ftExitTime, ftKernelTime, ftUserTime;
   GetThreadTimes(hThreadParent,
      &ftCreationTime, &ftExitTime, &ftKernelTime, &ftUserTime);
   // Function continues...
}
? , , . , -, . , - GetThreadTimes, - , . , - , , .

, - . DuplicateHandle ( 3)
+6

. , MSDN, DuplicateHandle. , , - , - , , CreateHandle , DuplicateHandle, , , , CreateHandle...

, , , .

+1

DuplicateHandle 32- 64- .

: /.

+1

Another use of DuplicateHandle is to open a file in several processes when the file is using FileOptions.DeleteOnClose. (such a file cannot be opened by several processes if the file path is used to open the file)

See my answer at fooobar.com/questions/1093897 / ...

+1
source

Source: https://habr.com/ru/post/1730753/


All Articles