[KERNEL32]
[reactos.git] / rostests / tests / lpctst / conport.c
1 /* $Id$
2 *
3 * reactos/apps/lpc/conport.c
4 *
5 * To be run in a real WNT 4.0 system with
6 * "\SmApiPort" as argument. Do not try to
7 * connect to "\Windows\ApiPort" since that
8 * reboots immeditely.
9 *
10 * Use Russinovich' HandleEx to verify
11 * conport.exe owns two unnamed LPC ports:
12 * the one created by kernel32.dll connecting
13 * to csrss.exe, and one connected to here.
14 *
15 * 19990627 (Emanuele Aliberti)
16 * Initial implementation.
17 * 19990704 (EA)
18 * Dump object's attributes moved in dumpinfo.c.
19 */
20 #include <windows.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #define PROTO_LPC
24 #include <ddk/ntddk.h>
25 #include "dumpinfo.h"
26
27 #define LPC_CONNECT_FLAG1 0x00000001
28 #define LPC_CONNECT_FLAG2 0x00000010
29 #define LPC_CONNECT_FLAG3 0x00000100
30 #define LPC_CONNECT_FLAG4 0x00001000
31 #define LPC_CONNECT_FLAG5 0x00010000
32
33 NTSTATUS
34 (WINAPI * ConnectPort)(
35 OUT PHANDLE PortHandle,
36 IN PUNICODE_STRING PortName,
37 IN POBJECT_ATTRIBUTES ObjectAttributes,
38 IN DWORD Unknown3,
39 IN DWORD Unknown4,
40 IN DWORD Unknown5,
41 IN DWORD Unknown6,
42 IN ULONG Flags
43 );
44
45 NTSTATUS
46 (WINAPI * QueryObject)(
47 IN HANDLE ObjectHandle,
48 IN CINT ObjectInformationClass,
49 OUT PVOID ObjectInformation,
50 IN ULONG Length,
51 OUT PULONG ResultLength
52 );
53
54 NTSTATUS
55 (WINAPI * YieldExecution)(VOID);
56
57 #define BUF_SIZE 1024
58 #define MAXARG 1000000
59
60
61 VOID
62 TryConnectPort(char *port_name)
63 {
64 DWORD Status = 0;
65 HANDLE Port = 0;
66 int i;
67 UNICODE_STRING PortName;
68 OBJECT_ATTRIBUTES ObjectAttributes;
69 WORD Name [BUF_SIZE] = {0};
70 int dwx = 0;
71 char * port_name_save = port_name;
72
73 /*
74 * Convert the port's name to Unicode.
75 */
76 for (
77 PortName.Length = 0;
78 ( *port_name
79 && (PortName.Length < BUF_SIZE)
80 );
81 )
82 {
83 Name[PortName.Length++] = (WORD) *port_name++;
84 }
85 Name[PortName.Length] = 0;
86
87 PortName.Length = PortName.Length * sizeof (WORD);
88 PortName.MaximumLength = PortName.Length + sizeof (WORD);
89 PortName.Buffer = (PWSTR) Name;
90 /*
91 * Prepare the port object attributes.
92 */
93 ObjectAttributes.Length =
94 sizeof (OBJECT_ATTRIBUTES);
95 ObjectAttributes.RootDirectory =
96 NULL;
97 ObjectAttributes.ObjectName =
98 NULL /*& PortName */;
99 ObjectAttributes.Attributes =
100 OBJ_CASE_INSENSITIVE;
101 ObjectAttributes.SecurityDescriptor =
102 NULL;
103 ObjectAttributes.SecurityQualityOfService =
104 NULL;
105 /*
106 * Try to issue a connection request.
107 */
108 Port = 0;
109 Status = ConnectPort(
110 & Port, /* & PortHandle */
111 & PortName, /* & PortName */
112 & ObjectAttributes, /* & PortAttributes */
113 NULL, /* & SecurityQos */
114 NULL, /* & SectionInfo */
115 NULL, /* & MapInfo */
116 NULL, /* & MaxMessageSize */
117 LPC_CONNECT_FLAG5 /* & ConnectInfoLength */
118 );
119 if (Status == STATUS_SUCCESS)
120 {
121 DumpInfo(
122 Name,
123 Status,
124 "connected",
125 Port
126 );
127 /* Hot waiting */
128 for (dwx=0; dwx<MAXARG; ++dwx)
129 {
130 YieldExecution();
131 }
132 if (FALSE == CloseHandle(Port))
133 {
134 printf(
135 "Could not close the port handle %08X.\n",
136 Port
137 );
138 }
139 return;
140 }
141 printf(
142 "Connection to port \"%s\" failed (Status = %08X).\n",
143 port_name_save,
144 Status
145 );
146 }
147
148
149 main( int argc, char * argv[] )
150 {
151 HINSTANCE ntdll;
152
153 if (argc != 2)
154 {
155 printf("WNT LPC Port Connector\n");
156 printf("Usage: %s [port_name]\n",argv[0]);
157 exit(EXIT_FAILURE);
158 }
159 printf("LoadLibrary(NTDLL)\n");
160 ntdll = LoadLibrary("NTDLL");
161 if (ntdll == NULL)
162 {
163 printf("Could not load NTDLL\n");
164 return EXIT_FAILURE;
165 }
166 printf("GetProcAddress(NTDLL.NtConnectPort)\n");
167 ConnectPort = (VOID*) GetProcAddress(
168 ntdll,
169 "NtConnectPort"
170 );
171 if (ConnectPort == NULL)
172 {
173 FreeLibrary(ntdll);
174 printf("Could not find NTDLL.NtConnectPort\n");
175 return EXIT_FAILURE;
176 }
177 printf("GetProcAddress(NTDLL.NtQueryObject)\n");
178 QueryObject = (VOID*) GetProcAddress(
179 ntdll,
180 "NtQueryObject"
181 );
182 if (QueryObject == NULL)
183 {
184 FreeLibrary(ntdll);
185 printf("Could not find NTDLL.NtQueryObject\n");
186 return EXIT_FAILURE;
187 }
188 printf("GetProcAddress(NTDLL.NtYieldExecution)\n");
189 YieldExecution = (VOID*) GetProcAddress(
190 ntdll,
191 "NtYieldExecution"
192 );
193 if (YieldExecution == NULL)
194 {
195 FreeLibrary(ntdll);
196 printf("Could not find NTDLL.NtYieldExecution\n");
197 return EXIT_FAILURE;
198 }
199 printf("TryConnectPort(%s)\n",argv[1]);
200 TryConnectPort(argv[1]);
201 printf("Done\n");
202 return EXIT_SUCCESS;
203 }
204
205 /* EOF */