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
10 #include <ddk/ntddk.h>
11 #include <ntdll/ldr.h>
15 #include <kernel32/kernel32.h>
16 #include <kernel32/error.h>
19 /* FUNCTIONS ****************************************************************/
23 DisableThreadLibraryCalls (
29 Status = LdrDisableThreadCalloutsForDll ((PVOID)hLibModule);
30 if (!NT_SUCCESS (Status))
32 SetLastErrorByStatus (Status);
45 return LoadLibraryExA (lpLibFileName, 0, 0);
57 UNICODE_STRING LibFileNameU;
58 ANSI_STRING LibFileName;
62 RtlInitAnsiString (&LibFileName,
63 (LPSTR)lpLibFileName);
65 /* convert ansi (or oem) string to unicode */
67 RtlAnsiStringToUnicodeString (&LibFileNameU,
71 RtlOemStringToUnicodeString (&LibFileNameU,
75 Status = LdrLoadDll(NULL,
80 RtlFreeUnicodeString (&LibFileNameU);
82 if ( !NT_SUCCESS(Status))
84 SetLastErrorByStatus (Status);
98 return LoadLibraryExW (lpLibFileName, 0, 0);
105 LPCWSTR lpLibFileName,
110 UNICODE_STRING DllName;
114 if ( lpLibFileName == NULL )
117 RtlInitUnicodeString (&DllName, (LPWSTR)lpLibFileName);
118 Status = LdrLoadDll(NULL, dwFlags, &DllName, (PVOID*)&hInst);
119 if ( !NT_SUCCESS(Status))
121 SetLastErrorByStatus (Status);
131 GetProcAddress( HMODULE hModule, LPCSTR lpProcName )
133 ANSI_STRING ProcedureName;
134 FARPROC fnExp = NULL;
136 if (HIWORD(lpProcName) != 0)
138 RtlInitAnsiString (&ProcedureName,
140 LdrGetProcedureAddress ((PVOID)hModule,
147 LdrGetProcedureAddress ((PVOID)hModule,
159 FreeLibrary( HMODULE hLibModule )
161 LdrUnloadDll(hLibModule);
168 FreeLibraryAndExitThread (
173 if ( FreeLibrary(hLibModule) )
174 ExitThread(dwExitCode);
187 ANSI_STRING FileName;
188 PLIST_ENTRY ModuleListHead;
194 Peb = NtCurrentPeb ();
195 RtlEnterCriticalSection (Peb->LoaderLock);
198 hModule = Peb->ImageBaseAddress;
200 ModuleListHead = &Peb->Ldr->InLoadOrderModuleList;
201 Entry = ModuleListHead->Flink;
203 while (Entry != ModuleListHead)
205 Module = CONTAINING_RECORD(Entry, LDR_MODULE, InLoadOrderModuleList);
206 if (Module->BaseAddress == (PVOID)hModule)
208 if (nSize * sizeof(WCHAR) < Module->FullDllName.Length)
210 SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL);
215 FileName.MaximumLength = nSize * sizeof(WCHAR);
216 FileName.Buffer = lpFilename;
218 /* convert unicode string to ansi (or oem) */
220 RtlUnicodeStringToAnsiString (&FileName,
221 &Module->FullDllName,
224 RtlUnicodeStringToOemString (&FileName,
225 &Module->FullDllName,
227 Length = Module->FullDllName.Length / sizeof(WCHAR);
230 RtlLeaveCriticalSection (Peb->LoaderLock);
234 Entry = Entry->Flink;
237 SetLastErrorByStatus (STATUS_DLL_NOT_FOUND);
238 RtlLeaveCriticalSection (Peb->LoaderLock);
252 UNICODE_STRING FileName;
253 PLIST_ENTRY ModuleListHead;
259 Peb = NtCurrentPeb ();
260 RtlEnterCriticalSection (Peb->LoaderLock);
263 hModule = Peb->ImageBaseAddress;
265 ModuleListHead = &Peb->Ldr->InLoadOrderModuleList;
266 Entry = ModuleListHead->Flink;
267 while (Entry != ModuleListHead)
269 Module = CONTAINING_RECORD(Entry, LDR_MODULE, InLoadOrderModuleList);
271 if (Module->BaseAddress == (PVOID)hModule)
273 if (nSize * sizeof(WCHAR) < Module->FullDllName.Length)
275 SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL);
280 FileName.MaximumLength = nSize * sizeof(WCHAR);
281 FileName.Buffer = lpFilename;
283 RtlCopyUnicodeString (&FileName,
284 &Module->FullDllName);
285 Length = Module->FullDllName.Length / sizeof(WCHAR);
288 RtlLeaveCriticalSection (Peb->LoaderLock);
292 Entry = Entry->Flink;
295 SetLastErrorByStatus (STATUS_DLL_NOT_FOUND);
296 RtlLeaveCriticalSection (Peb->LoaderLock);
304 GetModuleHandleA ( LPCSTR lpModuleName )
306 UNICODE_STRING UnicodeName;
307 ANSI_STRING ModuleName;
311 if (lpModuleName == NULL)
312 return ((HMODULE)NtCurrentPeb()->ImageBaseAddress);
313 RtlInitAnsiString (&ModuleName,
314 (LPSTR)lpModuleName);
316 /* convert ansi (or oem) string to unicode */
318 RtlAnsiStringToUnicodeString (&UnicodeName,
322 RtlOemStringToUnicodeString (&UnicodeName,
326 Status = LdrGetDllHandle (0,
331 RtlFreeUnicodeString (&UnicodeName);
333 if (!NT_SUCCESS(Status))
335 SetLastErrorByStatus (Status);
339 return ((HMODULE)BaseAddress);
345 GetModuleHandleW (LPCWSTR lpModuleName)
347 UNICODE_STRING ModuleName;
351 if (lpModuleName == NULL)
352 return ((HMODULE)NtCurrentPeb()->ImageBaseAddress);
354 RtlInitUnicodeString (&ModuleName,
355 (LPWSTR)lpModuleName);
357 Status = LdrGetDllHandle (0,
361 if (!NT_SUCCESS(Status))
363 SetLastErrorByStatus (Status);
367 return ((HMODULE)BaseAddress);