3 * COPYRIGHT: See COPYING in the top level directory
4 * PROJECT : ReactOS user mode libraries
5 * MODULE : kernel32.dll
6 * FILE : reactos/lib/kernel32/misc/ldr.c
7 * AUTHOR : Boudewijn Dekker
13 #include <kernel32/kernel32.h>
16 /* FUNCTIONS ****************************************************************/
20 DisableThreadLibraryCalls (
26 Status = LdrDisableThreadCalloutsForDll ((PVOID)hLibModule);
27 if (!NT_SUCCESS (Status))
29 SetLastErrorByStatus (Status);
42 return LoadLibraryExA (lpLibFileName, 0, 0);
54 UNICODE_STRING LibFileNameU;
55 ANSI_STRING LibFileName;
61 RtlInitAnsiString (&LibFileName,
62 (LPSTR)lpLibFileName);
64 /* convert ansi (or oem) string to unicode */
66 RtlAnsiStringToUnicodeString (&LibFileNameU,
70 RtlOemStringToUnicodeString (&LibFileNameU,
74 Status = LdrLoadDll(NULL,
79 RtlFreeUnicodeString (&LibFileNameU);
81 if ( !NT_SUCCESS(Status))
83 SetLastErrorByStatus (Status);
97 return LoadLibraryExW (lpLibFileName, 0, 0);
104 LPCWSTR lpLibFileName,
109 UNICODE_STRING DllName;
115 if ( lpLibFileName == NULL )
118 RtlInitUnicodeString (&DllName, (LPWSTR)lpLibFileName);
119 Status = LdrLoadDll(NULL, dwFlags, &DllName, (PVOID*)&hInst);
120 if ( !NT_SUCCESS(Status))
122 SetLastErrorByStatus (Status);
132 GetProcAddress( HMODULE hModule, LPCSTR lpProcName )
134 ANSI_STRING ProcedureName;
135 FARPROC fnExp = NULL;
137 if (HIWORD(lpProcName) != 0)
139 RtlInitAnsiString (&ProcedureName,
141 LdrGetProcedureAddress ((PVOID)hModule,
148 LdrGetProcedureAddress ((PVOID)hModule,
160 FreeLibrary( HMODULE hLibModule )
162 LdrUnloadDll(hLibModule);
169 FreeLibraryAndExitThread (
174 if ( FreeLibrary(hLibModule) )
175 ExitThread(dwExitCode);
188 ANSI_STRING FileName;
189 PLIST_ENTRY ModuleListHead;
195 Peb = NtCurrentPeb ();
196 RtlEnterCriticalSection (Peb->LoaderLock);
199 hModule = Peb->ImageBaseAddress;
201 ModuleListHead = &Peb->Ldr->InLoadOrderModuleList;
202 Entry = ModuleListHead->Flink;
204 while (Entry != ModuleListHead)
206 Module = CONTAINING_RECORD(Entry, LDR_MODULE, InLoadOrderModuleList);
207 if (Module->BaseAddress == (PVOID)hModule)
209 if (nSize * sizeof(WCHAR) < Module->FullDllName.Length)
211 SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL);
216 FileName.MaximumLength = nSize * sizeof(WCHAR);
217 FileName.Buffer = lpFilename;
219 /* convert unicode string to ansi (or oem) */
221 RtlUnicodeStringToAnsiString (&FileName,
222 &Module->FullDllName,
225 RtlUnicodeStringToOemString (&FileName,
226 &Module->FullDllName,
228 Length = Module->FullDllName.Length / sizeof(WCHAR);
231 RtlLeaveCriticalSection (Peb->LoaderLock);
235 Entry = Entry->Flink;
238 SetLastErrorByStatus (STATUS_DLL_NOT_FOUND);
239 RtlLeaveCriticalSection (Peb->LoaderLock);
253 UNICODE_STRING FileName;
254 PLIST_ENTRY ModuleListHead;
260 Peb = NtCurrentPeb ();
261 RtlEnterCriticalSection (Peb->LoaderLock);
264 hModule = Peb->ImageBaseAddress;
266 ModuleListHead = &Peb->Ldr->InLoadOrderModuleList;
267 Entry = ModuleListHead->Flink;
268 while (Entry != ModuleListHead)
270 Module = CONTAINING_RECORD(Entry, LDR_MODULE, InLoadOrderModuleList);
272 if (Module->BaseAddress == (PVOID)hModule)
274 if (nSize * sizeof(WCHAR) < Module->FullDllName.Length)
276 SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL);
281 FileName.MaximumLength = nSize * sizeof(WCHAR);
282 FileName.Buffer = lpFilename;
284 RtlCopyUnicodeString (&FileName,
285 &Module->FullDllName);
286 Length = Module->FullDllName.Length / sizeof(WCHAR);
289 RtlLeaveCriticalSection (Peb->LoaderLock);
293 Entry = Entry->Flink;
296 SetLastErrorByStatus (STATUS_DLL_NOT_FOUND);
297 RtlLeaveCriticalSection (Peb->LoaderLock);
305 GetModuleHandleA ( LPCSTR lpModuleName )
307 UNICODE_STRING UnicodeName;
308 ANSI_STRING ModuleName;
312 if (lpModuleName == NULL)
313 return ((HMODULE)NtCurrentPeb()->ImageBaseAddress);
314 RtlInitAnsiString (&ModuleName,
315 (LPSTR)lpModuleName);
317 /* convert ansi (or oem) string to unicode */
319 RtlAnsiStringToUnicodeString (&UnicodeName,
323 RtlOemStringToUnicodeString (&UnicodeName,
327 Status = LdrGetDllHandle (0,
332 RtlFreeUnicodeString (&UnicodeName);
334 if (!NT_SUCCESS(Status))
336 SetLastErrorByStatus (Status);
340 return ((HMODULE)BaseAddress);
346 GetModuleHandleW (LPCWSTR lpModuleName)
348 UNICODE_STRING ModuleName;
352 if (lpModuleName == NULL)
353 return ((HMODULE)NtCurrentPeb()->ImageBaseAddress);
355 RtlInitUnicodeString (&ModuleName,
356 (LPWSTR)lpModuleName);
358 Status = LdrGetDllHandle (0,
362 if (!NT_SUCCESS(Status))
364 SetLastErrorByStatus (Status);
368 return ((HMODULE)BaseAddress);