2004-08-15 Casper S. Hornstrup <chorns@users.sourceforge.net>
[reactos.git] / reactos / lib / userenv / misc.c
1 /* $Id: misc.c,v 1.6 2004/08/15 19:02:40 chorns Exp $
2 *
3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT: ReactOS system libraries
5 * FILE: lib/userenv/misc.c
6 * PURPOSE: User profile code
7 * PROGRAMMER: Eric Kohl
8 */
9
10 #include "precomp.h"
11
12
13 /* FUNCTIONS ***************************************************************/
14
15 LPWSTR
16 AppendBackslash (LPWSTR String)
17 {
18 ULONG Length;
19
20 Length = lstrlenW (String);
21 if (String[Length - 1] != L'\\')
22 {
23 String[Length] = L'\\';
24 Length++;
25 String[Length] = (WCHAR)0;
26 }
27
28 return &String[Length];
29 }
30
31
32 BOOL
33 GetUserSidFromToken (HANDLE hToken,
34 PUNICODE_STRING SidString)
35 {
36 PSID_AND_ATTRIBUTES SidBuffer;
37 ULONG Length;
38 NTSTATUS Status;
39
40 Length = 256;
41 SidBuffer = LocalAlloc (0,
42 Length);
43 if (SidBuffer == NULL)
44 return FALSE;
45
46 Status = NtQueryInformationToken (hToken,
47 TokenUser,
48 (PVOID)SidBuffer,
49 Length,
50 &Length);
51 if (Status == STATUS_BUFFER_TOO_SMALL)
52 {
53 SidBuffer = LocalReAlloc (SidBuffer,
54 Length,
55 0);
56 if (SidBuffer == NULL)
57 return FALSE;
58
59 Status = NtQueryInformationToken (hToken,
60 TokenUser,
61 (PVOID)SidBuffer,
62 Length,
63 &Length);
64 }
65
66 if (!NT_SUCCESS (Status))
67 {
68 LocalFree (SidBuffer);
69 return FALSE;
70 }
71
72 DPRINT ("SidLength: %lu\n", RtlLengthSid (SidBuffer[0].Sid));
73
74 Status = RtlConvertSidToUnicodeString (SidString,
75 SidBuffer[0].Sid,
76 TRUE);
77
78 LocalFree (SidBuffer);
79
80 if (!NT_SUCCESS (Status))
81 return FALSE;
82
83 DPRINT ("SidString.Length: %lu\n", SidString->Length);
84 DPRINT ("SidString.MaximumLength: %lu\n", SidString->MaximumLength);
85 DPRINT ("SidString: '%wZ'\n", SidString);
86
87 return TRUE;
88 }
89
90 /* Dynamic DLL loading interface **********************************************/
91
92 /* OLE32.DLL import table */
93 DYN_MODULE DynOle32 =
94 {
95 L"ole32.dll",
96 {
97 "CoInitialize",
98 "CoCreateInstance",
99 "CoUninitialize",
100 NULL
101 }
102 };
103
104 /*
105 Use this function to load functions from other modules. We cannot statically
106 link to e.g. ole32.dll because those dlls would get loaded on startup with
107 winlogon and they may try to register classes etc when not even a window station
108 has been created!
109 */
110
111 BOOL
112 LoadDynamicImports(PDYN_MODULE Module, PDYN_FUNCS DynFuncs)
113 {
114 LPSTR *fname;
115 PVOID *fn;
116
117 ZeroMemory(DynFuncs, sizeof(DYN_FUNCS));
118
119 DynFuncs->hModule = LoadLibraryW(Module->Library);
120 if(!DynFuncs->hModule)
121 {
122 return FALSE;
123 }
124
125 fn = &DynFuncs->fn.foo;
126
127 /* load the imports */
128 for(fname = Module->Functions; *fname != NULL; fname++)
129 {
130 *fn = GetProcAddress(DynFuncs->hModule, *fname);
131 if(*fn == NULL)
132 {
133 FreeLibrary(DynFuncs->hModule);
134 DynFuncs->hModule = (HMODULE)0;
135 return FALSE;
136 }
137 fn++;
138 }
139
140 return TRUE;
141 }
142
143 VOID
144 UnloadDynamicImports(PDYN_FUNCS DynFuncs)
145 {
146 if(DynFuncs->hModule)
147 {
148 FreeLibrary(DynFuncs->hModule);
149 DynFuncs->hModule = (HMODULE)0;
150 }
151 }
152
153 /* EOF */