Fix code assuming that the Reg* functions set the last error code
[reactos.git] / reactos / lib / userenv / misc.c
1 /*
2 * ReactOS kernel
3 * Copyright (C) 2004 ReactOS Team
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19 /* $Id$
20 *
21 * COPYRIGHT: See COPYING in the top level directory
22 * PROJECT: ReactOS system libraries
23 * FILE: lib/userenv/misc.c
24 * PURPOSE: User profile code
25 * PROGRAMMER: Eric Kohl
26 */
27
28 #include <precomp.h>
29
30 #define NDEBUG
31 #include <debug.h>
32
33
34 /* FUNCTIONS ***************************************************************/
35
36 LPWSTR
37 AppendBackslash (LPWSTR String)
38 {
39 ULONG Length;
40
41 Length = lstrlenW (String);
42 if (String[Length - 1] != L'\\')
43 {
44 String[Length] = L'\\';
45 Length++;
46 String[Length] = (WCHAR)0;
47 }
48
49 return &String[Length];
50 }
51
52
53 BOOL
54 GetUserSidFromToken (HANDLE hToken,
55 PUNICODE_STRING SidString)
56 {
57 PSID_AND_ATTRIBUTES SidBuffer, nsb;
58 ULONG Length;
59 NTSTATUS Status;
60
61 Length = 256;
62 SidBuffer = LocalAlloc (LMEM_FIXED,
63 Length);
64 if (SidBuffer == NULL)
65 return FALSE;
66
67 Status = NtQueryInformationToken (hToken,
68 TokenUser,
69 (PVOID)SidBuffer,
70 Length,
71 &Length);
72 if (Status == STATUS_BUFFER_TOO_SMALL)
73 {
74 nsb = LocalReAlloc (SidBuffer,
75 Length,
76 LMEM_MOVEABLE);
77 if (nsb == NULL)
78 {
79 LocalFree((HLOCAL)SidBuffer);
80 return FALSE;
81 }
82
83 SidBuffer = nsb;
84 Status = NtQueryInformationToken (hToken,
85 TokenUser,
86 (PVOID)SidBuffer,
87 Length,
88 &Length);
89 }
90
91 if (!NT_SUCCESS (Status))
92 {
93 LocalFree ((HLOCAL)SidBuffer);
94 SetLastError (RtlNtStatusToDosError (Status));
95 return FALSE;
96 }
97
98 DPRINT ("SidLength: %lu\n", RtlLengthSid (SidBuffer[0].Sid));
99
100 Status = RtlConvertSidToUnicodeString (SidString,
101 SidBuffer[0].Sid,
102 TRUE);
103
104 LocalFree ((HLOCAL)SidBuffer);
105
106 if (!NT_SUCCESS (Status))
107 {
108 SetLastError (RtlNtStatusToDosError (Status));
109 return FALSE;
110 }
111
112 DPRINT ("SidString.Length: %lu\n", SidString->Length);
113 DPRINT ("SidString.MaximumLength: %lu\n", SidString->MaximumLength);
114 DPRINT ("SidString: '%wZ'\n", SidString);
115
116 return TRUE;
117 }
118
119 PVOID
120 CreateDefaultSD(VOID)
121 {
122 /* FIXME - create a default security descriptor */
123 return NULL;
124 }
125
126 /* Dynamic DLL loading interface **********************************************/
127
128 /* OLE32.DLL import table */
129 DYN_MODULE DynOle32 =
130 {
131 L"ole32.dll",
132 {
133 "CoInitialize",
134 "CoCreateInstance",
135 "CoUninitialize",
136 NULL
137 }
138 };
139
140
141 /*
142 * Use this function to load functions from other modules. We cannot statically
143 * link to e.g. ole32.dll because those dlls would get loaded on startup with
144 * winlogon and they may try to register classes etc when not even a window station
145 * has been created!
146 */
147 BOOL
148 LoadDynamicImports(PDYN_MODULE Module, PDYN_FUNCS DynFuncs)
149 {
150 LPSTR *fname;
151 PVOID *fn;
152
153 ZeroMemory(DynFuncs, sizeof(DYN_FUNCS));
154
155 DynFuncs->hModule = LoadLibraryW(Module->Library);
156 if (!DynFuncs->hModule)
157 {
158 return FALSE;
159 }
160
161 fn = &DynFuncs->fn.foo;
162
163 /* load the imports */
164 for (fname = Module->Functions; *fname != NULL; fname++)
165 {
166 *fn = GetProcAddress(DynFuncs->hModule, *fname);
167 if (*fn == NULL)
168 {
169 FreeLibrary(DynFuncs->hModule);
170 DynFuncs->hModule = (HMODULE)0;
171
172 return FALSE;
173 }
174
175 fn++;
176 }
177
178 return TRUE;
179 }
180
181
182 VOID
183 UnloadDynamicImports(PDYN_FUNCS DynFuncs)
184 {
185 if (DynFuncs->hModule)
186 {
187 FreeLibrary(DynFuncs->hModule);
188 DynFuncs->hModule = (HMODULE)0;
189 }
190 }
191
192 /* EOF */