ReactOS
0.4.16-dev-1946-g52006dd
p_dup_handle.c
Go to the documentation of this file.
1
#include <stdio.h>
2
#include <windows.h>
3
#include <stdlib.h>
4
#include <string.h>
5
6
/* This tests the ability of the target win32 to duplicate a process handle,
7
* spawn a child, and have the child dup it's own handle back into the parent
8
* using the duplicated handle.
9
*/
10
11
int
main
(
int
argc
,
char
**
argv
) {
12
HANDLE
h_process;
13
HANDLE
h_process_in_parent;
14
15
fprintf
(
stderr
,
"%lu: Starting\n"
,
GetCurrentProcessId
() );
16
17
if
(
argc
== 2 ) {
18
h_process = (
HANDLE
)(
ULONG_PTR
)
atoi
(
argv
[1]);
19
}
else
{
20
if
( !
DuplicateHandle
(
GetCurrentProcess
(),
21
GetCurrentProcess
(),
22
GetCurrentProcess
(),
23
&h_process,
24
0,
25
TRUE
,
26
DUPLICATE_SAME_ACCESS
) ) {
27
fprintf
(
stderr
,
"%lu: Could not duplicate my own process handle.\n"
,
28
GetCurrentProcessId
() );
29
return
101;
30
}
31
}
32
33
if
(
argc
== 1 ) {
34
STARTUPINFO
si;
35
PROCESS_INFORMATION
pi
;
36
char
cmdline
[1000];
37
38
memset
( &si, 0,
sizeof
( si ) );
39
memset
( &
pi
, 0,
sizeof
(
pi
) );
40
41
sprintf
(
cmdline
,
"%s %p"
,
argv
[0], h_process );
42
if
( !
CreateProcess
(
NULL
,
cmdline
,
NULL
,
NULL
,
TRUE
, 0,
NULL
,
NULL
,
43
&si, &
pi
) ) {
44
fprintf
(
stderr
,
"%lu: Could not create child process.\n"
,
45
GetCurrentProcessId
() );
46
return
5;
47
}
48
49
if
(
WaitForSingleObject
(
pi
.hThread,
INFINITE
) !=
WAIT_OBJECT_0
) {
50
fprintf
(
stderr
,
"%lu: Failed to wait for child process to terminate.\n"
,
51
GetCurrentProcessId
() );
52
return
6;
53
}
54
}
else
{
55
if
( !
DuplicateHandle
(
GetCurrentProcess
(),
56
GetCurrentProcess
(),
57
h_process,
58
&h_process_in_parent,
59
0,
60
TRUE
,
61
DUPLICATE_SAME_ACCESS
) ) {
62
fprintf
(
stderr
,
"%lu: Could not duplicate my handle into the parent.\n"
,
63
GetCurrentProcessId
() );
64
return
102;
65
}
66
}
67
68
return
0;
69
}
argc
static int argc
Definition:
ServiceArgs.c:12
NULL
#define NULL
Definition:
types.h:112
TRUE
#define TRUE
Definition:
types.h:120
GetCurrentProcess
#define GetCurrentProcess()
Definition:
compat.h:759
DuplicateHandle
BOOL WINAPI DuplicateHandle(IN HANDLE hSourceProcessHandle, IN HANDLE hSourceHandle, IN HANDLE hTargetProcessHandle, OUT LPHANDLE lpTargetHandle, IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwOptions)
Definition:
handle.c:149
main
int main()
Definition:
test.c:6
INFINITE
#define INFINITE
Definition:
serial.h:102
stderr
#define stderr
Definition:
stdio.h:100
fprintf
_Check_return_opt_ _CRTIMP int __cdecl fprintf(_Inout_ FILE *_File, _In_z_ _Printf_format_string_ const char *_Format,...)
void
Definition:
nsiface.idl:2307
sprintf
#define sprintf
Definition:
sprintf.c:45
pi
static refpint_t pi[]
Definition:
server.c:112
argv
#define argv
Definition:
mplay32.c:18
atoi
_Check_return_ int __cdecl atoi(_In_z_ const char *_Str)
memset
#define memset(x, y, z)
Definition:
compat.h:39
cmdline
TCHAR * cmdline
Definition:
stretchblt.cpp:32
_PROCESS_INFORMATION
Definition:
processthreadsapi.h:65
_STARTUPINFOA
Definition:
processthreadsapi.h:15
WaitForSingleObject
DWORD WINAPI WaitForSingleObject(IN HANDLE hHandle, IN DWORD dwMilliseconds)
Definition:
synch.c:82
HANDLE
PVOID HANDLE
Definition:
typedefs.h:73
ULONG_PTR
uint32_t ULONG_PTR
Definition:
typedefs.h:65
CreateProcess
#define CreateProcess
Definition:
winbase.h:3507
GetCurrentProcessId
DWORD WINAPI GetCurrentProcessId(void)
Definition:
proc.c:1158
WAIT_OBJECT_0
#define WAIT_OBJECT_0
Definition:
winbase.h:383
DUPLICATE_SAME_ACCESS
#define DUPLICATE_SAME_ACCESS
modules
rostests
tests
p_dup_handle
p_dup_handle.c
Generated on Thu Dec 4 2025 06:09:54 for ReactOS by
1.9.6