2 * COPYRIGHT: See COPYING in the top level directory
3 * PROJECT : ReactOS system libraries
4 * MODULE : kernel32.dll
5 * FILE : reactos/dll/win32/kernel32/misc/ldr.c
6 * AUTHOR : Aleksey Bragin <aleksey@reactos.org>
14 /* FUNCTIONS ****************************************************************/
18 BasepInitializeTermsrvFpns(VOID
)
21 return STATUS_NOT_IMPLEMENTED
;
26 BasepGetModuleHandleExParameterValidation(DWORD dwFlags
,
27 LPCWSTR lpwModuleName
,
30 /* Set phModule to 0 if it's not a NULL pointer */
31 if (phModule
) *phModule
= 0;
33 /* Check for invalid flags combination */
34 if (dwFlags
& ~(GET_MODULE_HANDLE_EX_FLAG_PIN
|
35 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
|
36 GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
) ||
37 ((dwFlags
& GET_MODULE_HANDLE_EX_FLAG_PIN
) &&
38 (dwFlags
& GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
)) ||
39 (!lpwModuleName
&& (dwFlags
& GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
))
42 BaseSetLastNTError(STATUS_INVALID_PARAMETER_1
);
43 return BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_ERROR
;
46 /* Check 2nd parameter */
49 BaseSetLastNTError(STATUS_INVALID_PARAMETER_2
);
50 return BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_ERROR
;
53 /* Return what we have according to the module name */
56 return BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_CONTINUE
;
59 /* No name given, so put ImageBaseAddress there */
60 *phModule
= (HMODULE
)NtCurrentPeb()->ImageBaseAddress
;
62 return BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_SUCCESS
;
67 BasepMapModuleHandle(HMODULE hModule
, BOOLEAN AsDataFile
)
69 /* If no handle is provided - use current image base address */
70 if (!hModule
) return NtCurrentPeb()->ImageBaseAddress
;
72 /* Check if it's a normal or a datafile one */
73 if (LDR_IS_DATAFILE(hModule
) && !AsDataFile
)
76 /* It's a normal DLL, just return its handle */
85 DisableThreadLibraryCalls(
86 IN HMODULE hLibModule
)
90 /* Disable thread library calls */
91 Status
= LdrDisableThreadCalloutsForDll((PVOID
)hLibModule
);
93 /* If it wasn't success - set last error and return failure */
94 if (!NT_SUCCESS(Status
))
96 BaseSetLastNTError(Status
);
111 LoadLibraryA(LPCSTR lpLibFileName
)
117 /* Treat twain_32.dll in a special way (what a surprise...) */
118 if (lpLibFileName
&& !_strcmpi(lpLibFileName
, "twain_32.dll"))
120 /* Allocate space for the buffer */
121 PathBuffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, MAX_PATH
);
124 /* Get windows dir in this buffer */
125 Len
= GetWindowsDirectoryA(PathBuffer
, MAX_PATH
- 13); /* 13 is sizeof of '\\twain_32.dll' */
126 if (Len
&& Len
< (MAX_PATH
- 13))
128 /* We successfully got windows directory. Concatenate twain_32.dll to it */
129 strncat(PathBuffer
, "\\twain_32.dll", 13);
131 /* And recursively call ourselves with a new string */
132 Result
= LoadLibraryA(PathBuffer
);
134 /* If it was successful - free memory and return result */
137 RtlFreeHeap(RtlGetProcessHeap(), 0, PathBuffer
);
142 /* Free allocated buffer */
143 RtlFreeHeap(RtlGetProcessHeap(), 0, PathBuffer
);
147 /* Call the Ex version of the API */
148 return LoadLibraryExA(lpLibFileName
, 0, 0);
157 LoadLibraryExA(LPCSTR lpLibFileName
,
161 PUNICODE_STRING FileNameW
;
163 /* Convert file name to unicode */
164 if (!(FileNameW
= Basep8BitStringToStaticUnicodeString(lpLibFileName
)))
167 /* And call W version of the API */
168 return LoadLibraryExW(FileNameW
->Buffer
, hFile
, dwFlags
);
177 LoadLibraryW(LPCWSTR lpLibFileName
)
179 /* Call Ex version of the API */
180 return LoadLibraryExW(lpLibFileName
, 0, 0);
186 BasepLoadLibraryAsDatafile(PWSTR Path
, LPCWSTR Name
, HMODULE
*hModule
)
188 WCHAR FilenameW
[MAX_PATH
];
189 HANDLE hFile
= INVALID_HANDLE_VALUE
;
192 PVOID lpBaseAddress
= NULL
;
194 //PUNICODE_STRING OriginalName;
195 //UNICODE_STRING dotDLL = RTL_CONSTANT_STRING(L".DLL");
197 /* Zero out handle value */
200 DPRINT("BasepLoadLibraryAsDatafile(%S %S %p)\n", Path
, Name
, hModule
);
202 /*Status = RtlDosApplyFileIsolationRedirection_Ustr(TRUE,
212 /* Try to search for it */
213 if (!SearchPathW(Path
,
216 sizeof(FilenameW
) / sizeof(FilenameW
[0]),
220 /* Return last status value directly */
221 return NtCurrentTeb()->LastStatusValue
;
224 /* Open this file we found */
225 hFile
= CreateFileW(FilenameW
,
227 FILE_SHARE_READ
| FILE_SHARE_DELETE
,
233 /* If opening failed - return last status value */
234 if (hFile
== INVALID_HANDLE_VALUE
) return NtCurrentTeb()->LastStatusValue
;
236 /* Create file mapping */
237 hMapping
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
239 /* Close the file handle */
242 /* If creating file mapping failed - return last status value */
243 if (!hMapping
) return NtCurrentTeb()->LastStatusValue
;
245 /* Map view of section */
246 Status
= NtMapViewOfSection(hMapping
,
257 /* Close handle to the section */
258 CloseHandle(hMapping
);
260 /* If mapping view of section failed - return last status value */
261 if (!NT_SUCCESS(Status
)) return NtCurrentTeb()->LastStatusValue
;
263 /* Make sure it's a valid PE file */
264 if (!RtlImageNtHeader(lpBaseAddress
))
266 /* Unmap the view and return failure status */
267 UnmapViewOfFile(lpBaseAddress
);
268 return STATUS_INVALID_IMAGE_FORMAT
;
271 /* Set low bit of handle to indicate datafile module */
272 *hModule
= (HMODULE
)((ULONG_PTR
)lpBaseAddress
| 1);
274 /* Load alternate resource module */
275 //LdrLoadAlternateResourceModule(*hModule, FilenameW);
277 return STATUS_SUCCESS
;
286 LoadLibraryExW(LPCWSTR lpLibFileName
,
290 UNICODE_STRING DllName
;
294 ULONG DllCharacteristics
= 0;
295 BOOL FreeString
= FALSE
;
297 /* Check for any flags LdrLoadDll might be interested in */
298 if (dwFlags
& DONT_RESOLVE_DLL_REFERENCES
)
300 /* Tell LDR to treat it as an EXE */
301 DllCharacteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
;
304 /* Build up a unicode dll name from null-terminated string */
305 RtlInitUnicodeString(&DllName
, (LPWSTR
)lpLibFileName
);
307 /* Lazy-initialize BasepExeLdrEntry */
308 if (!BasepExeLdrEntry
)
309 LdrEnumerateLoadedModules(0, BasepLocateExeLdrEntry
, NtCurrentPeb()->ImageBaseAddress
);
311 /* Check if that module is our exe*/
312 if (BasepExeLdrEntry
&& !(dwFlags
& LOAD_LIBRARY_AS_DATAFILE
) &&
313 DllName
.Length
== BasepExeLdrEntry
->FullDllName
.Length
)
315 /* Lengths match and it's not a datafile, so perform name comparison */
316 if (RtlEqualUnicodeString(&DllName
, &BasepExeLdrEntry
->FullDllName
, TRUE
))
319 return BasepExeLdrEntry
->DllBase
;
323 /* Check for trailing spaces and remove them if necessary */
324 if (DllName
.Buffer
[DllName
.Length
/sizeof(WCHAR
) - 1] == L
' ')
326 RtlCreateUnicodeString(&DllName
, (LPWSTR
)lpLibFileName
);
327 while (DllName
.Length
> sizeof(WCHAR
) &&
328 DllName
.Buffer
[DllName
.Length
/sizeof(WCHAR
) - 1] == L
' ')
330 DllName
.Length
-= sizeof(WCHAR
);
332 DllName
.Buffer
[DllName
.Length
/sizeof(WCHAR
)] = UNICODE_NULL
;
336 /* Compute the load path */
337 SearchPath
= BaseComputeProcessDllPath((dwFlags
& LOAD_WITH_ALTERED_SEARCH_PATH
) ?
338 DllName
.Buffer
: NULL
,
342 /* Getting DLL path failed, so set last error, free mem and return */
343 BaseSetLastNTError(STATUS_NO_MEMORY
);
344 if (FreeString
) RtlFreeUnicodeString(&DllName
);
350 if (dwFlags
& LOAD_LIBRARY_AS_DATAFILE
)
352 /* If the image is loaded as a datafile, try to get its handle */
353 Status
= LdrGetDllHandleEx(0, SearchPath
, NULL
, &DllName
, (PVOID
*)&hInst
);
354 if (!NT_SUCCESS(Status
))
356 /* It's not loaded yet - so load it up */
357 Status
= BasepLoadLibraryAsDatafile(SearchPath
, DllName
.Buffer
, &hInst
);
359 _SEH2_YIELD(goto done
;)
362 /* Call the API Properly */
363 Status
= LdrLoadDll(SearchPath
,
368 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
370 Status
= _SEH2_GetExceptionCode();
375 /* Free SearchPath buffer */
376 RtlFreeHeap(RtlGetProcessHeap(), 0, SearchPath
);
378 /* Free DllName string if it was dynamically allocated */
379 if (FreeString
) RtlFreeUnicodeString(&DllName
);
381 /* Set last error in failure case */
382 if (!NT_SUCCESS(Status
))
384 DPRINT1("LoadLibraryExW(%ls) failing with status %lx\n", lpLibFileName
, Status
);
385 BaseSetLastNTError(Status
);
389 /* Return loaded module handle */
399 GetProcAddress(HMODULE hModule
, LPCSTR lpProcName
)
401 ANSI_STRING ProcedureName
, *ProcNamePtr
= NULL
;
402 FARPROC fnExp
= NULL
;
407 if (HIWORD(lpProcName
) != 0)
409 /* Look up by name */
410 RtlInitAnsiString(&ProcedureName
, (LPSTR
)lpProcName
);
411 ProcNamePtr
= &ProcedureName
;
415 /* Look up by ordinal */
416 Ordinal
= (ULONG
)lpProcName
;
419 /* Map provided handle */
420 hMapped
= BasepMapModuleHandle(hModule
, FALSE
);
422 /* Get the proc address */
423 Status
= LdrGetProcedureAddress(hMapped
,
428 if (!NT_SUCCESS(Status
))
430 BaseSetLastNTError(Status
);
434 /* Check for a special case when returned pointer is
435 the same as image's base address */
436 if (fnExp
== hMapped
)
438 /* Set correct error code */
439 if (HIWORD(lpProcName
) != 0)
440 BaseSetLastNTError(STATUS_ENTRYPOINT_NOT_FOUND
);
442 BaseSetLastNTError(STATUS_ORDINAL_NOT_FOUND
);
447 /* All good, return procedure pointer */
458 FreeLibrary(HINSTANCE hLibModule
)
461 PIMAGE_NT_HEADERS NtHeaders
;
463 if (LDR_IS_DATAFILE(hLibModule
))
465 // FIXME: This SEH should go inside RtlImageNtHeader instead
468 /* This is a LOAD_LIBRARY_AS_DATAFILE module, check if it's a valid one */
469 NtHeaders
= RtlImageNtHeader((PVOID
)((ULONG_PTR
)hLibModule
& ~1));
471 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
479 Status
= NtUnmapViewOfSection(NtCurrentProcess(), (PVOID
)((ULONG_PTR
)hLibModule
& ~1));
481 /* Unload alternate resource module */
482 LdrUnloadAlternateResourceModule(hLibModule
);
485 Status
= STATUS_INVALID_IMAGE_FORMAT
;
490 Status
= LdrUnloadDll((PVOID
)hLibModule
);
493 /* Check what kind of status we got */
494 if (!NT_SUCCESS(Status
))
497 BaseSetLastNTError(Status
);
513 FreeLibraryAndExitThread(HMODULE hLibModule
,
517 if (LDR_IS_DATAFILE(hLibModule
))
519 /* This is a LOAD_LIBRARY_AS_DATAFILE module */
520 if (RtlImageNtHeader((PVOID
)((ULONG_PTR
)hLibModule
& ~1)))
523 NtUnmapViewOfSection(NtCurrentProcess(), (PVOID
)((ULONG_PTR
)hLibModule
& ~1));
525 /* Unload alternate resource module */
526 LdrUnloadAlternateResourceModule(hLibModule
);
532 LdrUnloadDll((PVOID
)hLibModule
);
536 ExitThread(dwExitCode
);
545 GetModuleFileNameA(HINSTANCE hModule
,
549 UNICODE_STRING FilenameW
;
550 ANSI_STRING FilenameA
;
552 DWORD Length
= 0, LengthToCopy
;
554 /* Allocate a unicode buffer */
555 FilenameW
.Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, nSize
* sizeof(WCHAR
));
556 if (!FilenameW
.Buffer
)
558 BaseSetLastNTError(STATUS_NO_MEMORY
);
562 /* Call unicode API */
563 FilenameW
.Length
= (USHORT
)GetModuleFileNameW(hModule
, FilenameW
.Buffer
, nSize
) * sizeof(WCHAR
);
564 FilenameW
.MaximumLength
= FilenameW
.Length
+ sizeof(WCHAR
);
566 if (FilenameW
.Length
)
568 /* Convert to ansi string */
569 Status
= BasepUnicodeStringTo8BitString(&FilenameA
, &FilenameW
, TRUE
);
570 if (!NT_SUCCESS(Status
))
572 /* Set last error, free string and return failure */
573 BaseSetLastNTError(Status
);
574 RtlFreeUnicodeString(&FilenameW
);
578 /* Calculate size to copy */
579 Length
= min(nSize
, FilenameA
.Length
);
581 /* Include terminating zero */
583 LengthToCopy
= Length
+ 1;
585 LengthToCopy
= nSize
;
587 /* Now copy back to the caller amount he asked */
588 RtlMoveMemory(lpFilename
, FilenameA
.Buffer
, LengthToCopy
);
590 /* Free ansi filename */
591 RtlFreeAnsiString(&FilenameA
);
594 /* Free unicode filename */
595 RtlFreeHeap(RtlGetProcessHeap(), 0, FilenameW
.Buffer
);
597 /* Return length copied */
606 GetModuleFileNameW(HINSTANCE hModule
,
610 PLIST_ENTRY ModuleListHead
, Entry
;
611 PLDR_DATA_TABLE_ENTRY Module
;
616 hModule
= BasepMapModuleHandle(hModule
, FALSE
);
618 /* Upscale nSize from chars to bytes */
619 nSize
*= sizeof(WCHAR
);
623 /* We don't use per-thread cur dir now */
624 //PRTL_PERTHREAD_CURDIR PerThreadCurdir = (PRTL_PERTHREAD_CURDIR)teb->NtTib.SubSystemTib;
626 Peb
= NtCurrentPeb ();
628 /* Acquire a loader lock */
629 LdrLockLoaderLock(LDR_LOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS
, NULL
, &Cookie
);
631 /* Traverse the module list */
632 ModuleListHead
= &Peb
->Ldr
->InLoadOrderModuleList
;
633 Entry
= ModuleListHead
->Flink
;
634 while (Entry
!= ModuleListHead
)
636 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
638 /* Check if this is the requested module */
639 if (Module
->DllBase
== (PVOID
)hModule
)
641 /* Calculate size to copy */
642 Length
= min(nSize
, Module
->FullDllName
.MaximumLength
);
645 RtlMoveMemory(lpFilename
, Module
->FullDllName
.Buffer
, Length
);
647 /* Subtract a terminating zero */
648 if (Length
== Module
->FullDllName
.MaximumLength
)
649 Length
-= sizeof(WCHAR
);
651 /* Break out of the loop */
655 /* Advance to the next entry */
656 Entry
= Entry
->Flink
;
659 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
661 BaseSetLastNTError(_SEH2_GetExceptionCode());
665 /* Release the loader lock */
666 LdrUnlockLoaderLock(LDR_LOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS
, Cookie
);
668 return Length
/ sizeof(WCHAR
);
673 GetModuleHandleForUnicodeString(PUNICODE_STRING ModuleName
)
679 /* Try to get a handle with a magic value of 1 for DllPath */
680 Status
= LdrGetDllHandle((LPWSTR
)1, NULL
, ModuleName
, &Module
);
682 /* If that succeeded - we're done */
683 if (NT_SUCCESS(Status
)) return Module
;
685 /* If not, then the path should be computed */
686 DllPath
= BaseComputeProcessDllPath(NULL
, 0);
689 Status
= STATUS_NO_MEMORY
;
695 Status
= LdrGetDllHandle(DllPath
, NULL
, ModuleName
, &Module
);
697 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
699 /* Fail with the SEH error */
700 Status
= _SEH2_GetExceptionCode();
705 /* Free the DllPath */
706 RtlFreeHeap(RtlGetProcessHeap(), 0, DllPath
);
708 /* In case of error set last win32 error and return NULL */
709 if (!NT_SUCCESS(Status
))
711 DPRINT("Failure acquiring DLL module '%wZ' handle, Status 0x%08X\n", ModuleName
, Status
);
712 BaseSetLastNTError(Status
);
717 return (HMODULE
)Module
;
722 BasepGetModuleHandleExW(BOOLEAN NoLock
, DWORD dwPublicFlags
, LPCWSTR lpwModuleName
, HMODULE
*phModule
)
725 NTSTATUS Status
= STATUS_SUCCESS
, Status2
;
726 HANDLE hModule
= NULL
;
727 UNICODE_STRING ModuleNameU
;
729 BOOLEAN Redirected
= FALSE
; // FIXME
731 /* Validate parameters */
732 dwValid
= BasepGetModuleHandleExParameterValidation(dwPublicFlags
, lpwModuleName
, phModule
);
733 ASSERT(dwValid
== BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_CONTINUE
);
735 /* Acquire lock if necessary */
738 Status
= LdrLockLoaderLock(0, NULL
, &Cookie
);
739 if (!NT_SUCCESS(Status
))
742 BaseSetLastNTError(Status
);
743 if (phModule
) *phModule
= NULL
;
744 return NT_SUCCESS(Status
);
748 if (!(dwPublicFlags
& GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
))
750 /* Create a unicode string out of module name */
751 RtlInitUnicodeString(&ModuleNameU
, lpwModuleName
);
753 // FIXME: Do some redirected DLL stuff?
761 hModule
= GetModuleHandleForUnicodeString(&ModuleNameU
);
764 /* Last error is already set, so just return failure by setting status */
765 Status
= STATUS_DLL_NOT_FOUND
;
772 /* Perform Pc to file header to get module instance */
773 hModule
= (HMODULE
)RtlPcToFileHeader((PVOID
)lpwModuleName
,
776 /* Check if it succeeded */
779 /* Set "dll not found" status and quit */
780 Status
= STATUS_DLL_NOT_FOUND
;
785 /* Check if changing reference is not forbidden */
786 if (!(dwPublicFlags
& GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
))
788 /* Add reference to this DLL */
789 Status
= LdrAddRefDll((dwPublicFlags
& GET_MODULE_HANDLE_EX_FLAG_PIN
) ? LDR_ADDREF_DLL_PIN
: 0,
794 /* Set last error in case of failure */
795 if (!NT_SUCCESS(Status
))
796 BaseSetLastNTError(Status
);
798 /* Unlock loader lock if it was acquired */
801 Status2
= LdrUnlockLoaderLock(0, Cookie
);
802 ASSERT(NT_SUCCESS(Status2
));
805 /* Set the module handle to the caller */
806 if (phModule
) *phModule
= hModule
;
808 /* Return TRUE on success and FALSE otherwise */
809 return NT_SUCCESS(Status
);
818 GetModuleHandleA(LPCSTR lpModuleName
)
820 PUNICODE_STRING ModuleNameW
;
821 PTEB pTeb
= NtCurrentTeb();
823 /* Check if we have no name to convert */
825 return ((HMODULE
)pTeb
->ProcessEnvironmentBlock
->ImageBaseAddress
);
827 /* Convert module name to unicode */
828 ModuleNameW
= Basep8BitStringToStaticUnicodeString(lpModuleName
);
830 /* Call W version if conversion was successful */
832 return GetModuleHandleW(ModuleNameW
->Buffer
);
844 GetModuleHandleW(LPCWSTR lpModuleName
)
849 /* If current module is requested - return it right away */
851 return ((HMODULE
)NtCurrentPeb()->ImageBaseAddress
);
853 /* Use common helper routine */
854 Success
= BasepGetModuleHandleExW(TRUE
,
855 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
,
859 /* If it wasn't successful - return NULL */
860 if (!Success
) hModule
= NULL
;
862 /* Return the handle */
872 GetModuleHandleExW(IN DWORD dwFlags
,
873 IN LPCWSTR lpwModuleName OPTIONAL
,
874 OUT HMODULE
* phModule
)
879 /* Validate parameters */
880 dwValid
= BasepGetModuleHandleExParameterValidation(dwFlags
, lpwModuleName
, phModule
);
882 /* If result is invalid parameter - return failure */
883 if (dwValid
== BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_ERROR
) return FALSE
;
885 /* If result is 2, there is no need to do anything - return success. */
886 if (dwValid
== BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_SUCCESS
) return TRUE
;
888 /* Use common helper routine */
889 Ret
= BasepGetModuleHandleExW(FALSE
,
902 GetModuleHandleExA(IN DWORD dwFlags
,
903 IN LPCSTR lpModuleName OPTIONAL
,
904 OUT HMODULE
* phModule
)
906 PUNICODE_STRING lpModuleNameW
;
910 /* Validate parameters */
911 dwValid
= BasepGetModuleHandleExParameterValidation(dwFlags
, (LPCWSTR
)lpModuleName
, phModule
);
913 /* If result is invalid parameter - return failure */
914 if (dwValid
== BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_ERROR
) return FALSE
;
916 /* If result is 2, there is no need to do anything - return success. */
917 if (dwValid
== BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_SUCCESS
) return TRUE
;
919 /* Check if we don't need to convert the name */
920 if (dwFlags
& GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
)
922 /* Call the extended version of the API without conversion */
923 Ret
= BasepGetModuleHandleExW(FALSE
,
925 (LPCWSTR
)lpModuleName
,
930 /* Convert module name to unicode */
931 lpModuleNameW
= Basep8BitStringToStaticUnicodeString(lpModuleName
);
933 /* Return FALSE if conversion failed */
934 if (!lpModuleNameW
) return FALSE
;
936 /* Call the extended version of the API */
937 Ret
= BasepGetModuleHandleExW(FALSE
,
939 lpModuleNameW
->Buffer
,
953 LoadModule(LPCSTR lpModuleName
,
954 LPVOID lpParameterBlock
)
956 STARTUPINFOA StartupInfo
;
957 PROCESS_INFORMATION ProcessInformation
;
958 LOADPARMS32
*LoadParams
;
959 char FileName
[MAX_PATH
];
964 UNICODE_STRING UnicStr
;
965 RTL_PATH_TYPE PathType
;
968 LoadParams
= (LOADPARMS32
*)lpParameterBlock
;
970 /* Check load parameters */
971 if (LoadParams
->dwReserved
|| LoadParams
->wMagicValue
!= 2)
973 /* Fail with invalid param error */
974 BaseSetLastNTError(STATUS_INVALID_PARAMETER
);
979 Length
= SearchPathA(NULL
, lpModuleName
, ".exe", MAX_PATH
, FileName
, NULL
);
981 /* Check if path was found */
982 if (Length
&& Length
< MAX_PATH
)
984 /* Build StartupInfo */
985 RtlZeroMemory(&StartupInfo
, sizeof(StartupInfo
));
987 StartupInfo
.cb
= sizeof(STARTUPINFOA
);
988 StartupInfo
.dwFlags
= STARTF_USESHOWWINDOW
;
989 StartupInfo
.wShowWindow
= LoadParams
->wCmdShow
;
991 /* Allocate command line buffer */
992 CommandLine
= RtlAllocateHeap(RtlGetProcessHeap(),
994 (ULONG
)LoadParams
->lpCmdLine
[0] + Length
+ 2);
996 /* Put module name there, then a space, and then copy provided command line,
997 and null-terminate it */
998 RtlCopyMemory(CommandLine
, FileName
, Length
);
999 CommandLine
[Length
] = ' ';
1000 RtlCopyMemory(&CommandLine
[Length
+ 1], &LoadParams
->lpCmdLine
[1], (ULONG
)LoadParams
->lpCmdLine
[0]);
1001 CommandLine
[Length
+ 1 + (ULONG
)LoadParams
->lpCmdLine
[0]] = 0;
1003 /* Create the process */
1004 ProcessStatus
= CreateProcessA(FileName
,
1010 LoadParams
->lpEnvAddress
,
1013 &ProcessInformation
);
1015 /* Free the command line buffer */
1016 RtlFreeHeap(RtlGetProcessHeap(), 0, CommandLine
);
1020 /* Creating process failed, return right error code */
1021 Error
= GetLastError();
1024 case ERROR_BAD_EXE_FORMAT
:
1025 return ERROR_BAD_FORMAT
;
1027 case ERROR_FILE_NOT_FOUND
:
1028 case ERROR_PATH_NOT_FOUND
:
1032 /* Return 0 otherwise */
1036 /* Wait up to 30 seconds for the process to become idle */
1037 if (UserWaitForInputIdleRoutine
)
1039 UserWaitForInputIdleRoutine(ProcessInformation
.hProcess
, 30000);
1043 NtClose(ProcessInformation
.hThread
);
1044 NtClose(ProcessInformation
.hProcess
);
1046 /* Return magic success value (33) */
1050 /* The path was not found, create an ansi string from
1051 the module name and convert it to unicode */
1052 RtlInitAnsiString(&AnsiStr
, lpModuleName
);
1053 if (!NT_SUCCESS(RtlAnsiStringToUnicodeString(&UnicStr
,&AnsiStr
,TRUE
)))
1054 return ERROR_FILE_NOT_FOUND
;
1056 /* Determine path type */
1057 PathType
= RtlDetermineDosPathNameType_U(UnicStr
.Buffer
);
1059 /* Free the unicode module name */
1060 RtlFreeUnicodeString(&UnicStr
);
1062 /* If it's a relative path, return file not found */
1063 if (PathType
== RtlPathTypeRelative
)
1064 return ERROR_FILE_NOT_FOUND
;
1066 /* If not, try to open it */
1067 Handle
= CreateFile(lpModuleName
,
1069 FILE_SHARE_READ
| FILE_SHARE_WRITE
,
1072 FILE_ATTRIBUTE_NORMAL
,
1075 if (Handle
!= INVALID_HANDLE_VALUE
)
1077 /* Opening file succeeded for some reason, close the handle and return file not found anyway */
1078 CloseHandle(Handle
);
1079 return ERROR_FILE_NOT_FOUND
;
1082 /* Return last error which CreateFile set during an attempt to open it */
1083 return GetLastError();
1089 FARPROC WINAPI
DelayLoadFailureHook(LPCSTR pszDllName
, LPCSTR pszProcName
)
1098 BOOL WINAPI
UTRegister( HMODULE hModule
, LPSTR lpsz16BITDLL
,
1099 LPSTR lpszInitName
, LPSTR lpszProcName
,
1100 FARPROC
*ppfn32Thunk
, FARPROC pfnUT32CallBack
,
1110 VOID WINAPI
UTUnRegister( HMODULE hModule
)
1120 BaseQueryModuleData(IN LPSTR ModuleName
,
1126 DPRINT1("BaseQueryModuleData called: %s %s %p %p %p\n",
1140 BaseProcessInitPostImport(VOID
)
1142 /* Check if this is a terminal server */
1143 DPRINT1("Post-init called\n");
1144 if (SharedUserData
->SuiteMask
& VER_SUITE_TERMINAL
)
1146 /* Initialize TS pointers */
1147 return BasepInitializeTermsrvFpns();
1150 /* FIXME: Initialize TS pointers */
1151 return STATUS_SUCCESS
;