[ROSTESTS]
[reactos.git] / rostests / tests / p_dup_handle / p_dup_handle.c
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 }