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 ****************************************************************/
23 DisableThreadLibraryCalls (
29 Status = LdrDisableThreadCalloutsForDll ((PVOID)hLibModule);
30 if (!NT_SUCCESS (Status))
32 SetLastErrorByStatus (Status);
48 return LoadLibraryExA (lpLibFileName, 0, 0);
63 UNICODE_STRING LibFileNameU;
64 ANSI_STRING LibFileName;
70 RtlInitAnsiString (&LibFileName,
71 (LPSTR)lpLibFileName);
73 /* convert ansi (or oem) string to unicode */
75 RtlAnsiStringToUnicodeString (&LibFileNameU,
79 RtlOemStringToUnicodeString (&LibFileNameU,
83 Status = LdrLoadDll(NULL,
88 RtlFreeUnicodeString (&LibFileNameU);
90 if ( !NT_SUCCESS(Status))
92 SetLastErrorByStatus (Status);
106 LPCWSTR lpLibFileName
109 return LoadLibraryExW (lpLibFileName, 0, 0);
119 LPCWSTR lpLibFileName,
124 UNICODE_STRING DllName;
130 if ( lpLibFileName == NULL )
133 RtlInitUnicodeString (&DllName, (LPWSTR)lpLibFileName);
134 Status = LdrLoadDll(NULL, dwFlags, &DllName, (PVOID*)&hInst);
135 if ( !NT_SUCCESS(Status))
137 SetLastErrorByStatus (Status);
150 GetProcAddress( HMODULE hModule, LPCSTR lpProcName )
152 ANSI_STRING ProcedureName;
153 FARPROC fnExp = NULL;
155 if (HIWORD(lpProcName) != 0)
157 RtlInitAnsiString (&ProcedureName,
159 LdrGetProcedureAddress ((PVOID)hModule,
166 LdrGetProcedureAddress ((PVOID)hModule,
181 FreeLibrary( HMODULE hLibModule )
183 LdrUnloadDll(hLibModule);
193 FreeLibraryAndExitThread (
198 if ( FreeLibrary(hLibModule) )
199 ExitThread(dwExitCode);
215 ANSI_STRING FileName;
216 PLIST_ENTRY ModuleListHead;
222 Peb = NtCurrentPeb ();
223 RtlEnterCriticalSection (Peb->LoaderLock);
226 hModule = Peb->ImageBaseAddress;
228 ModuleListHead = &Peb->Ldr->InLoadOrderModuleList;
229 Entry = ModuleListHead->Flink;
231 while (Entry != ModuleListHead)
233 Module = CONTAINING_RECORD(Entry, LDR_MODULE, InLoadOrderModuleList);
234 if (Module->BaseAddress == (PVOID)hModule)
236 if (nSize * sizeof(WCHAR) < Module->FullDllName.Length)
238 SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL);
243 FileName.MaximumLength = nSize * sizeof(WCHAR);
244 FileName.Buffer = lpFilename;
246 /* convert unicode string to ansi (or oem) */
248 RtlUnicodeStringToAnsiString (&FileName,
249 &Module->FullDllName,
252 RtlUnicodeStringToOemString (&FileName,
253 &Module->FullDllName,
255 Length = Module->FullDllName.Length / sizeof(WCHAR);
258 RtlLeaveCriticalSection (Peb->LoaderLock);
262 Entry = Entry->Flink;
265 SetLastErrorByStatus (STATUS_DLL_NOT_FOUND);
266 RtlLeaveCriticalSection (Peb->LoaderLock);
283 UNICODE_STRING FileName;
284 PLIST_ENTRY ModuleListHead;
290 Peb = NtCurrentPeb ();
291 RtlEnterCriticalSection (Peb->LoaderLock);
294 hModule = Peb->ImageBaseAddress;
296 ModuleListHead = &Peb->Ldr->InLoadOrderModuleList;
297 Entry = ModuleListHead->Flink;
298 while (Entry != ModuleListHead)
300 Module = CONTAINING_RECORD(Entry, LDR_MODULE, InLoadOrderModuleList);
302 if (Module->BaseAddress == (PVOID)hModule)
304 if (nSize * sizeof(WCHAR) < Module->FullDllName.Length)
306 SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL);
311 FileName.MaximumLength = nSize * sizeof(WCHAR);
312 FileName.Buffer = lpFilename;
314 RtlCopyUnicodeString (&FileName,
315 &Module->FullDllName);
316 Length = Module->FullDllName.Length / sizeof(WCHAR);
319 RtlLeaveCriticalSection (Peb->LoaderLock);
323 Entry = Entry->Flink;
326 SetLastErrorByStatus (STATUS_DLL_NOT_FOUND);
327 RtlLeaveCriticalSection (Peb->LoaderLock);
338 GetModuleHandleA ( LPCSTR lpModuleName )
340 UNICODE_STRING UnicodeName;
341 ANSI_STRING ModuleName;
345 if (lpModuleName == NULL)
346 return ((HMODULE)NtCurrentPeb()->ImageBaseAddress);
347 RtlInitAnsiString (&ModuleName,
348 (LPSTR)lpModuleName);
350 /* convert ansi (or oem) string to unicode */
352 RtlAnsiStringToUnicodeString (&UnicodeName,
356 RtlOemStringToUnicodeString (&UnicodeName,
360 Status = LdrGetDllHandle (0,
365 RtlFreeUnicodeString (&UnicodeName);
367 if (!NT_SUCCESS(Status))
369 SetLastErrorByStatus (Status);
373 return ((HMODULE)BaseAddress);
382 GetModuleHandleW (LPCWSTR lpModuleName)
384 UNICODE_STRING ModuleName;
388 if (lpModuleName == NULL)
389 return ((HMODULE)NtCurrentPeb()->ImageBaseAddress);
391 RtlInitUnicodeString (&ModuleName,
392 (LPWSTR)lpModuleName);
394 Status = LdrGetDllHandle (0,
398 if (!NT_SUCCESS(Status))
400 SetLastErrorByStatus (Status);
404 return ((HMODULE)BaseAddress);