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
|
|
|
|
* AUTHOR : Boudewijn Dekker
|
|
|
|
*/
|
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
|
2004-01-23 21:16:04 +00:00
|
|
|
#include "../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
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
LPWSTR STDCALL
|
|
|
|
GetDllLoadPath(LPCWSTR lpModule)
|
|
|
|
{
|
|
|
|
ULONG Pos = 0, Length = 0;
|
|
|
|
PWCHAR EnvironmentBufferW = NULL;
|
|
|
|
LPCWSTR lpModuleEnd = NULL;
|
|
|
|
UNICODE_STRING ModuleName;
|
|
|
|
|
|
|
|
if (lpModule != NULL)
|
|
|
|
{
|
|
|
|
lpModuleEnd = lpModule + wcslen(lpModule);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2005-07-12 01:56:14 +00:00
|
|
|
ModuleName = NtCurrentTeb()->ProcessEnvironmentBlock->ProcessParameters->ImagePathName;
|
2005-03-27 11:00:41 +00:00
|
|
|
lpModule = ModuleName.Buffer;
|
|
|
|
lpModuleEnd = lpModule + (ModuleName.Length / sizeof(WCHAR));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (lpModule != NULL)
|
|
|
|
{
|
|
|
|
while (lpModuleEnd > lpModule && *lpModuleEnd != L'/' &&
|
|
|
|
*lpModuleEnd != L'\\' && *lpModuleEnd != L':')
|
|
|
|
--lpModuleEnd;
|
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);
|
|
|
|
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,
|
|
|
|
Length * sizeof(WCHAR));
|
|
|
|
if (EnvironmentBufferW == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
if (lpModule)
|
|
|
|
{
|
|
|
|
RtlCopyMemory(EnvironmentBufferW, lpModule,
|
|
|
|
(lpModuleEnd - lpModule) * sizeof(WCHAR));
|
|
|
|
Pos += lpModuleEnd - lpModule;
|
|
|
|
EnvironmentBufferW[Pos++] = L';';
|
|
|
|
}
|
|
|
|
Pos += GetCurrentDirectoryW(Length, EnvironmentBufferW + Pos);
|
|
|
|
EnvironmentBufferW[Pos++] = L';';
|
|
|
|
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);
|
|
|
|
EnvironmentBufferW[Pos] = 0;
|
|
|
|
|
|
|
|
return EnvironmentBufferW;
|
|
|
|
}
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2004-01-23 17:18:16 +00:00
|
|
|
BOOL
|
2000-09-01 17:09:50 +00:00
|
|
|
STDCALL
|
|
|
|
DisableThreadLibraryCalls (
|
|
|
|
HMODULE hLibModule
|
|
|
|
)
|
|
|
|
{
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
|
|
|
Status = LdrDisableThreadCalloutsForDll ((PVOID)hLibModule);
|
|
|
|
if (!NT_SUCCESS (Status))
|
|
|
|
{
|
|
|
|
SetLastErrorByStatus (Status);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
1999-10-31 22:41:15 +00:00
|
|
|
HINSTANCE
|
|
|
|
STDCALL
|
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
|
|
|
|
STDCALL
|
|
|
|
LoadLibraryExA (
|
|
|
|
LPCSTR lpLibFileName,
|
|
|
|
HANDLE hFile,
|
|
|
|
DWORD dwFlags
|
|
|
|
)
|
|
|
|
{
|
2005-03-27 11:00:41 +00:00
|
|
|
PWCHAR FileNameW;
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2005-03-27 11:00:41 +00:00
|
|
|
if (!(FileNameW = FilenameA2W(lpLibFileName, FALSE)))
|
|
|
|
return FALSE;
|
2000-04-14 01:50:38 +00:00
|
|
|
|
2005-03-27 11:00:41 +00:00
|
|
|
return LoadLibraryExW(FileNameW, 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
|
|
|
|
STDCALL
|
|
|
|
LoadLibraryW (
|
|
|
|
LPCWSTR lpLibFileName
|
|
|
|
)
|
|
|
|
{
|
|
|
|
return LoadLibraryExW (lpLibFileName, 0, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2003-07-10 18:50:51 +00:00
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
2000-04-14 01:50:38 +00:00
|
|
|
HINSTANCE
|
|
|
|
STDCALL
|
|
|
|
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;
|
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-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);
|
2005-03-27 11:00:41 +00:00
|
|
|
Status = LdrLoadDll(SearchPath, dwFlags, &DllName, (PVOID*)&hInst);
|
2005-03-27 13:03:30 +00:00
|
|
|
RtlFreeHeap(RtlGetProcessHeap(), 0, SearchPath);
|
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
|
|
|
|
STDCALL
|
|
|
|
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;
|
2000-04-14 01:50:38 +00:00
|
|
|
|
2000-08-05 18:01:58 +00:00
|
|
|
if (HIWORD(lpProcName) != 0)
|
|
|
|
{
|
|
|
|
RtlInitAnsiString (&ProcedureName,
|
|
|
|
(LPSTR)lpProcName);
|
|
|
|
LdrGetProcedureAddress ((PVOID)hModule,
|
|
|
|
&ProcedureName,
|
|
|
|
0,
|
|
|
|
(PVOID*)&fnExp);
|
|
|
|
}
|
1999-10-12 19:45:01 +00:00
|
|
|
else
|
2000-08-05 18:01:58 +00:00
|
|
|
{
|
|
|
|
LdrGetProcedureAddress ((PVOID)hModule,
|
|
|
|
NULL,
|
|
|
|
(ULONG)lpProcName,
|
|
|
|
(PVOID*)&fnExp);
|
|
|
|
}
|
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
|
|
|
|
*/
|
2004-01-23 17:18:16 +00:00
|
|
|
BOOL
|
1999-10-31 22:41:15 +00:00
|
|
|
STDCALL
|
|
|
|
FreeLibrary( HMODULE hLibModule )
|
1999-10-12 19:45:01 +00:00
|
|
|
{
|
|
|
|
LdrUnloadDll(hLibModule);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
STDCALL
|
2000-04-14 01:50:38 +00:00
|
|
|
FreeLibraryAndExitThread (
|
|
|
|
HMODULE hLibModule,
|
|
|
|
DWORD dwExitCode
|
|
|
|
)
|
1999-10-17 18:18:44 +00:00
|
|
|
{
|
|
|
|
if ( FreeLibrary(hLibModule) )
|
|
|
|
ExitThread(dwExitCode);
|
2004-06-13 20:04:56 +00:00
|
|
|
for (;;)
|
|
|
|
;
|
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
|
|
|
|
STDCALL
|
|
|
|
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)
|
|
|
|
{
|
2005-06-21 04:10:45 +00:00
|
|
|
Module = CONTAINING_RECORD(Entry, LDR_DATA_TABLE_ENTRY, InLoadOrderModuleList);
|
|
|
|
if (Module->DllBase == (PVOID)hModule)
|
2000-09-01 17:09:50 +00:00
|
|
|
{
|
|
|
|
if (nSize * sizeof(WCHAR) < Module->FullDllName.Length)
|
|
|
|
{
|
|
|
|
SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FileName.Length = 0;
|
|
|
|
FileName.MaximumLength = nSize * 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);
|
|
|
|
Length = Module->FullDllName.Length / sizeof(WCHAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
STDCALL
|
|
|
|
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)
|
|
|
|
{
|
2005-06-21 04:10:45 +00:00
|
|
|
Module = CONTAINING_RECORD(Entry, LDR_DATA_TABLE_ENTRY, InLoadOrderModuleList);
|
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
|
|
|
{
|
|
|
|
if (nSize * sizeof(WCHAR) < Module->FullDllName.Length)
|
|
|
|
{
|
|
|
|
SetLastErrorByStatus (STATUS_BUFFER_TOO_SMALL);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
FileName.Length = 0;
|
|
|
|
FileName.MaximumLength = nSize * sizeof(WCHAR);
|
|
|
|
FileName.Buffer = lpFilename;
|
|
|
|
|
|
|
|
RtlCopyUnicodeString (&FileName,
|
|
|
|
&Module->FullDllName);
|
|
|
|
Length = Module->FullDllName.Length / sizeof(WCHAR);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
1999-10-31 22:41:15 +00:00
|
|
|
HMODULE
|
|
|
|
STDCALL
|
|
|
|
GetModuleHandleA ( LPCSTR lpModuleName )
|
1999-10-12 19:45:01 +00:00
|
|
|
{
|
2000-09-01 17:09:50 +00:00
|
|
|
UNICODE_STRING UnicodeName;
|
|
|
|
ANSI_STRING ModuleName;
|
2000-08-27 22:37:45 +00:00
|
|
|
PVOID BaseAddress;
|
|
|
|
NTSTATUS Status;
|
|
|
|
|
2001-01-24 04:41:58 +00:00
|
|
|
if (lpModuleName == NULL)
|
|
|
|
return ((HMODULE)NtCurrentPeb()->ImageBaseAddress);
|
2000-09-01 17:09:50 +00:00
|
|
|
RtlInitAnsiString (&ModuleName,
|
|
|
|
(LPSTR)lpModuleName);
|
|
|
|
|
|
|
|
/* convert ansi (or oem) string to unicode */
|
|
|
|
if (bIsFileApiAnsi)
|
|
|
|
RtlAnsiStringToUnicodeString (&UnicodeName,
|
|
|
|
&ModuleName,
|
|
|
|
TRUE);
|
|
|
|
else
|
|
|
|
RtlOemStringToUnicodeString (&UnicodeName,
|
|
|
|
&ModuleName,
|
|
|
|
TRUE);
|
1999-11-17 21:30:00 +00:00
|
|
|
|
2000-08-27 22:37:45 +00:00
|
|
|
Status = LdrGetDllHandle (0,
|
|
|
|
0,
|
2000-09-01 17:09:50 +00:00
|
|
|
&UnicodeName,
|
2000-08-27 22:37:45 +00:00
|
|
|
&BaseAddress);
|
|
|
|
|
2000-09-01 17:09:50 +00:00
|
|
|
RtlFreeUnicodeString (&UnicodeName);
|
2000-08-27 22:37:45 +00:00
|
|
|
|
|
|
|
if (!NT_SUCCESS(Status))
|
|
|
|
{
|
|
|
|
SetLastErrorByStatus (Status);
|
|
|
|
return NULL;
|
|
|
|
}
|
1999-11-17 21:30:00 +00:00
|
|
|
|
2000-08-27 22:37:45 +00:00
|
|
|
return ((HMODULE)BaseAddress);
|
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
|
|
|
|
STDCALL
|
|
|
|
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
|
|
|
|
|
|
|
/*
|
|
|
|
* @implemented
|
|
|
|
*/
|
|
|
|
DWORD
|
|
|
|
STDCALL
|
|
|
|
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];
|
|
|
|
if(!(CommandLine = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY,
|
|
|
|
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
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
HeapFree(GetProcessHeap(), 0, CommandLine);
|
|
|
|
/* 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
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
HeapFree(GetProcessHeap(), 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 */
|
|
|
|
WaitForInputIdle(ProcessInformation.hProcess, 15000);
|
2005-05-09 01:46:57 +00:00
|
|
|
|
2004-05-03 14:34:44 +00:00
|
|
|
CloseHandle(ProcessInformation.hThread);
|
|
|
|
CloseHandle(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 */
|