- Add proper file header.
[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: misc.c,v 1.7 2004/09/30 20:23:00 ekohl Exp $
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
31 /* FUNCTIONS ***************************************************************/
32
33 LPWSTR
34 AppendBackslash (LPWSTR String)
35 {
36 ULONG Length;
37
38 Length = lstrlenW (String);
39 if (String[Length - 1] != L'\\')
40 {
41 String[Length] = L'\\';
42 Length++;
43 String[Length] = (WCHAR)0;
44 }
45
46 return &String[Length];
47 }
48
49
50 BOOL
51 GetUserSidFromToken (HANDLE hToken,
52 PUNICODE_STRING SidString)
53 {
54 PSID_AND_ATTRIBUTES SidBuffer;
55 ULONG Length;
56 NTSTATUS Status;
57
58 Length = 256;
59 SidBuffer = LocalAlloc (0,
60 Length);
61 if (SidBuffer == NULL)
62 return FALSE;
63
64 Status = NtQueryInformationToken (hToken,
65 TokenUser,
66 (PVOID)SidBuffer,
67 Length,
68 &Length);
69 if (Status == STATUS_BUFFER_TOO_SMALL)
70 {
71 SidBuffer = LocalReAlloc (SidBuffer,
72 Length,
73 0);
74 if (SidBuffer == NULL)
75 return FALSE;
76
77 Status = NtQueryInformationToken (hToken,
78 TokenUser,
79 (PVOID)SidBuffer,
80 Length,
81 &Length);
82 }
83
84 if (!NT_SUCCESS (Status))
85 {
86 LocalFree (SidBuffer);
87 return FALSE;
88 }
89
90 DPRINT ("SidLength: %lu\n", RtlLengthSid (SidBuffer[0].Sid));
91
92 Status = RtlConvertSidToUnicodeString (SidString,
93 SidBuffer[0].Sid,
94 TRUE);
95
96 LocalFree (SidBuffer);
97
98 if (!NT_SUCCESS (Status))
99 return FALSE;
100
101 DPRINT ("SidString.Length: %lu\n", SidString->Length);
102 DPRINT ("SidString.MaximumLength: %lu\n", SidString->MaximumLength);
103 DPRINT ("SidString: '%wZ'\n", SidString);
104
105 return TRUE;
106 }
107
108 /* Dynamic DLL loading interface **********************************************/
109
110 /* OLE32.DLL import table */
111 DYN_MODULE DynOle32 =
112 {
113 L"ole32.dll",
114 {
115 "CoInitialize",
116 "CoCreateInstance",
117 "CoUninitialize",
118 NULL
119 }
120 };
121
122
123 /*
124 * Use this function to load functions from other modules. We cannot statically
125 * link to e.g. ole32.dll because those dlls would get loaded on startup with
126 * winlogon and they may try to register classes etc when not even a window station
127 * has been created!
128 */
129 BOOL
130 LoadDynamicImports(PDYN_MODULE Module, PDYN_FUNCS DynFuncs)
131 {
132 LPSTR *fname;
133 PVOID *fn;
134
135 ZeroMemory(DynFuncs, sizeof(DYN_FUNCS));
136
137 DynFuncs->hModule = LoadLibraryW(Module->Library);
138 if (!DynFuncs->hModule)
139 {
140 return FALSE;
141 }
142
143 fn = &DynFuncs->fn.foo;
144
145 /* load the imports */
146 for (fname = Module->Functions; *fname != NULL; fname++)
147 {
148 *fn = GetProcAddress(DynFuncs->hModule, *fname);
149 if (*fn == NULL)
150 {
151 FreeLibrary(DynFuncs->hModule);
152 DynFuncs->hModule = (HMODULE)0;
153
154 return FALSE;
155 }
156
157 fn++;
158 }
159
160 return TRUE;
161 }
162
163
164 VOID
165 UnloadDynamicImports(PDYN_FUNCS DynFuncs)
166 {
167 if (DynFuncs->hModule)
168 {
169 FreeLibrary(DynFuncs->hModule);
170 DynFuncs->hModule = (HMODULE)0;
171 }
172 }
173
174 /* EOF */