2005-01-06 13:58:04 +00:00
|
|
|
/* $Id$
|
1999-10-31 22:41:15 +00:00
|
|
|
*
|
|
|
|
* COPYRIGHT: See COPYING in the top level directory
|
|
|
|
* PROJECT : ReactOS user mode libraries
|
|
|
|
* MODULE : kernel32.dll
|
|
|
|
* FILE : reactos/lib/kernel32/misc/ldr.c
|
2006-12-03 21:06:03 +00:00
|
|
|
* AUTHOR : Ariadne
|
1999-10-31 22:41:15 +00:00
|
|
|
*/
|
2000-04-14 01:50:38 +00:00
|
|
|
|
2003-01-15 21:24:36 +00:00
|
|
|
#include <k32.h>
|
2000-04-14 01:50:38 +00:00
|
|
|
|
|
|
|
#define NDEBUG
|
2007-09-02 19:42:22 +00:00
|
|
|
#include <debug.h>
|
2000-04-14 01:50:38 +00:00
|
|
|
|
2004-06-13 20:04:56 +00:00
|
|
|
typedef struct tagLOADPARMS32 {
|
|
|
|
LPSTR lpEnvAddress;
|
|
|
|
LPSTR lpCmdLine;
|
|
|
|
LPSTR lpCmdShow;
|
|
|
|
DWORD dwReserved;
|
|
|
|
} LOADPARMS32;
|
1999-10-17 18:18:44 +00:00
|
|
|
|
2005-09-26 20:08:11 +00:00
|
|
|
extern BOOLEAN InWindows;
|
2009-07-17 17:15:17 +00:00
|
|
|
extern WaitForInputIdleType lpfnGlobalRegisterWaitForInputIdle;
|
2005-09-26 20:08:11 +00:00
|
|
|
|
2000-04-14 01:50:38 +00:00
|
|
|
/* FUNCTIONS ****************************************************************/
|
1999-10-12 19:45:01 +00:00
|
|
|
|
2005-03-27 11:00:41 +00:00
|
|
|
/**
|
|
|
|
* @name GetDllLoadPath
|
|
|
|
*
|
|
|
|
* Internal function to compute the load path to use for a given dll.
|
|
|
|
*
|
|
|
|
* @remarks Returned pointer must be freed by caller.
|
|
|
|
*/
|
|
|
|
|
2009-07-17 13:24:07 +00:00
|
|
|
LPWSTR
|
2005-03-27 11:00:41 +00:00
|
|
|
GetDllLoadPath(LPCWSTR lpModule)
|
|
|
|
{
|
2007-10-10 23:14:20 +00:00
|
|
|
ULONG Pos = 0, Length = 0;
|
|
|
|
PWCHAR EnvironmentBufferW = NULL;
|
|
|
|
LPCWSTR lpModuleEnd = NULL;
|
|
|
|
UNICODE_STRING ModuleName;
|
2008-09-13 11:35:01 +00:00
|
|
|
DWORD LastError = GetLastError(); /* GetEnvironmentVariable changes LastError */
|
2005-03-27 11:00:41 +00:00
|
|
|
|
2009-04-22 12:20:26 +00:00
|
|
|
if ((lpModule != NULL) && (wcslen(lpModule) > 2) && (lpModule[1] == ':'))
|
2005-03-27 11:00:41 +00:00
|
|
|
{
|
|
|
|
lpModuleEnd = lpModule + wcslen(lpModule);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-10 23:14:20 +00:00
|
|
|
ModuleName = NtCurrentPeb()->ProcessParameters->ImagePathName;
|
|
|
|
lpModule = ModuleName.Buffer;
|
|
|
|
lpModuleEnd = lpModule + (ModuleName.Length / sizeof(WCHAR));
|
2005-03-27 11:00:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (lpModule != NULL)
|
|
|
|
{
|
2007-10-10 23:14:20 +00:00
|
|
|
while (lpModuleEnd > lpModule && *lpModuleEnd != L'/' &&
|
|
|
|
*lpModuleEnd != L'\\' && *lpModuleEnd != L':')
|
|
|
|
{
|
2005-03-27 11:00:41 +00:00
|
|
|
--lpModuleEnd;
|
2007-10-10 23:14:20 +00:00
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
Length = (lpModuleEnd - lpModule) + 1;
|
2005-03-27 11:00:41 +00:00
|
|
|
}
|
|
|
|
|
2005-03-27 13:03:30 +00:00
|
|
|
Length += GetCurrentDirectoryW(0, NULL);
|
2009-10-10 09:16:34 +00:00
|
|
|
Length += GetDllDirectoryW(0, NULL);
|
2005-03-27 13:03:30 +00:00
|
|
|
Length += GetSystemDirectoryW(NULL, 0);
|
|
|
|
Length += GetWindowsDirectoryW(NULL, 0);
|
|
|
|
Length += GetEnvironmentVariableW(L"PATH", NULL, 0);
|
2005-03-27 11:00:41 +00:00
|
|
|
|
|
|
|
EnvironmentBufferW = RtlAllocateHeap(RtlGetProcessHeap(), 0,
|
2007-10-10 23:14:20 +00:00
|
|
|
Length * sizeof(WCHAR));
|
2005-03-27 11:00:41 +00:00
|
|
|
if (EnvironmentBufferW == NULL)
|
2007-10-10 23:14:20 +00:00
|
|
|
{
|
2005-03-27 11:00:41 +00:00
|
|
|
return NULL;
|
2007-10-10 23:14:20 +00:00
|
|
|
}
|
2005-03-27 11:00:41 +00:00
|
|
|
|
|
|
|
if (lpModule)
|
|
|
|
{
|
|
|
|
RtlCopyMemory(EnvironmentBufferW, lpModule,
|
|
|
|
(lpModuleEnd - lpModule) * sizeof(WCHAR));
|
|
|
|
Pos += lpModuleEnd - lpModule;
|
|
|
|
EnvironmentBufferW[Pos++] = L';';
|
|
|
|
}
|
2007-10-10 23:14:20 +00:00
|
|
|
|
2005-03-27 11:00:41 +00:00
|
|
|
Pos += GetCurrentDirectoryW(Length, EnvironmentBufferW + Pos);
|
|
|
|
EnvironmentBufferW[Pos++] = L';';
|
2009-10-10 09:16:34 +00:00
|
|
|
Pos += GetDllDirectoryW(Length - Pos, EnvironmentBufferW + Pos);
|
|
|
|
EnvironmentBufferW[Pos++] = L';';
|
2005-03-27 11:00:41 +00:00
|
|
|
Pos += GetSystemDirectoryW(EnvironmentBufferW + Pos, Length - Pos);
|
|
|
|
EnvironmentBufferW[Pos++] = L';';
|
|
|
|
Pos += GetWindowsDirectoryW(EnvironmentBufferW + Pos, Length - Pos);
|
|
|
|
EnvironmentBufferW[Pos++] = L';';
|
|
|
|
Pos += GetEnvironmentVariableW(L"PATH", EnvironmentBufferW + Pos, Length - Pos);
|
|
|
|
|
2008-09-13 11:35:01 +00:00
|
|
|
SetLastError(LastError);
|
2005-03-27 11:00:41 +00:00
|
|
|
return EnvironmentBufferW;
|
|
|
|
}
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2004-01-23 17:18:16 +00:00
|
|
|
BOOL
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2011-02-19 17:00:08 +00:00
|
|
|
DisableThreadLibraryCalls(
|
|
|
|
IN HMODULE hLibModule)
|
2000-09-01 17:09:50 +00:00
|
|
|
{
|
2011-02-19 17:00:08 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
Status = LdrDisableThreadCalloutsForDll((PVOID)hLibModule);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
BaseSetLastNTError(Status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
2000-09-01 17:09:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
1999-10-31 22:41:15 +00:00
|
|
|
HINSTANCE
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2000-04-14 01:50:38 +00:00
|
|
|
LoadLibraryA (
|
|
|
|
LPCSTR lpLibFileName
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return LoadLibraryExA (lpLibFileName, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2000-04-14 01:50:38 +00:00
|
|
|
HINSTANCE
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2011-02-19 17:00:08 +00:00
|
|
|
LoadLibraryExA(
|
|
|
|
LPCSTR lpLibFileName,
|
|
|
|
HANDLE hFile,
|
|
|
|
DWORD dwFlags)
|
2000-04-14 01:50:38 +00:00
|
|
|
{
|
2011-02-19 17:00:08 +00:00
|
|
|
PUNICODE_STRING FileNameW;
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2011-02-19 17:00:08 +00:00
|
|
|
if (!(FileNameW = Basep8BitStringToStaticUnicodeString(lpLibFileName)))
|
|
|
|
return NULL;
|
2000-04-14 01:50:38 +00:00
|
|
|
|
2011-02-19 17:00:08 +00:00
|
|
|
return LoadLibraryExW(FileNameW->Buffer, hFile, dwFlags);
|
2000-04-14 01:50:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2000-04-14 01:50:38 +00:00
|
|
|
HINSTANCE
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2000-04-14 01:50:38 +00:00
|
|
|
LoadLibraryW (
|
|
|
|
LPCWSTR lpLibFileName
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return LoadLibraryExW (lpLibFileName, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-07-17 15:53:15 +00:00
|
|
|
static
|
|
|
|
NTSTATUS
|
|
|
|
LoadLibraryAsDatafile(PWSTR path, LPCWSTR name, HMODULE* hmod)
|
|
|
|
{
|
|
|
|
static const WCHAR dotDLL[] = {'.','d','l','l',0};
|
|
|
|
|
|
|
|
WCHAR filenameW[MAX_PATH];
|
|
|
|
HANDLE hFile = INVALID_HANDLE_VALUE;
|
|
|
|
HANDLE mapping;
|
|
|
|
HMODULE module;
|
|
|
|
|
|
|
|
*hmod = 0;
|
|
|
|
|
|
|
|
if (!SearchPathW( path, name, dotDLL, sizeof(filenameW) / sizeof(filenameW[0]),
|
|
|
|
filenameW, NULL ))
|
|
|
|
{
|
|
|
|
return NtCurrentTeb()->LastStatusValue;
|
|
|
|
}
|
|
|
|
|
|
|
|
hFile = CreateFileW( filenameW, GENERIC_READ, FILE_SHARE_READ,
|
|
|
|
NULL, OPEN_EXISTING, 0, 0 );
|
|
|
|
|
|
|
|
if (hFile == INVALID_HANDLE_VALUE) return NtCurrentTeb()->LastStatusValue;
|
|
|
|
|
|
|
|
mapping = CreateFileMappingW( hFile, NULL, PAGE_READONLY, 0, 0, NULL );
|
|
|
|
CloseHandle( hFile );
|
|
|
|
if (!mapping) return NtCurrentTeb()->LastStatusValue;
|
|
|
|
|
|
|
|
module = MapViewOfFile( mapping, FILE_MAP_READ, 0, 0, 0 );
|
|
|
|
CloseHandle( mapping );
|
|
|
|
if (!module) return NtCurrentTeb()->LastStatusValue;
|
|
|
|
|
|
|
|
/* make sure it's a valid PE file */
|
|
|
|
if (!RtlImageNtHeader(module))
|
|
|
|
{
|
|
|
|
UnmapViewOfFile( module );
|
|
|
|
return STATUS_INVALID_IMAGE_FORMAT;
|
|
|
|
}
|
|
|
|
*hmod = (HMODULE)((char *)module + 1); /* set low bit of handle to indicate datafile module */
|
|
|
|
return STATUS_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2000-04-14 01:50:38 +00:00
|
|
|
HINSTANCE
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2000-04-14 01:50:38 +00:00
|
|
|
LoadLibraryExW (
|
|
|
|
LPCWSTR lpLibFileName,
|
|
|
|
HANDLE hFile,
|
|
|
|
DWORD dwFlags
|
|
|
|
)
|
1999-10-12 19:45:01 +00:00
|
|
|
{
|
2000-09-01 17:09:50 +00:00
|
|
|
UNICODE_STRING DllName;
|
1999-10-12 19:45:01 +00:00
|
|
|
HINSTANCE hInst;
|
1999-11-17 21:30:00 +00:00
|
|
|
NTSTATUS Status;
|
2005-03-27 11:00:41 +00:00
|
|
|
PWSTR SearchPath;
|
2005-09-26 20:08:11 +00:00
|
|
|
ULONG DllCharacteristics;
|
2008-09-13 11:35:01 +00:00
|
|
|
BOOL FreeString = FALSE;
|
2000-04-14 01:50:38 +00:00
|
|
|
|
2003-02-12 00:39:31 +00:00
|
|
|
(void)hFile;
|
|
|
|
|
1999-10-12 19:45:01 +00:00
|
|
|
if ( lpLibFileName == NULL )
|
|
|
|
return NULL;
|
2000-04-14 01:50:38 +00:00
|
|
|
|
2005-09-26 20:08:11 +00:00
|
|
|
/* Check for any flags LdrLoadDll might be interested in */
|
|
|
|
if (dwFlags & DONT_RESOLVE_DLL_REFERENCES)
|
|
|
|
{
|
|
|
|
/* Tell LDR to treat it as an EXE */
|
|
|
|
DllCharacteristics = IMAGE_FILE_EXECUTABLE_IMAGE;
|
|
|
|
}
|
|
|
|
|
2005-05-09 01:46:57 +00:00
|
|
|
dwFlags &=
|
2003-10-30 15:52:29 +00:00
|
|
|
DONT_RESOLVE_DLL_REFERENCES |
|
|
|
|
LOAD_LIBRARY_AS_DATAFILE |
|
|
|
|
LOAD_WITH_ALTERED_SEARCH_PATH;
|
|
|
|
|
2005-03-27 11:00:41 +00:00
|
|
|
SearchPath = GetDllLoadPath(
|
|
|
|
dwFlags & LOAD_WITH_ALTERED_SEARCH_PATH ? lpLibFileName : NULL);
|
|
|
|
|
2005-03-27 13:03:30 +00:00
|
|
|
RtlInitUnicodeString(&DllName, (LPWSTR)lpLibFileName);
|
2009-07-17 15:53:15 +00:00
|
|
|
|
2008-09-13 11:35:01 +00:00
|
|
|
if (DllName.Buffer[DllName.Length/sizeof(WCHAR) - 1] == L' ')
|
|
|
|
{
|
|
|
|
RtlCreateUnicodeString(&DllName, (LPWSTR)lpLibFileName);
|
|
|
|
while (DllName.Length > sizeof(WCHAR) &&
|
|
|
|
DllName.Buffer[DllName.Length/sizeof(WCHAR) - 1] == L' ')
|
|
|
|
{
|
|
|
|
DllName.Length -= sizeof(WCHAR);
|
|
|
|
}
|
|
|
|
DllName.Buffer[DllName.Length/sizeof(WCHAR)] = UNICODE_NULL;
|
|
|
|
FreeString = TRUE;
|
|
|
|
}
|
2009-07-17 15:53:15 +00:00
|
|
|
|
|
|
|
if (dwFlags & LOAD_LIBRARY_AS_DATAFILE)
|
|
|
|
{
|
|
|
|
Status = LdrGetDllHandle(SearchPath, NULL, &DllName, (PVOID*)&hInst);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
/* The method in load_library_as_datafile allows searching for the
|
|
|
|
* 'native' libraries only
|
|
|
|
*/
|
|
|
|
Status = LoadLibraryAsDatafile(SearchPath, DllName.Buffer, &hInst);
|
2009-07-23 20:48:43 +00:00
|
|
|
goto done;
|
2009-07-17 15:53:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* HACK!!! FIXME */
|
2005-09-26 20:08:11 +00:00
|
|
|
if (InWindows)
|
|
|
|
{
|
|
|
|
/* Call the API Properly */
|
|
|
|
Status = LdrLoadDll(SearchPath,
|
2006-05-10 17:47:44 +00:00
|
|
|
&DllCharacteristics,
|
2005-09-26 20:08:11 +00:00
|
|
|
&DllName,
|
|
|
|
(PVOID*)&hInst);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Call the ROS API. NOTE: Don't fix this, I have a patch to merge later. */
|
2006-05-10 17:47:44 +00:00
|
|
|
Status = LdrLoadDll(SearchPath, &dwFlags, &DllName, (PVOID*)&hInst);
|
2005-09-26 20:08:11 +00:00
|
|
|
}
|
2009-07-17 15:53:15 +00:00
|
|
|
|
2009-07-23 20:48:43 +00:00
|
|
|
done:
|
2005-03-27 13:03:30 +00:00
|
|
|
RtlFreeHeap(RtlGetProcessHeap(), 0, SearchPath);
|
2008-09-13 11:35:01 +00:00
|
|
|
if (FreeString)
|
|
|
|
RtlFreeUnicodeString(&DllName);
|
1999-11-17 21:30:00 +00:00
|
|
|
if ( !NT_SUCCESS(Status))
|
1999-10-12 19:45:01 +00:00
|
|
|
{
|
2000-07-01 17:07:02 +00:00
|
|
|
SetLastErrorByStatus (Status);
|
1999-10-12 19:45:01 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
1999-10-12 19:45:01 +00:00
|
|
|
return hInst;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
1999-10-31 22:41:15 +00:00
|
|
|
FARPROC
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
1999-10-31 22:41:15 +00:00
|
|
|
GetProcAddress( HMODULE hModule, LPCSTR lpProcName )
|
1999-10-12 19:45:01 +00:00
|
|
|
{
|
2000-08-05 18:01:58 +00:00
|
|
|
ANSI_STRING ProcedureName;
|
|
|
|
FARPROC fnExp = NULL;
|
2008-09-13 07:37:05 +00:00
|
|
|
NTSTATUS Status;
|
2000-04-14 01:50:38 +00:00
|
|
|
|
2011-03-21 14:16:01 +00:00
|
|
|
if (!hModule)
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_PROC_NOT_FOUND);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2000-08-05 18:01:58 +00:00
|
|
|
if (HIWORD(lpProcName) != 0)
|
|
|
|
{
|
|
|
|
RtlInitAnsiString (&ProcedureName,
|
|
|
|
(LPSTR)lpProcName);
|
2008-09-13 07:37:05 +00:00
|
|
|
Status = LdrGetProcedureAddress ((PVOID)hModule,
|
2000-08-05 18:01:58 +00:00
|
|
|
&ProcedureName,
|
|
|
|
0,
|
|
|
|
(PVOID*)&fnExp);
|
|
|
|
}
|
1999-10-12 19:45:01 +00:00
|
|
|
else
|
2000-08-05 18:01:58 +00:00
|
|
|
{
|
2008-09-13 07:37:05 +00:00
|
|
|
Status = LdrGetProcedureAddress ((PVOID)hModule,
|
2000-08-05 18:01:58 +00:00
|
|
|
NULL,
|
|
|
|
(ULONG)lpProcName,
|
|
|
|
(PVOID*)&fnExp);
|
|
|
|
}
|
1999-10-12 19:45:01 +00:00
|
|
|
|
2008-09-13 07:37:05 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
2009-07-17 17:53:37 +00:00
|
|
|
SetLastErrorByStatus(Status);
|
2008-09-13 07:37:05 +00:00
|
|
|
fnExp = NULL;
|
|
|
|
}
|
|
|
|
|
1999-10-12 19:45:01 +00:00
|
|
|
return fnExp;
|
|
|
|
}
|
|
|
|
|
1999-10-31 22:41:15 +00:00
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2009-08-04 16:43:37 +00:00
|
|
|
BOOL WINAPI FreeLibrary(HINSTANCE hLibModule)
|
1999-10-12 19:45:01 +00:00
|
|
|
{
|
2009-07-17 17:15:17 +00:00
|
|
|
NTSTATUS Status;
|
|
|
|
|
2009-08-04 16:43:37 +00:00
|
|
|
if (!hLibModule)
|
2009-07-17 17:15:17 +00:00
|
|
|
{
|
2009-08-04 16:43:37 +00:00
|
|
|
SetLastError(ERROR_INVALID_HANDLE);
|
|
|
|
return FALSE;
|
2009-07-17 17:15:17 +00:00
|
|
|
}
|
2009-08-04 16:43:37 +00:00
|
|
|
|
|
|
|
if ((ULONG_PTR)hLibModule & 1)
|
2009-07-17 17:15:17 +00:00
|
|
|
{
|
2009-08-04 16:43:37 +00:00
|
|
|
/* this is a LOAD_LIBRARY_AS_DATAFILE module */
|
|
|
|
char *ptr = (char *)hLibModule - 1;
|
2011-03-21 14:16:01 +00:00
|
|
|
return UnmapViewOfFile(ptr);
|
2009-07-17 17:15:17 +00:00
|
|
|
}
|
|
|
|
|
2009-08-04 16:43:37 +00:00
|
|
|
Status = LdrUnloadDll(hLibModule);
|
2009-07-17 17:15:17 +00:00
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
SetLastErrorByStatus(Status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
1999-10-12 19:45:01 +00:00
|
|
|
}
|
|
|
|
|
2000-04-14 01:50:38 +00:00
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
1999-10-17 18:18:44 +00:00
|
|
|
VOID
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2000-04-14 01:50:38 +00:00
|
|
|
FreeLibraryAndExitThread (
|
|
|
|
HMODULE hLibModule,
|
|
|
|
DWORD dwExitCode
|
|
|
|
)
|
1999-10-17 18:18:44 +00:00
|
|
|
{
|
2009-07-17 17:53:37 +00:00
|
|
|
FreeLibrary(hLibModule);
|
|
|
|
ExitThread(dwExitCode);
|
1999-10-17 18:18:44 +00:00
|
|
|
}
|
1999-10-12 19:45:01 +00:00
|
|
|
|
1999-10-31 22:41:15 +00:00
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2000-09-01 17:09:50 +00:00
|
|
|
DWORD
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2000-09-01 17:09:50 +00:00
|
|
|
GetModuleFileNameA (
|
|
|
|
HINSTANCE hModule,
|
|
|
|
LPSTR lpFilename,
|
|
|
|
DWORD nSize
|
|
|
|
)
|
|
|
|
{
|
|
|
|
ANSI_STRING FileName;
|
|
|
|
PLIST_ENTRY ModuleListHead;
|
|
|
|
PLIST_ENTRY Entry;
|
2005-06-21 04:10:45 +00:00
|
|
|
PLDR_DATA_TABLE_ENTRY Module;
|
2000-09-01 17:09:50 +00:00
|
|
|
PPEB Peb;
|
|
|
|
ULONG Length = 0;
|
|
|
|
|
|
|
|
Peb = NtCurrentPeb ();
|
|
|
|
RtlEnterCriticalSection (Peb->LoaderLock);
|
|
|
|
|
2001-02-10 22:29:35 +00:00
|
|
|
if (hModule == NULL)
|
|
|
|
hModule = Peb->ImageBaseAddress;
|
|
|
|
|
2000-09-01 17:09:50 +00:00
|
|
|
ModuleListHead = &Peb->Ldr->InLoadOrderModuleList;
|
|
|
|
Entry = ModuleListHead->Flink;
|
|
|
|
|
|
|
|
while (Entry != ModuleListHead)
|
|
|
|
{
|
2006-05-10 17:47:44 +00:00
|
|
|
Module = CONTAINING_RECORD(Entry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
|
2005-06-21 04:10:45 +00:00
|
|
|
if (Module->DllBase == (PVOID)hModule)
|
2000-09-01 17:09:50 +00:00
|
|
|
{
|
2008-09-13 07:37:05 +00:00
|
|
|
Length = min(nSize, Module->FullDllName.Length / sizeof(WCHAR));
|
|
|
|
FileName.Length = 0;
|
|
|
|
FileName.MaximumLength = (USHORT)Length * sizeof(WCHAR);
|
|
|
|
FileName.Buffer = lpFilename;
|
|
|
|
|
|
|
|
/* convert unicode string to ansi (or oem) */
|
|
|
|
if (bIsFileApiAnsi)
|
|
|
|
RtlUnicodeStringToAnsiString (&FileName,
|
|
|
|
&Module->FullDllName,
|
|
|
|
FALSE);
|
|
|
|
else
|
|
|
|
RtlUnicodeStringToOemString (&FileName,
|
|
|
|
&Module->FullDllName,
|
|
|
|
FALSE);
|
|
|
|
|
2011-02-26 16:50:20 +00:00
|
|
|
if (Length < nSize)
|
2008-09-13 07:37:05 +00:00
|
|
|
lpFilename[Length] = '\0';
|
2011-02-26 16:50:20 +00:00
|
|
|
else
|
|
|
|
SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL);
|
2000-09-01 17:09:50 +00:00
|
|
|
|
|
|
|
RtlLeaveCriticalSection (Peb->LoaderLock);
|
|
|
|
return Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry = Entry->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetLastErrorByStatus (STATUS_DLL_NOT_FOUND);
|
|
|
|
RtlLeaveCriticalSection (Peb->LoaderLock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2000-09-01 17:09:50 +00:00
|
|
|
DWORD
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2000-09-01 17:09:50 +00:00
|
|
|
GetModuleFileNameW (
|
|
|
|
HINSTANCE hModule,
|
|
|
|
LPWSTR lpFilename,
|
|
|
|
DWORD nSize
|
|
|
|
)
|
|
|
|
{
|
|
|
|
UNICODE_STRING FileName;
|
|
|
|
PLIST_ENTRY ModuleListHead;
|
|
|
|
PLIST_ENTRY Entry;
|
2005-06-21 04:10:45 +00:00
|
|
|
PLDR_DATA_TABLE_ENTRY Module;
|
2000-09-01 17:09:50 +00:00
|
|
|
PPEB Peb;
|
|
|
|
ULONG Length = 0;
|
|
|
|
|
|
|
|
Peb = NtCurrentPeb ();
|
|
|
|
RtlEnterCriticalSection (Peb->LoaderLock);
|
|
|
|
|
2001-02-10 22:29:35 +00:00
|
|
|
if (hModule == NULL)
|
|
|
|
hModule = Peb->ImageBaseAddress;
|
|
|
|
|
2000-09-01 17:09:50 +00:00
|
|
|
ModuleListHead = &Peb->Ldr->InLoadOrderModuleList;
|
|
|
|
Entry = ModuleListHead->Flink;
|
|
|
|
while (Entry != ModuleListHead)
|
|
|
|
{
|
2006-05-10 17:47:44 +00:00
|
|
|
Module = CONTAINING_RECORD(Entry, LDR_DATA_TABLE_ENTRY, InLoadOrderLinks);
|
2000-09-01 17:09:50 +00:00
|
|
|
|
2005-06-21 04:10:45 +00:00
|
|
|
if (Module->DllBase == (PVOID)hModule)
|
2000-09-01 17:09:50 +00:00
|
|
|
{
|
2008-09-13 07:37:05 +00:00
|
|
|
Length = min(nSize, Module->FullDllName.Length / sizeof(WCHAR));
|
|
|
|
FileName.Length = 0;
|
|
|
|
FileName.MaximumLength = (USHORT) Length * sizeof(WCHAR);
|
|
|
|
FileName.Buffer = lpFilename;
|
|
|
|
|
|
|
|
RtlCopyUnicodeString (&FileName,
|
|
|
|
&Module->FullDllName);
|
2011-02-26 16:50:20 +00:00
|
|
|
if (Length < nSize)
|
2008-09-13 07:37:05 +00:00
|
|
|
lpFilename[Length] = L'\0';
|
2011-02-26 16:50:20 +00:00
|
|
|
else
|
|
|
|
SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL);
|
2000-09-01 17:09:50 +00:00
|
|
|
|
|
|
|
RtlLeaveCriticalSection (Peb->LoaderLock);
|
2008-09-13 07:37:05 +00:00
|
|
|
|
2000-09-01 17:09:50 +00:00
|
|
|
return Length;
|
|
|
|
}
|
|
|
|
|
|
|
|
Entry = Entry->Flink;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetLastErrorByStatus (STATUS_DLL_NOT_FOUND);
|
|
|
|
RtlLeaveCriticalSection (Peb->LoaderLock);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
1999-10-31 22:41:15 +00:00
|
|
|
HMODULE
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
1999-10-31 22:41:15 +00:00
|
|
|
GetModuleHandleA ( LPCSTR lpModuleName )
|
1999-10-12 19:45:01 +00:00
|
|
|
{
|
2000-09-01 17:09:50 +00:00
|
|
|
ANSI_STRING ModuleName;
|
2000-08-27 22:37:45 +00:00
|
|
|
NTSTATUS Status;
|
2007-10-10 23:14:20 +00:00
|
|
|
PTEB pTeb = NtCurrentTeb();
|
2000-08-27 22:37:45 +00:00
|
|
|
|
2001-01-24 04:41:58 +00:00
|
|
|
if (lpModuleName == NULL)
|
2007-10-10 23:14:20 +00:00
|
|
|
{
|
|
|
|
return ((HMODULE)pTeb->ProcessEnvironmentBlock->ImageBaseAddress);
|
|
|
|
}
|
1999-11-17 21:30:00 +00:00
|
|
|
|
2007-10-10 23:14:20 +00:00
|
|
|
RtlInitAnsiString(&ModuleName, lpModuleName);
|
2000-08-27 22:37:45 +00:00
|
|
|
|
2007-10-10 23:14:20 +00:00
|
|
|
Status = RtlAnsiStringToUnicodeString(&pTeb->StaticUnicodeString,
|
|
|
|
&ModuleName,
|
|
|
|
FALSE);
|
2000-08-27 22:37:45 +00:00
|
|
|
|
2007-10-10 23:14:20 +00:00
|
|
|
if (NT_SUCCESS(Status))
|
2000-08-27 22:37:45 +00:00
|
|
|
{
|
2007-10-10 23:14:20 +00:00
|
|
|
return GetModuleHandleW(pTeb->StaticUnicodeString.Buffer);
|
2000-08-27 22:37:45 +00:00
|
|
|
}
|
1999-11-17 21:30:00 +00:00
|
|
|
|
2007-10-10 23:14:20 +00:00
|
|
|
SetLastErrorByStatus(Status);
|
|
|
|
return FALSE;
|
1999-10-31 22:41:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2000-08-27 22:37:45 +00:00
|
|
|
HMODULE
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2000-08-27 22:37:45 +00:00
|
|
|
GetModuleHandleW (LPCWSTR lpModuleName)
|
|
|
|
{
|
|
|
|
UNICODE_STRING ModuleName;
|
|
|
|
PVOID BaseAddress;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
if (lpModuleName == NULL)
|
|
|
|
return ((HMODULE)NtCurrentPeb()->ImageBaseAddress);
|
|
|
|
|
|
|
|
RtlInitUnicodeString (&ModuleName,
|
|
|
|
(LPWSTR)lpModuleName);
|
|
|
|
|
|
|
|
Status = LdrGetDllHandle (0,
|
|
|
|
0,
|
|
|
|
&ModuleName,
|
|
|
|
&BaseAddress);
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
SetLastErrorByStatus (Status);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ((HMODULE)BaseAddress);
|
|
|
|
}
|
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
|
2006-01-12 04:31:37 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOL
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2006-01-12 04:31:37 +00:00
|
|
|
GetModuleHandleExW(IN DWORD dwFlags,
|
|
|
|
IN LPCWSTR lpModuleName OPTIONAL,
|
|
|
|
OUT HMODULE* phModule)
|
|
|
|
{
|
|
|
|
HMODULE hModule;
|
|
|
|
NTSTATUS Status;
|
|
|
|
BOOL Ret = FALSE;
|
|
|
|
|
|
|
|
if (phModule == NULL ||
|
|
|
|
((dwFlags & (GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT)) ==
|
|
|
|
(GET_MODULE_HANDLE_EX_FLAG_PIN | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT)))
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpModuleName == NULL)
|
|
|
|
{
|
|
|
|
hModule = NtCurrentPeb()->ImageBaseAddress;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (dwFlags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)
|
|
|
|
{
|
|
|
|
hModule = (HMODULE)RtlPcToFileHeader((PVOID)lpModuleName,
|
|
|
|
(PVOID*)&hModule);
|
|
|
|
if (hModule == NULL)
|
|
|
|
{
|
|
|
|
SetLastErrorByStatus(STATUS_DLL_NOT_FOUND);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
hModule = GetModuleHandleW(lpModuleName);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hModule != NULL)
|
|
|
|
{
|
|
|
|
if (!(dwFlags & GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT))
|
|
|
|
{
|
|
|
|
Status = LdrAddRefDll((dwFlags & GET_MODULE_HANDLE_EX_FLAG_PIN) ? LDR_PIN_MODULE : 0,
|
|
|
|
hModule);
|
|
|
|
|
|
|
|
if (NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
Ret = TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
SetLastErrorByStatus(Status);
|
|
|
|
hModule = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
Ret = TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
*phModule = hModule;
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
BOOL
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2006-01-12 04:31:37 +00:00
|
|
|
GetModuleHandleExA(IN DWORD dwFlags,
|
|
|
|
IN LPCSTR lpModuleName OPTIONAL,
|
|
|
|
OUT HMODULE* phModule)
|
|
|
|
{
|
|
|
|
ANSI_STRING ModuleName;
|
|
|
|
LPCWSTR lpModuleNameW;
|
|
|
|
NTSTATUS Status;
|
|
|
|
BOOL Ret;
|
|
|
|
|
2007-10-10 23:14:20 +00:00
|
|
|
PTEB pTeb = NtCurrentTeb();
|
|
|
|
|
2006-01-12 04:31:37 +00:00
|
|
|
if (dwFlags & GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS)
|
|
|
|
{
|
|
|
|
lpModuleNameW = (LPCWSTR)lpModuleName;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2007-10-10 23:14:20 +00:00
|
|
|
RtlInitAnsiString(&ModuleName, lpModuleName);
|
|
|
|
|
|
|
|
Status = RtlAnsiStringToUnicodeString(&pTeb->StaticUnicodeString,
|
|
|
|
&ModuleName,
|
|
|
|
FALSE);
|
2006-01-12 04:31:37 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
SetLastErrorByStatus(Status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2007-10-10 23:14:20 +00:00
|
|
|
lpModuleNameW = pTeb->StaticUnicodeString.Buffer;
|
2006-01-12 04:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Ret = GetModuleHandleExW(dwFlags,
|
|
|
|
lpModuleNameW,
|
|
|
|
phModule);
|
|
|
|
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
DWORD
|
2008-11-30 11:42:05 +00:00
|
|
|
WINAPI
|
2004-05-03 14:34:44 +00:00
|
|
|
LoadModule (
|
|
|
|
LPCSTR lpModuleName,
|
|
|
|
LPVOID lpParameterBlock
|
|
|
|
)
|
|
|
|
{
|
|
|
|
STARTUPINFOA StartupInfo;
|
|
|
|
PROCESS_INFORMATION ProcessInformation;
|
|
|
|
LOADPARMS32 *LoadParams;
|
|
|
|
char FileName[MAX_PATH];
|
|
|
|
char *CommandLine, *t;
|
|
|
|
BYTE Length;
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
LoadParams = (LOADPARMS32*)lpParameterBlock;
|
|
|
|
if(!lpModuleName || !LoadParams || (((WORD*)LoadParams->lpCmdShow)[0] != 2))
|
|
|
|
{
|
|
|
|
/* windows doesn't check parameters, we do */
|
|
|
|
SetLastError(ERROR_INVALID_PARAMETER);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
if(!SearchPathA(NULL, lpModuleName, ".exe", MAX_PATH, FileName, NULL) &&
|
|
|
|
!SearchPathA(NULL, lpModuleName, NULL, MAX_PATH, FileName, NULL))
|
|
|
|
{
|
|
|
|
return ERROR_FILE_NOT_FOUND;
|
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
Length = (BYTE)LoadParams->lpCmdLine[0];
|
2009-07-17 17:15:17 +00:00
|
|
|
if(!(CommandLine = RtlAllocateHeap(RtlGetProcessHeap(), HEAP_ZERO_MEMORY,
|
2004-05-03 14:34:44 +00:00
|
|
|
strlen(lpModuleName) + Length + 2)))
|
|
|
|
{
|
|
|
|
SetLastError(ERROR_NOT_ENOUGH_MEMORY);
|
|
|
|
return 0;
|
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
/* Create command line string */
|
|
|
|
strcpy(CommandLine, lpModuleName);
|
|
|
|
t = CommandLine + strlen(CommandLine);
|
|
|
|
*(t++) = ' ';
|
|
|
|
memcpy(t, LoadParams->lpCmdLine + 1, Length);
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
/* Build StartupInfo */
|
|
|
|
RtlZeroMemory(&StartupInfo, sizeof(STARTUPINFOA));
|
|
|
|
StartupInfo.cb = sizeof(STARTUPINFOA);
|
|
|
|
StartupInfo.dwFlags = STARTF_USESHOWWINDOW;
|
|
|
|
StartupInfo.wShowWindow = ((WORD*)LoadParams->lpCmdShow)[1];
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
if(!CreateProcessA(FileName, CommandLine, NULL, NULL, FALSE, 0, LoadParams->lpEnvAddress,
|
|
|
|
NULL, &StartupInfo, &ProcessInformation))
|
|
|
|
{
|
|
|
|
DWORD Error;
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2009-07-17 17:15:17 +00:00
|
|
|
RtlFreeHeap(RtlGetProcessHeap(), 0, CommandLine);
|
2004-05-03 14:34:44 +00:00
|
|
|
/* return the right value */
|
|
|
|
Error = GetLastError();
|
|
|
|
switch(Error)
|
|
|
|
{
|
|
|
|
case ERROR_BAD_EXE_FORMAT:
|
|
|
|
{
|
|
|
|
return ERROR_BAD_FORMAT;
|
|
|
|
}
|
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
|
|
|
{
|
|
|
|
return Error;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2009-07-17 17:15:17 +00:00
|
|
|
RtlFreeHeap(RtlGetProcessHeap(), 0, CommandLine);
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
/* Wait up to 15 seconds for the process to become idle */
|
2009-07-17 17:15:17 +00:00
|
|
|
if (NULL != lpfnGlobalRegisterWaitForInputIdle)
|
|
|
|
{
|
|
|
|
lpfnGlobalRegisterWaitForInputIdle(ProcessInformation.hProcess, 15000);
|
|
|
|
}
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2009-07-17 17:15:17 +00:00
|
|
|
NtClose(ProcessInformation.hThread);
|
|
|
|
NtClose(ProcessInformation.hProcess);
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
return 33;
|
|
|
|
}
|
|
|
|
|
1999-10-31 22:41:15 +00:00
|
|
|
/* EOF */
|