:pserver:cvsanon@mok.lvcm.com:/CVS/ReactOS reactos
[reactos.git] / apps / tests / lpc / creport.c
1 /* $Id$
2  *
3  * reactos/apps/lpc/creport.c
4  *
5  * To be run in a real WNT 4.0 system to
6  * create an LPC named port.
7  * 
8  * Use Russinovich' HandleEx to verify
9  * creport.exe owns the named LPC port
10  * you asked to create.
11  */
12 #include <windows.h>
13 #include <stdio.h>
14 #include <stdlib.h>
15 #define PROTO_LPC
16 #include <ddk/ntddk.h>
17 #include "dumpinfo.h"
18
19 #define LPC_CONNECT_FLAG1 0x00000001
20 #define LPC_CONNECT_FLAG2 0x00000010
21 #define LPC_CONNECT_FLAG3 0x00000100
22 #define LPC_CONNECT_FLAG4 0x00001000
23 #define LPC_CONNECT_FLAG5 0x00010000
24
25 NTSTATUS
26 (STDCALL * CreatePort)(
27         /*OUT   PHANDLE                 PortHandle,*/
28         PVOID   Buffer,
29         IN      POBJECT_ATTRIBUTES      PortAttributes  OPTIONAL,  
30         IN      ACCESS_MASK             DesiredAccess,
31         IN      DWORD                   Unknown3,
32         IN      ULONG                   Flags
33         );
34
35 NTSTATUS
36 (STDCALL * QueryObject)(
37         IN      HANDLE  ObjectHandle,
38         IN      CINT    ObjectInformationClass,
39         OUT     PVOID   ObjectInformation,
40         IN      ULONG   Length,
41         OUT     PULONG  ResultLength
42         );
43   
44 NTSTATUS
45 (STDCALL * YieldExecution)(VOID);
46
47 #define BUF_SIZE 1024
48 #define MAXARG   5000000
49
50
51 VOID
52 TryCreatePort(char *port_name)
53 {
54         DWORD                   Status = 0;
55         HANDLE                  Port = 0;
56         int                     i;
57         UNICODE_STRING          PortName;
58         OBJECT_ATTRIBUTES       ObjectAttributes;
59         WORD                    Name [BUF_SIZE] = {0};
60         int                     dwx = 0;
61         char                    * port_name_save = port_name;
62
63         /*
64          * Convert the port's name to Unicode.
65          */
66         for (
67                 PortName.Length = 0;
68                 (       *port_name
69                         && (PortName.Length < BUF_SIZE)
70                         );
71                 )
72         {
73                 Name[PortName.Length++] = (WORD) *port_name++;
74         }
75         Name[PortName.Length] = 0;
76
77         PortName.Length = PortName.Length * sizeof (WORD);
78         PortName.MaximumLength = PortName.Length + sizeof (WORD);
79         PortName.Buffer = (PWSTR) Name;
80         /*
81          * Prepare the port object attributes.
82          */
83         ObjectAttributes.Length =
84                 sizeof (OBJECT_ATTRIBUTES);
85         ObjectAttributes.RootDirectory =
86                 NULL;
87         ObjectAttributes.ObjectName =
88                 & PortName;
89         ObjectAttributes.Attributes =
90                 0; //OBJ_CASE_INSENSITIVE --> STATUS_INVALID_PARAMETER ==> case sensitive!;
91         ObjectAttributes.SecurityDescriptor =
92                 NULL;
93         ObjectAttributes.SecurityQualityOfService =
94                 NULL;
95         /*
96          * Try to issue a connection request.
97          */
98         Port = 0;
99         Status = CreatePort(
100                         & Port,
101                         & ObjectAttributes,
102                         0, /* ACCESS_MASK? */
103                         0, /* Unknown3 */
104                         LPC_CONNECT_FLAG5
105                         );
106         if (Status == STATUS_SUCCESS)
107         {
108                 DumpInfo(
109                         Name,
110                         Status,
111                         "created",
112                         Port
113                         );
114                 /* Hot waiting */
115                 for (dwx=0; dwx<MAXARG; ++dwx)
116                 {
117                         YieldExecution();
118                 }
119                 if (FALSE == CloseHandle(Port))
120                 {
121                         printf(
122                                 "Could not close the port handle %08X.\n",
123                                 Port
124                                 );
125                 }
126                 return;
127         }
128         printf(
129                 "Creating port \"%s\" failed (Status = %08X).\n",
130                 port_name_save,
131                 Status
132                 );
133 }
134
135
136 main( int argc, char * argv[] )
137 {
138         HINSTANCE ntdll;
139
140         if (argc != 2)
141         {
142                 printf("WNT LPC Port Creator\n");
143                 printf("Usage: %s [port_name]\n",argv[0]);
144                 exit(EXIT_FAILURE);
145         }
146         printf("LoadLibrary(NTDLL)\n");
147         ntdll = LoadLibrary("NTDLL");
148         if (ntdll == NULL)
149         {
150                 printf("Could not load NTDLL\n");
151                 return EXIT_FAILURE;
152         }
153         printf("GetProcAddress(NTDLL.NtCreatePort)\n");
154         CreatePort = (VOID*) GetProcAddress(
155                                         ntdll,
156                                         "NtCreatePort"
157                                         );
158         if (CreatePort == NULL)
159         {
160                 FreeLibrary(ntdll);
161                 printf("Could not find NTDLL.NtCreatePort\n");
162                 return EXIT_FAILURE;
163         }
164         printf("GetProcAddress(NTDLL.NtQueryObject)\n");
165         QueryObject = (VOID*) GetProcAddress(
166                                         ntdll,
167                                         "NtQueryObject"
168                                         );
169         if (QueryObject == NULL)
170         {
171                 FreeLibrary(ntdll);
172                 printf("Could not find NTDLL.NtQueryObject\n");
173                 return EXIT_FAILURE;
174         }
175         printf("GetProcAddress(NTDLL.NtYieldExecution)\n");
176         YieldExecution = (VOID*) GetProcAddress(
177                                         ntdll,
178                                         "NtYieldExecution"
179                                         );
180         if (YieldExecution == NULL)
181         {
182                 FreeLibrary(ntdll);
183                 printf("Could not find NTDLL.NtYieldExecution\n");
184                 return EXIT_FAILURE;
185         }
186         printf("TryCreatePort(%s)\n",argv[1]);
187         TryCreatePort(argv[1]);
188         printf("Done\n");
189         return EXIT_SUCCESS;
190 }
191
192 /* EOF */