internalNameBuilder(): Fixed missing REACTOS_UCS2() wrapper
[reactos.git] / subsys / csrss / csrss.c
1 /* $Id$
2  *
3  * csrss.c - Client/Server Runtime subsystem
4  * 
5  * ReactOS Operating System
6  * 
7  * --------------------------------------------------------------------
8  *
9  * This software is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of the
12  * License, or (at your option) any later version.
13  *
14  * This software is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Library General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this software; see the file COPYING.LIB. If not, write
21  * to the Free Software Foundation, Inc., 675 Mass Ave, Cambridge,
22  * MA 02139, USA.  
23  *
24  * --------------------------------------------------------------------
25  * 
26  *      19990417 (Emanuele Aliberti)
27  *              Do nothing native application skeleton
28  *      19990528 (Emanuele Aliberti)
29  *              Compiled successfully with egcs 1.1.2
30  *      19990605 (Emanuele Aliberti)
31  *              First standalone run under ReactOS (it
32  *              actually does nothing but running).
33  */
34 #include <ddk/ntddk.h>
35 #include <ntdll/rtl.h>
36 #include <csrss/csrss.h>
37
38 #include "api.h"
39
40 /* Native process' entry point */
41
42 VOID STDCALL NtProcessStartup(PPEB Peb)
43 {
44    PRTL_USER_PROCESS_PARAMETERS ProcParams;
45    PWSTR ArgBuffer;
46    PWSTR *argv;
47    ULONG argc = 0;
48    int i = 0;
49    int afterlastspace = 0;
50    OBJECT_ATTRIBUTES ObjectAttributes;
51    HANDLE CsrssInitEvent;
52    UNICODE_STRING UnicodeString;
53    NTSTATUS Status;
54
55    ProcParams = RtlNormalizeProcessParams (Peb->ProcessParameters);
56
57    argv = (PWSTR *)RtlAllocateHeap (Peb->ProcessHeap,
58                                     0, 512 * sizeof(PWSTR));
59    ArgBuffer = (PWSTR)RtlAllocateHeap (Peb->ProcessHeap,
60                                        0,
61                                        ProcParams->CommandLine.Length + sizeof(WCHAR));
62    memcpy (ArgBuffer,
63            ProcParams->CommandLine.Buffer,
64            ProcParams->CommandLine.Length + sizeof(WCHAR));
65
66    while (ArgBuffer[i])
67      {
68         if (ArgBuffer[i] == L' ')
69           {
70              argc++;
71              ArgBuffer[i] = L'\0';
72              argv[argc-1] = &(ArgBuffer[afterlastspace]);
73              i++;
74              while (ArgBuffer[i] == L' ')
75                 i++;
76              afterlastspace = i;
77           }
78         else
79           {
80              i++;
81           }
82      }
83
84    if (ArgBuffer[afterlastspace] != L'\0')
85      {
86         argc++;
87         ArgBuffer[i] = L'\0';
88         argv[argc-1] = &(ArgBuffer[afterlastspace]);
89      }
90    
91    RtlInitUnicodeStringFromLiteral(&UnicodeString,
92                         L"\\CsrssInitDone");
93    InitializeObjectAttributes(&ObjectAttributes,
94                               &UnicodeString,
95                               EVENT_ALL_ACCESS,
96                               0,
97                               NULL);
98    Status = NtOpenEvent(&CsrssInitEvent,
99                         EVENT_ALL_ACCESS,
100                         &ObjectAttributes);
101    if (!NT_SUCCESS(Status))
102      {
103         DbgPrint("CSR: Failed to open csrss notification event\n");
104      }
105    if (CsrServerInitialization (argc, argv) == TRUE)
106      {
107
108         NtSetEvent(CsrssInitEvent,
109                    NULL);
110         
111         RtlFreeHeap (Peb->ProcessHeap,
112                      0, argv);
113         RtlFreeHeap (Peb->ProcessHeap,
114                      0,
115                      ArgBuffer);
116
117         /* terminate the current thread only */
118         NtTerminateThread( NtCurrentThread(), 0 );
119      }
120    else
121      {
122         DisplayString( L"CSR: Subsystem initialization failed.\n" );
123
124         RtlFreeHeap (Peb->ProcessHeap,
125                      0, argv);
126         RtlFreeHeap (Peb->ProcessHeap,
127                      0,
128                      ArgBuffer);
129
130         /*
131          * Tell SM we failed.
132          */
133         NtTerminateProcess( NtCurrentProcess(), 0 );
134      }
135 }
136
137 /* EOF */