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'a 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
);
110 LoadLibraryA(LPCSTR lpLibFileName
)
116 /* Treat twain_32.dll in a special way (what a surprise...) */
117 if (lpLibFileName
&& !_strcmpi(lpLibFileName
, "twain_32.dll"))
119 /* Allocate space for the buffer */
120 PathBuffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, MAX_PATH
);
123 /* Get windows dir in this buffer */
124 Len
= GetWindowsDirectoryA(PathBuffer
, MAX_PATH
- 13); /* 13 is sizeof of '\\twain_32.dll' */
125 if (Len
&& Len
< (MAX_PATH
- 13))
127 /* We successfully got windows directory. Concatenate twain_32.dll to it */
128 strncat(PathBuffer
, "\\twain_32.dll", 13);
130 /* And recursively call ourselves with a new string */
131 Result
= LoadLibraryA(PathBuffer
);
133 /* If it was successful - free memory and return result */
136 RtlFreeHeap(RtlGetProcessHeap(), 0, PathBuffer
);
141 /* Free allocated buffer */
142 RtlFreeHeap(RtlGetProcessHeap(), 0, PathBuffer
);
146 /* Call the Ex version of the API */
147 return LoadLibraryExA(lpLibFileName
, 0, 0);
155 LoadLibraryExA(LPCSTR lpLibFileName
,
159 PUNICODE_STRING FileNameW
;
161 /* Convert file name to unicode */
162 if (!(FileNameW
= Basep8BitStringToStaticUnicodeString(lpLibFileName
)))
165 /* And call W version of the API */
166 return LoadLibraryExW(FileNameW
->Buffer
, hFile
, dwFlags
);
174 LoadLibraryW(LPCWSTR lpLibFileName
)
176 /* Call Ex version of the API */
177 return LoadLibraryExW (lpLibFileName
, 0, 0);
183 BasepLoadLibraryAsDatafile(PWSTR Path
, LPCWSTR Name
, HMODULE
*hModule
)
185 WCHAR FilenameW
[MAX_PATH
];
186 HANDLE hFile
= INVALID_HANDLE_VALUE
;
189 PVOID lpBaseAddress
= NULL
;
191 //PUNICODE_STRING OriginalName;
192 //UNICODE_STRING dotDLL = RTL_CONSTANT_STRING(L".DLL");
194 /* Zero out handle value */
197 DPRINT("BasepLoadLibraryAsDatafile(%S %S %p)\n", Path
, Name
, hModule
);
199 /*Status = RtlDosApplyFileIsolationRedirection_Ustr(TRUE,
209 /* Try to search for it */
210 if (!SearchPathW(Path
,
213 sizeof(FilenameW
) / sizeof(FilenameW
[0]),
217 /* Return last status value directly */
218 return NtCurrentTeb()->LastStatusValue
;
221 /* Open this file we found */
222 hFile
= CreateFileW(FilenameW
,
224 FILE_SHARE_READ
| FILE_SHARE_DELETE
,
230 /* If opening failed - return last status value */
231 if (hFile
== INVALID_HANDLE_VALUE
) return NtCurrentTeb()->LastStatusValue
;
233 /* Create file mapping */
234 hMapping
= CreateFileMappingW(hFile
, NULL
, PAGE_READONLY
, 0, 0, NULL
);
236 /* Close the file handle */
239 /* If creating file mapping failed - return last status value */
240 if (!hMapping
) return NtCurrentTeb()->LastStatusValue
;
242 /* Map view of section */
243 Status
= NtMapViewOfSection(hMapping
,
254 /* Close handle to the section */
255 CloseHandle(hMapping
);
257 /* If mapping view of section failed - return last status value */
258 if (!NT_SUCCESS(Status
)) return NtCurrentTeb()->LastStatusValue
;
260 /* Make sure it's a valid PE file */
261 if (!RtlImageNtHeader(lpBaseAddress
))
263 /* Unmap the view and return failure status */
264 UnmapViewOfFile(lpBaseAddress
);
265 return STATUS_INVALID_IMAGE_FORMAT
;
268 /* Set low bit of handle to indicate datafile module */
269 *hModule
= (HMODULE
)((ULONG_PTR
)lpBaseAddress
| 1);
271 /* Load alternate resource module */
272 //LdrLoadAlternateResourceModule(*hModule, FilenameW);
274 return STATUS_SUCCESS
;
282 LoadLibraryExW(LPCWSTR lpLibFileName
,
286 UNICODE_STRING DllName
;
290 ULONG DllCharacteristics
= 0;
291 BOOL FreeString
= FALSE
;
293 /* Check for any flags LdrLoadDll might be interested in */
294 if (dwFlags
& DONT_RESOLVE_DLL_REFERENCES
)
296 /* Tell LDR to treat it as an EXE */
297 DllCharacteristics
= IMAGE_FILE_EXECUTABLE_IMAGE
;
300 /* Build up a unicode dll name from null-terminated string */
301 RtlInitUnicodeString(&DllName
, (LPWSTR
)lpLibFileName
);
303 /* Lazy-initialize BasepExeLdrEntry */
304 if (!BasepExeLdrEntry
)
305 LdrEnumerateLoadedModules(0, BasepLocateExeLdrEntry
, NtCurrentPeb()->ImageBaseAddress
);
307 /* Check if that module is our exe*/
308 if (BasepExeLdrEntry
&& !(dwFlags
& LOAD_LIBRARY_AS_DATAFILE
) &&
309 DllName
.Length
== BasepExeLdrEntry
->FullDllName
.Length
)
311 /* Lengths match and it's not a datafile, so perform name comparison */
312 if (RtlEqualUnicodeString(&DllName
, &BasepExeLdrEntry
->FullDllName
, TRUE
))
315 return BasepExeLdrEntry
->DllBase
;
319 /* Check for trailing spaces and remove them if necessary */
320 if (DllName
.Buffer
[DllName
.Length
/sizeof(WCHAR
) - 1] == L
' ')
322 RtlCreateUnicodeString(&DllName
, (LPWSTR
)lpLibFileName
);
323 while (DllName
.Length
> sizeof(WCHAR
) &&
324 DllName
.Buffer
[DllName
.Length
/sizeof(WCHAR
) - 1] == L
' ')
326 DllName
.Length
-= sizeof(WCHAR
);
328 DllName
.Buffer
[DllName
.Length
/sizeof(WCHAR
)] = UNICODE_NULL
;
332 /* Compute the load path */
333 SearchPath
= BaseComputeProcessDllPath((dwFlags
& LOAD_WITH_ALTERED_SEARCH_PATH
) ?
334 DllName
.Buffer
: NULL
,
338 /* Getting DLL path failed, so set last error, free mem and return */
339 BaseSetLastNTError(STATUS_NO_MEMORY
);
340 if (FreeString
) RtlFreeUnicodeString(&DllName
);
346 if (dwFlags
& LOAD_LIBRARY_AS_DATAFILE
)
348 /* If the image is loaded as a datafile, try to get its handle */
349 Status
= LdrGetDllHandleEx(0, SearchPath
, NULL
, &DllName
, (PVOID
*)&hInst
);
350 if (!NT_SUCCESS(Status
))
352 /* It's not loaded yet - so load it up */
353 Status
= BasepLoadLibraryAsDatafile(SearchPath
, DllName
.Buffer
, &hInst
);
355 _SEH2_YIELD(goto done
;)
358 /* Call the API Properly */
359 Status
= LdrLoadDll(SearchPath
,
364 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
366 Status
= _SEH2_GetExceptionCode();
371 /* Free SearchPath buffer */
372 RtlFreeHeap(RtlGetProcessHeap(), 0, SearchPath
);
374 /* Free DllName string if it was dynamically allocated */
375 if (FreeString
) RtlFreeUnicodeString(&DllName
);
377 /* Set last error in failure case */
378 if (!NT_SUCCESS(Status
))
380 BaseSetLastNTError(Status
);
384 /* Return loaded module handle */
394 GetProcAddress(HMODULE hModule
, LPCSTR lpProcName
)
396 ANSI_STRING ProcedureName
, *ProcNamePtr
= NULL
;
397 FARPROC fnExp
= NULL
;
402 if (HIWORD(lpProcName
) != 0)
404 /* Look up by name */
405 RtlInitAnsiString(&ProcedureName
, (LPSTR
)lpProcName
);
406 ProcNamePtr
= &ProcedureName
;
410 /* Look up by ordinal */
411 Ordinal
= (ULONG
)lpProcName
;
414 /* Map provided handle */
415 hMapped
= BasepMapModuleHandle(hModule
, FALSE
);
417 /* Get the proc address */
418 Status
= LdrGetProcedureAddress(hMapped
,
423 if (!NT_SUCCESS(Status
))
425 BaseSetLastNTError(Status
);
429 /* Check for a special case when returned pointer is
430 the same as iamge's base address */
431 if (fnExp
== hMapped
)
433 /* Set correct error code */
434 if (HIWORD(lpProcName
) != 0)
435 BaseSetLastNTError(STATUS_ENTRYPOINT_NOT_FOUND
);
437 BaseSetLastNTError(STATUS_ORDINAL_NOT_FOUND
);
442 /* All good, return procedure pointer */
450 BOOL WINAPI
FreeLibrary(HINSTANCE hLibModule
)
453 PIMAGE_NT_HEADERS NtHeaders
;
455 if (LDR_IS_DATAFILE(hLibModule
))
457 // FIXME: This SEH should go inside RtlImageNtHeader instead
460 /* This is a LOAD_LIBRARY_AS_DATAFILE module, check if it's a valid one */
461 NtHeaders
= RtlImageNtHeader((PVOID
)((ULONG_PTR
)hLibModule
& ~1));
463 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
471 Status
= NtUnmapViewOfSection(NtCurrentProcess(), (PVOID
)((ULONG_PTR
)hLibModule
& ~1));
473 /* Unload alternate resource module */
474 LdrUnloadAlternateResourceModule(hLibModule
);
477 Status
= STATUS_INVALID_IMAGE_FORMAT
;
482 Status
= LdrUnloadDll((PVOID
)hLibModule
);
485 /* Check what kind of status we got */
486 if (!NT_SUCCESS(Status
))
489 BaseSetLastNTError(Status
);
505 FreeLibraryAndExitThread(HMODULE hLibModule
,
509 if (LDR_IS_DATAFILE(hLibModule
))
511 /* This is a LOAD_LIBRARY_AS_DATAFILE module */
512 if (RtlImageNtHeader((PVOID
)((ULONG_PTR
)hLibModule
& ~1)))
515 NtUnmapViewOfSection(NtCurrentProcess(), (PVOID
)((ULONG_PTR
)hLibModule
& ~1));
517 /* Unload alternate resource module */
518 LdrUnloadAlternateResourceModule(hLibModule
);
524 LdrUnloadDll((PVOID
)hLibModule
);
528 ExitThread(dwExitCode
);
537 GetModuleFileNameA(HINSTANCE hModule
,
541 UNICODE_STRING FilenameW
;
542 ANSI_STRING FilenameA
;
544 DWORD Length
= 0, LengthToCopy
;
546 /* Allocate a unicode buffer */
547 FilenameW
.Buffer
= RtlAllocateHeap(RtlGetProcessHeap(), 0, nSize
* sizeof(WCHAR
));
548 if (!FilenameW
.Buffer
)
550 BaseSetLastNTError(STATUS_NO_MEMORY
);
554 /* Call unicode API */
555 FilenameW
.Length
= (USHORT
)GetModuleFileNameW(hModule
, FilenameW
.Buffer
, nSize
) * sizeof(WCHAR
);
556 FilenameW
.MaximumLength
= FilenameW
.Length
+ sizeof(WCHAR
);
558 if (FilenameW
.Length
)
560 /* Convert to ansi string */
561 Status
= BasepUnicodeStringTo8BitString(&FilenameA
, &FilenameW
, TRUE
);
562 if (!NT_SUCCESS(Status
))
564 /* Set last error, free string and retun failure */
565 BaseSetLastNTError(Status
);
566 RtlFreeUnicodeString(&FilenameW
);
570 /* Calculate size to copy */
571 Length
= min(nSize
, FilenameA
.Length
);
573 /* Include terminating zero */
575 LengthToCopy
= Length
+ 1;
577 LengthToCopy
= nSize
;
579 /* Now copy back to the caller amount he asked */
580 RtlMoveMemory(lpFilename
, FilenameA
.Buffer
, LengthToCopy
);
582 /* Free ansi filename */
583 RtlFreeAnsiString(&FilenameA
);
586 /* Free unicode filename */
587 RtlFreeHeap(RtlGetProcessHeap(), 0, FilenameW
.Buffer
);
589 /* Return length copied */
598 GetModuleFileNameW(HINSTANCE hModule
,
602 PLIST_ENTRY ModuleListHead
, Entry
;
603 PLDR_DATA_TABLE_ENTRY Module
;
608 hModule
= BasepMapModuleHandle(hModule
, FALSE
);
610 /* Upscale nSize from chars to bytes */
611 nSize
*= sizeof(WCHAR
);
615 /* We don't use per-thread cur dir now */
616 //PRTL_PERTHREAD_CURDIR PerThreadCurdir = (PRTL_PERTHREAD_CURDIR)teb->NtTib.SubSystemTib;
618 Peb
= NtCurrentPeb ();
620 /* Acquire a loader lock */
621 LdrLockLoaderLock(LDR_LOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS
, NULL
, &Cookie
);
623 /* Traverse the module list */
624 ModuleListHead
= &Peb
->Ldr
->InLoadOrderModuleList
;
625 Entry
= ModuleListHead
->Flink
;
626 while (Entry
!= ModuleListHead
)
628 Module
= CONTAINING_RECORD(Entry
, LDR_DATA_TABLE_ENTRY
, InLoadOrderLinks
);
630 /* Check if this is the requested module */
631 if (Module
->DllBase
== (PVOID
)hModule
)
633 /* Calculate size to copy */
634 Length
= min(nSize
, Module
->FullDllName
.MaximumLength
);
637 RtlMoveMemory(lpFilename
, Module
->FullDllName
.Buffer
, Length
);
639 /* Subtract a terminating zero */
640 if (Length
== Module
->FullDllName
.MaximumLength
)
641 Length
-= sizeof(WCHAR
);
643 /* Break out of the loop */
647 /* Advance to the next entry */
648 Entry
= Entry
->Flink
;
651 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
653 BaseSetLastNTError(_SEH2_GetExceptionCode());
657 /* Release the loader lock */
658 LdrUnlockLoaderLock(LDR_LOCK_LOADER_LOCK_FLAG_RAISE_ON_ERRORS
, Cookie
);
660 return Length
/ sizeof(WCHAR
);
665 GetModuleHandleForUnicodeString(PUNICODE_STRING ModuleName
)
671 /* Try to get a handle with a magic value of 1 for DllPath */
672 Status
= LdrGetDllHandle((LPWSTR
)1, NULL
, ModuleName
, &Module
);
674 /* If that succeeded - we're done */
675 if (NT_SUCCESS(Status
)) return Module
;
677 /* If not, then the path should be computed */
678 DllPath
= BaseComputeProcessDllPath(NULL
, 0);
681 Status
= STATUS_NO_MEMORY
;
687 Status
= LdrGetDllHandle(DllPath
, NULL
, ModuleName
, &Module
);
689 _SEH2_EXCEPT(EXCEPTION_EXECUTE_HANDLER
)
691 /* Fail with the SEH error */
692 Status
= _SEH2_GetExceptionCode();
697 /* Free the DllPath */
698 RtlFreeHeap(RtlGetProcessHeap(), 0, DllPath
);
700 /* In case of error set last win32 error and return NULL */
701 if (!NT_SUCCESS(Status
))
703 DPRINT("Failure acquiring DLL module '%wZ' handle, Status 0x%08X\n", ModuleName
, Status
);
704 BaseSetLastNTError(Status
);
709 return (HMODULE
)Module
;
714 BasepGetModuleHandleExW(BOOLEAN NoLock
, DWORD dwPublicFlags
, LPCWSTR lpwModuleName
, HMODULE
*phModule
)
717 NTSTATUS Status
= STATUS_SUCCESS
, Status2
;
719 UNICODE_STRING ModuleNameU
;
721 BOOLEAN Redirected
= FALSE
; // FIXME
723 /* Validate parameters */
724 dwValid
= BasepGetModuleHandleExParameterValidation(dwPublicFlags
, lpwModuleName
, phModule
);
725 ASSERT(dwValid
== BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_CONTINUE
);
727 /* Acquire lock if necessary */
730 Status
= LdrLockLoaderLock(0, NULL
, &Cookie
);
731 if (!NT_SUCCESS(Status
))
734 BaseSetLastNTError(Status
);
735 if (phModule
) *phModule
= 0;
736 return NT_SUCCESS(Status
);
740 if (!(dwPublicFlags
& GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
))
742 /* Create a unicode string out of module name */
743 RtlInitUnicodeString(&ModuleNameU
, lpwModuleName
);
745 // FIXME: Do some redirected DLL stuff?
753 hModule
= GetModuleHandleForUnicodeString(&ModuleNameU
);
756 /* Last error is already set, so just return failure by setting status */
757 Status
= STATUS_DLL_NOT_FOUND
;
764 /* Perform Pc to file header to get module instance */
765 hModule
= (HMODULE
)RtlPcToFileHeader((PVOID
)lpwModuleName
,
768 /* Check if it succeeded */
771 /* Set "dll not found" status and quit */
772 Status
= STATUS_DLL_NOT_FOUND
;
777 /* Check if changing reference is not forbidden */
778 if (!(dwPublicFlags
& GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
))
780 /* Add reference to this DLL */
781 Status
= LdrAddRefDll((dwPublicFlags
& GET_MODULE_HANDLE_EX_FLAG_PIN
) ? LDR_ADDREF_DLL_PIN
: 0,
785 /* Set last error in case of failure */
786 if (!NT_SUCCESS(Status
))
787 BaseSetLastNTError(Status
);
790 /* Unlock loader lock if it was acquired */
793 Status2
= LdrUnlockLoaderLock(0, Cookie
);
794 ASSERT(NT_SUCCESS(Status2
));
797 /* Set the module handle to the caller */
798 if (phModule
) *phModule
= hModule
;
800 /* Return TRUE on success and FALSE otherwise */
801 return NT_SUCCESS(Status
);
809 GetModuleHandleA(LPCSTR lpModuleName
)
811 PUNICODE_STRING ModuleNameW
;
812 PTEB pTeb
= NtCurrentTeb();
814 /* Check if we have no name to convert */
816 return ((HMODULE
)pTeb
->ProcessEnvironmentBlock
->ImageBaseAddress
);
818 /* Convert module name to unicode */
819 ModuleNameW
= Basep8BitStringToStaticUnicodeString(lpModuleName
);
821 /* Call W version if conversion was successful */
823 return GetModuleHandleW(ModuleNameW
->Buffer
);
835 GetModuleHandleW(LPCWSTR lpModuleName
)
840 /* If current module is requested - return it right away */
842 return ((HMODULE
)NtCurrentPeb()->ImageBaseAddress
);
844 /* Use common helper routine */
845 Status
= BasepGetModuleHandleExW(TRUE
,
846 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT
,
850 /* If it wasn't successful - return 0 */
851 if (!NT_SUCCESS(Status
)) hModule
= 0;
853 /* Return the handle */
863 GetModuleHandleExW(IN DWORD dwFlags
,
864 IN LPCWSTR lpwModuleName OPTIONAL
,
865 OUT HMODULE
* phModule
)
871 /* Validate parameters */
872 dwValid
= BasepGetModuleHandleExParameterValidation(dwFlags
, lpwModuleName
, phModule
);
874 /* If result is invalid parameter - return failure */
875 if (dwValid
== BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_ERROR
) return FALSE
;
877 /* If result is 2, there is no need to do anything - return success. */
878 if (dwValid
== BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_SUCCESS
) return TRUE
;
880 /* Use common helper routine */
881 Status
= BasepGetModuleHandleExW(FALSE
,
886 /* Return TRUE in case of success */
887 if (NT_SUCCESS(Status
)) Ret
= TRUE
;
897 GetModuleHandleExA(IN DWORD dwFlags
,
898 IN LPCSTR lpModuleName OPTIONAL
,
899 OUT HMODULE
* phModule
)
901 PUNICODE_STRING lpModuleNameW
;
906 /* Validate parameters */
907 dwValid
= BasepGetModuleHandleExParameterValidation(dwFlags
, (LPCWSTR
)lpModuleName
, phModule
);
909 /* If result is invalid parameter - return failure */
910 if (dwValid
== BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_ERROR
) return FALSE
;
912 /* If result is 2, there is no need to do anything - return success. */
913 if (dwValid
== BASEP_GET_MODULE_HANDLE_EX_PARAMETER_VALIDATION_SUCCESS
) return TRUE
;
915 /* Check if we don't need to convert the name */
916 if (dwFlags
& GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS
)
918 /* Call the extended version of the API without conversion */
919 Status
= BasepGetModuleHandleExW(FALSE
,
921 (LPCWSTR
)lpModuleName
,
926 /* Convert module name to unicode */
927 lpModuleNameW
= Basep8BitStringToStaticUnicodeString(lpModuleName
);
929 /* Return FALSE if conversion failed */
930 if (!lpModuleNameW
) return FALSE
;
932 /* Call the extended version of the API */
933 Status
= BasepGetModuleHandleExW(FALSE
,
935 lpModuleNameW
->Buffer
,
939 /* If result was successful - return true */
940 if (NT_SUCCESS(Status
))
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
;