From 08108f008e4ec12993127384ddf3b147008b4dbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?G=C3=A9=20van=20Geldorp?= Date: Wed, 2 Jun 2004 18:26:58 +0000 Subject: [PATCH] Patch by Pablo Borobia Correct search path for DLL loading (including directory from which .exe was loaded). svn path=/trunk/; revision=9589 --- reactos/lib/kernel32/file/dir.c | 904 ++++++++++++----------- reactos/lib/ntdll/ldr/utils.c | 1210 ++++++++++++++++--------------- 2 files changed, 1077 insertions(+), 1037 deletions(-) diff --git a/reactos/lib/kernel32/file/dir.c b/reactos/lib/kernel32/file/dir.c index f8411ee5857..36b63751ad4 100644 --- a/reactos/lib/kernel32/file/dir.c +++ b/reactos/lib/kernel32/file/dir.c @@ -1,4 +1,4 @@ -/* $Id: dir.c,v 1.44 2004/01/23 21:16:03 ekohl Exp $ +/* $Id: dir.c,v 1.45 2004/06/02 18:26:57 gvg Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS system libraries @@ -29,13 +29,13 @@ BOOL STDCALL CreateDirectoryA ( - LPCSTR lpPathName, - LPSECURITY_ATTRIBUTES lpSecurityAttributes - ) + LPCSTR lpPathName, + LPSECURITY_ATTRIBUTES lpSecurityAttributes + ) { - return CreateDirectoryExA (NULL, - lpPathName, - lpSecurityAttributes); + return CreateDirectoryExA (NULL, + lpPathName, + lpSecurityAttributes); } @@ -45,69 +45,69 @@ CreateDirectoryA ( BOOL STDCALL CreateDirectoryExA ( - LPCSTR lpTemplateDirectory, - LPCSTR lpNewDirectory, - LPSECURITY_ATTRIBUTES lpSecurityAttributes) + LPCSTR lpTemplateDirectory, + LPCSTR lpNewDirectory, + LPSECURITY_ATTRIBUTES lpSecurityAttributes) { - UNICODE_STRING TmplDirU; - UNICODE_STRING NewDirU; - ANSI_STRING TmplDir; - ANSI_STRING NewDir; - BOOL Result; + UNICODE_STRING TmplDirU; + UNICODE_STRING NewDirU; + ANSI_STRING TmplDir; + ANSI_STRING NewDir; + BOOL Result; - RtlInitUnicodeString (&TmplDirU, - NULL); + RtlInitUnicodeString (&TmplDirU, + NULL); - RtlInitUnicodeString (&NewDirU, - NULL); + RtlInitUnicodeString (&NewDirU, + NULL); - if (lpTemplateDirectory != NULL) - { - RtlInitAnsiString (&TmplDir, - (LPSTR)lpTemplateDirectory); + if (lpTemplateDirectory != NULL) + { + RtlInitAnsiString (&TmplDir, + (LPSTR)lpTemplateDirectory); - /* convert ansi (or oem) string to unicode */ - if (bIsFileApiAnsi) - RtlAnsiStringToUnicodeString (&TmplDirU, - &TmplDir, - TRUE); - else - RtlOemStringToUnicodeString (&TmplDirU, - &TmplDir, - TRUE); - } + /* convert ansi (or oem) string to unicode */ + if (bIsFileApiAnsi) + RtlAnsiStringToUnicodeString (&TmplDirU, + &TmplDir, + TRUE); + else + RtlOemStringToUnicodeString (&TmplDirU, + &TmplDir, + TRUE); + } - if (lpNewDirectory != NULL) - { - RtlInitAnsiString (&NewDir, - (LPSTR)lpNewDirectory); + if (lpNewDirectory != NULL) + { + RtlInitAnsiString (&NewDir, + (LPSTR)lpNewDirectory); - /* convert ansi (or oem) string to unicode */ - if (bIsFileApiAnsi) - RtlAnsiStringToUnicodeString (&NewDirU, - &NewDir, - TRUE); - else - RtlOemStringToUnicodeString (&NewDirU, - &NewDir, - TRUE); - } + /* convert ansi (or oem) string to unicode */ + if (bIsFileApiAnsi) + RtlAnsiStringToUnicodeString (&NewDirU, + &NewDir, + TRUE); + else + RtlOemStringToUnicodeString (&NewDirU, + &NewDir, + TRUE); + } - Result = CreateDirectoryExW (TmplDirU.Buffer, - NewDirU.Buffer, - lpSecurityAttributes); + Result = CreateDirectoryExW (TmplDirU.Buffer, + NewDirU.Buffer, + lpSecurityAttributes); - if (lpTemplateDirectory != NULL) - RtlFreeHeap (RtlGetProcessHeap (), - 0, - TmplDirU.Buffer); + if (lpTemplateDirectory != NULL) + RtlFreeHeap (RtlGetProcessHeap (), + 0, + TmplDirU.Buffer); - if (lpNewDirectory != NULL) - RtlFreeHeap (RtlGetProcessHeap (), - 0, - NewDirU.Buffer); + if (lpNewDirectory != NULL) + RtlFreeHeap (RtlGetProcessHeap (), + 0, + NewDirU.Buffer); - return Result; + return Result; } @@ -117,13 +117,13 @@ CreateDirectoryExA ( BOOL STDCALL CreateDirectoryW ( - LPCWSTR lpPathName, - LPSECURITY_ATTRIBUTES lpSecurityAttributes - ) + LPCWSTR lpPathName, + LPSECURITY_ATTRIBUTES lpSecurityAttributes + ) { - return CreateDirectoryExW (NULL, - lpPathName, - lpSecurityAttributes); + return CreateDirectoryExW (NULL, + lpPathName, + lpSecurityAttributes); } @@ -133,20 +133,20 @@ CreateDirectoryW ( BOOL STDCALL CreateDirectoryExW ( - LPCWSTR lpTemplateDirectory, - LPCWSTR lpNewDirectory, - LPSECURITY_ATTRIBUTES lpSecurityAttributes - ) + LPCWSTR lpTemplateDirectory, + LPCWSTR lpNewDirectory, + LPSECURITY_ATTRIBUTES lpSecurityAttributes + ) { - OBJECT_ATTRIBUTES ObjectAttributes; - IO_STATUS_BLOCK IoStatusBlock; - UNICODE_STRING NtPathU; - HANDLE DirectoryHandle; - NTSTATUS Status; + OBJECT_ATTRIBUTES ObjectAttributes; + IO_STATUS_BLOCK IoStatusBlock; + UNICODE_STRING NtPathU; + HANDLE DirectoryHandle; + NTSTATUS Status; + + DPRINT ("lpTemplateDirectory %S lpNewDirectory %S lpSecurityAttributes %p\n", + lpTemplateDirectory, lpNewDirectory, lpSecurityAttributes); - DPRINT ("lpTemplateDirectory %S lpNewDirectory %S lpSecurityAttributes %p\n", - lpTemplateDirectory, lpNewDirectory, lpSecurityAttributes); - // Can't create empty directory if(lpNewDirectory == NULL || *lpNewDirectory == 0) { @@ -154,54 +154,54 @@ CreateDirectoryExW ( return FALSE; } - if (lpTemplateDirectory != NULL && *lpTemplateDirectory != 0) - { - // get object attributes from template directory - DPRINT("KERNEL32:FIXME:%s:%d\n",__FILE__,__LINE__); - return FALSE; - } + if (lpTemplateDirectory != NULL && *lpTemplateDirectory != 0) + { + // get object attributes from template directory + DPRINT("KERNEL32:FIXME:%s:%d\n",__FILE__,__LINE__); + return FALSE; + } - if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpNewDirectory, - &NtPathU, - NULL, - NULL)) - return FALSE; + if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpNewDirectory, + &NtPathU, + NULL, + NULL)) + return FALSE; - DPRINT ("NtPathU \'%wZ\'\n", &NtPathU); + DPRINT ("NtPathU \'%wZ\'\n", &NtPathU); - ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES); - ObjectAttributes.RootDirectory = NULL; - ObjectAttributes.ObjectName = &NtPathU; - ObjectAttributes.Attributes = OBJ_CASE_INSENSITIVE | OBJ_INHERIT; - ObjectAttributes.SecurityDescriptor = NULL; - ObjectAttributes.SecurityQualityOfService = NULL; + ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES); + ObjectAttributes.RootDirectory = NULL; + ObjectAttributes.ObjectName = &NtPathU; + ObjectAttributes.Attributes = OBJ_CASE_INSENSITIVE | OBJ_INHERIT; + ObjectAttributes.SecurityDescriptor = NULL; + ObjectAttributes.SecurityQualityOfService = NULL; - Status = NtCreateFile (&DirectoryHandle, - DIRECTORY_ALL_ACCESS, - &ObjectAttributes, - &IoStatusBlock, - NULL, - FILE_ATTRIBUTE_DIRECTORY, - 0, - FILE_CREATE, - FILE_DIRECTORY_FILE, - NULL, - 0); - DPRINT("Status: %lx\n", Status); + Status = NtCreateFile (&DirectoryHandle, + DIRECTORY_ALL_ACCESS, + &ObjectAttributes, + &IoStatusBlock, + NULL, + FILE_ATTRIBUTE_DIRECTORY, + 0, + FILE_CREATE, + FILE_DIRECTORY_FILE, + NULL, + 0); + DPRINT("Status: %lx\n", Status); - RtlFreeHeap (RtlGetProcessHeap (), - 0, - NtPathU.Buffer); + RtlFreeHeap (RtlGetProcessHeap (), + 0, + NtPathU.Buffer); - if (!NT_SUCCESS(Status)) - { - SetLastErrorByStatus(Status); - return FALSE; - } + if (!NT_SUCCESS(Status)) + { + SetLastErrorByStatus(Status); + return FALSE; + } - NtClose (DirectoryHandle); + NtClose (DirectoryHandle); - return TRUE; + return TRUE; } @@ -211,33 +211,33 @@ CreateDirectoryExW ( BOOL STDCALL RemoveDirectoryA ( - LPCSTR lpPathName - ) + LPCSTR lpPathName + ) { - UNICODE_STRING PathNameU; - ANSI_STRING PathName; - BOOL Result; + UNICODE_STRING PathNameU; + ANSI_STRING PathName; + BOOL Result; - RtlInitAnsiString (&PathName, - (LPSTR)lpPathName); + RtlInitAnsiString (&PathName, + (LPSTR)lpPathName); - /* convert ansi (or oem) string to unicode */ - if (bIsFileApiAnsi) - RtlAnsiStringToUnicodeString (&PathNameU, - &PathName, - TRUE); - else - RtlOemStringToUnicodeString (&PathNameU, - &PathName, - TRUE); + /* convert ansi (or oem) string to unicode */ + if (bIsFileApiAnsi) + RtlAnsiStringToUnicodeString (&PathNameU, + &PathName, + TRUE); + else + RtlOemStringToUnicodeString (&PathNameU, + &PathName, + TRUE); - Result = RemoveDirectoryW (PathNameU.Buffer); + Result = RemoveDirectoryW (PathNameU.Buffer); - RtlFreeHeap (RtlGetProcessHeap (), - 0, - PathNameU.Buffer); + RtlFreeHeap (RtlGetProcessHeap (), + 0, + PathNameU.Buffer); - return Result; + return Result; } @@ -247,80 +247,80 @@ RemoveDirectoryA ( BOOL STDCALL RemoveDirectoryW ( - LPCWSTR lpPathName - ) + LPCWSTR lpPathName + ) { - FILE_DISPOSITION_INFORMATION FileDispInfo; - OBJECT_ATTRIBUTES ObjectAttributes; - IO_STATUS_BLOCK IoStatusBlock; - UNICODE_STRING NtPathU; - HANDLE DirectoryHandle; - NTSTATUS Status; + FILE_DISPOSITION_INFORMATION FileDispInfo; + OBJECT_ATTRIBUTES ObjectAttributes; + IO_STATUS_BLOCK IoStatusBlock; + UNICODE_STRING NtPathU; + HANDLE DirectoryHandle; + NTSTATUS Status; - DPRINT("lpPathName %S\n", lpPathName); + DPRINT("lpPathName %S\n", lpPathName); - if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpPathName, - &NtPathU, - NULL, - NULL)) - return FALSE; + if (!RtlDosPathNameToNtPathName_U ((LPWSTR)lpPathName, + &NtPathU, + NULL, + NULL)) + return FALSE; - ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES); - ObjectAttributes.RootDirectory = NULL; - ObjectAttributes.ObjectName = &NtPathU; - ObjectAttributes.Attributes = OBJ_CASE_INSENSITIVE| OBJ_INHERIT; - ObjectAttributes.SecurityDescriptor = NULL; - ObjectAttributes.SecurityQualityOfService = NULL; + ObjectAttributes.Length = sizeof(OBJECT_ATTRIBUTES); + ObjectAttributes.RootDirectory = NULL; + ObjectAttributes.ObjectName = &NtPathU; + ObjectAttributes.Attributes = OBJ_CASE_INSENSITIVE| OBJ_INHERIT; + ObjectAttributes.SecurityDescriptor = NULL; + ObjectAttributes.SecurityQualityOfService = NULL; - DPRINT("NtPathU '%S'\n", NtPathU.Buffer); + DPRINT("NtPathU '%S'\n", NtPathU.Buffer); - Status = NtCreateFile (&DirectoryHandle, - FILE_WRITE_ATTRIBUTES, /* 0x110080 */ - &ObjectAttributes, - &IoStatusBlock, - NULL, - FILE_ATTRIBUTE_DIRECTORY, /* 0x7 */ - 0, - FILE_OPEN, - FILE_DIRECTORY_FILE, /* 0x204021 */ - NULL, - 0); + Status = NtCreateFile (&DirectoryHandle, + FILE_WRITE_ATTRIBUTES, /* 0x110080 */ + &ObjectAttributes, + &IoStatusBlock, + NULL, + FILE_ATTRIBUTE_DIRECTORY, /* 0x7 */ + 0, + FILE_OPEN, + FILE_DIRECTORY_FILE, /* 0x204021 */ + NULL, + 0); - RtlFreeHeap (RtlGetProcessHeap (), - 0, - NtPathU.Buffer); + RtlFreeHeap (RtlGetProcessHeap (), + 0, + NtPathU.Buffer); - if (!NT_SUCCESS(Status)) - { - CHECKPOINT; - SetLastErrorByStatus (Status); - return FALSE; - } + if (!NT_SUCCESS(Status)) + { + CHECKPOINT; + SetLastErrorByStatus (Status); + return FALSE; + } - FileDispInfo.DoDeleteFile = TRUE; + FileDispInfo.DoDeleteFile = TRUE; - Status = NtSetInformationFile (DirectoryHandle, - &IoStatusBlock, - &FileDispInfo, - sizeof(FILE_DISPOSITION_INFORMATION), - FileDispositionInformation); - if (!NT_SUCCESS(Status)) - { - CHECKPOINT; - NtClose(DirectoryHandle); - SetLastErrorByStatus (Status); - return FALSE; - } + Status = NtSetInformationFile (DirectoryHandle, + &IoStatusBlock, + &FileDispInfo, + sizeof(FILE_DISPOSITION_INFORMATION), + FileDispositionInformation); + if (!NT_SUCCESS(Status)) + { + CHECKPOINT; + NtClose(DirectoryHandle); + SetLastErrorByStatus (Status); + return FALSE; + } - Status = NtClose (DirectoryHandle); - if (!NT_SUCCESS(Status)) - { - CHECKPOINT; - SetLastErrorByStatus (Status); - return FALSE; - } + Status = NtClose (DirectoryHandle); + if (!NT_SUCCESS(Status)) + { + CHECKPOINT; + SetLastErrorByStatus (Status); + return FALSE; + } - return TRUE; + return TRUE; } @@ -330,76 +330,76 @@ RemoveDirectoryW ( DWORD STDCALL GetFullPathNameA ( - LPCSTR lpFileName, - DWORD nBufferLength, - LPSTR lpBuffer, - LPSTR *lpFilePart - ) + LPCSTR lpFileName, + DWORD nBufferLength, + LPSTR lpBuffer, + LPSTR *lpFilePart + ) { - UNICODE_STRING nameW; - WCHAR bufferW[MAX_PATH]; - DWORD ret, retW; - LPWSTR FilePart = NULL; + UNICODE_STRING nameW; + WCHAR bufferW[MAX_PATH]; + DWORD ret, retW; + LPWSTR FilePart = NULL; - DPRINT("GetFullPathNameA(lpFileName %s, nBufferLength %d, lpBuffer %p, " - "lpFilePart %p)\n",lpFileName,nBufferLength,lpBuffer,lpFilePart); + DPRINT("GetFullPathNameA(lpFileName %s, nBufferLength %d, lpBuffer %p, " + "lpFilePart %p)\n",lpFileName,nBufferLength,lpBuffer,lpFilePart); - if (!lpFileName) - { - SetLastError(ERROR_INVALID_PARAMETER); - return 0; - } + if (!lpFileName) + { + SetLastError(ERROR_INVALID_PARAMETER); + return 0; + } - if (!RtlCreateUnicodeStringFromAsciiz(&nameW, (LPSTR)lpFileName)) - { - SetLastError(ERROR_NOT_ENOUGH_MEMORY); - return 0; - } + if (!RtlCreateUnicodeStringFromAsciiz(&nameW, (LPSTR)lpFileName)) + { + SetLastError(ERROR_NOT_ENOUGH_MEMORY); + return 0; + } - if (lpFilePart) - { - *lpFilePart = NULL; - } + if (lpFilePart) + { + *lpFilePart = NULL; + } - retW = GetFullPathNameW(nameW.Buffer, MAX_PATH, bufferW, &FilePart); + retW = GetFullPathNameW(nameW.Buffer, MAX_PATH, bufferW, &FilePart); - if (!retW) - { - ret = 0; - } - else if (retW > MAX_PATH) - { - SetLastError(ERROR_FILENAME_EXCED_RANGE); - ret = 0; - } - else - { - ANSI_STRING AnsiBuffer; - UNICODE_STRING UnicodeBuffer; + if (!retW) + { + ret = 0; + } + else if (retW > MAX_PATH) + { + SetLastError(ERROR_FILENAME_EXCED_RANGE); + ret = 0; + } + else + { + ANSI_STRING AnsiBuffer; + UNICODE_STRING UnicodeBuffer; - UnicodeBuffer.Length = wcslen(bufferW) * sizeof(WCHAR); - ret = nameW.Length; - if (nameW.Length <= nBufferLength) - { - UnicodeBuffer.Buffer = bufferW; - AnsiBuffer.MaximumLength = nBufferLength; - AnsiBuffer.Length = 0; - AnsiBuffer.Buffer = lpBuffer; - RtlUnicodeStringToAnsiString(&AnsiBuffer, &UnicodeBuffer, FALSE); + UnicodeBuffer.Length = wcslen(bufferW) * sizeof(WCHAR); + ret = nameW.Length; + if (nameW.Length <= nBufferLength) + { + UnicodeBuffer.Buffer = bufferW; + AnsiBuffer.MaximumLength = nBufferLength; + AnsiBuffer.Length = 0; + AnsiBuffer.Buffer = lpBuffer; + RtlUnicodeStringToAnsiString(&AnsiBuffer, &UnicodeBuffer, FALSE); - if (lpFilePart && FilePart != NULL) - { - *lpFilePart = (FilePart - bufferW) + lpBuffer; - } - } - } + if (lpFilePart && FilePart != NULL) + { + *lpFilePart = (FilePart - bufferW) + lpBuffer; + } + } + } - RtlFreeUnicodeString(&nameW); + RtlFreeUnicodeString(&nameW); - DPRINT("lpBuffer %s lpFilePart %s Length %ld\n", - lpBuffer, (lpFilePart == NULL) ? "NULL" : *lpFilePart, nameW.Length); + DPRINT("lpBuffer %s lpFilePart %s Length %ld\n", + lpBuffer, (lpFilePart == NULL) ? "NULL" : *lpFilePart, nameW.Length); - return ret; + return ret; } @@ -409,26 +409,26 @@ GetFullPathNameA ( DWORD STDCALL GetFullPathNameW ( - LPCWSTR lpFileName, - DWORD nBufferLength, - LPWSTR lpBuffer, - LPWSTR *lpFilePart - ) + LPCWSTR lpFileName, + DWORD nBufferLength, + LPWSTR lpBuffer, + LPWSTR *lpFilePart + ) { - ULONG Length; + ULONG Length; - DPRINT("GetFullPathNameW(lpFileName %S, nBufferLength %d, lpBuffer %p, " - "lpFilePart %p)\n",lpFileName,nBufferLength,lpBuffer,lpFilePart); + DPRINT("GetFullPathNameW(lpFileName %S, nBufferLength %d, lpBuffer %p, " + "lpFilePart %p)\n",lpFileName,nBufferLength,lpBuffer,lpFilePart); - Length = RtlGetFullPathName_U ((LPWSTR)lpFileName, - nBufferLength * sizeof(WCHAR), - lpBuffer, - lpFilePart); + Length = RtlGetFullPathName_U ((LPWSTR)lpFileName, + nBufferLength * sizeof(WCHAR), + lpBuffer, + lpFilePart); - DPRINT("lpBuffer %S lpFilePart %S Length %ld\n", - lpBuffer, (lpFilePart == NULL) ? L"NULL" : *lpFilePart, Length / sizeof(WCHAR)); + DPRINT("lpBuffer %S lpFilePart %S Length %ld\n", + lpBuffer, (lpFilePart == NULL) ? L"NULL" : *lpFilePart, Length / sizeof(WCHAR)); - return (Length / sizeof(WCHAR)); + return (Length / sizeof(WCHAR)); } @@ -438,23 +438,23 @@ GetFullPathNameW ( DWORD STDCALL GetShortPathNameA ( - LPCSTR lpszLongPath, - LPSTR lpszShortPath, - DWORD cchBuffer - ) + LPCSTR lpszLongPath, + LPSTR lpszShortPath, + DWORD cchBuffer + ) { - //1 remove unicode chars and spaces - //2 remove preceding and trailing periods. - //3 remove embedded periods except the last one - - //4 Split the string in two parts before and after the period - // truncate the part before the period to 6 chars and add ~1 - // truncate the part after the period to 3 chars - //3 Put the new name in uppercase - - //4 Increment the ~1 string if the resulting name allready exists + //1 remove unicode chars and spaces + //2 remove preceding and trailing periods. + //3 remove embedded periods except the last one - return 0; + //4 Split the string in two parts before and after the period + // truncate the part before the period to 6 chars and add ~1 + // truncate the part after the period to 3 chars + //3 Put the new name in uppercase + + //4 Increment the ~1 string if the resulting name allready exists + + return 0; } @@ -464,12 +464,12 @@ GetShortPathNameA ( DWORD STDCALL GetShortPathNameW ( - LPCWSTR lpszLongPath, - LPWSTR lpszShortPath, - DWORD cchBuffer - ) + LPCWSTR lpszLongPath, + LPWSTR lpszShortPath, + DWORD cchBuffer + ) { - return 0; + return 0; } @@ -479,111 +479,111 @@ GetShortPathNameW ( DWORD STDCALL SearchPathA ( - LPCSTR lpPath, - LPCSTR lpFileName, - LPCSTR lpExtension, - DWORD nBufferLength, - LPSTR lpBuffer, - LPSTR *lpFilePart - ) + LPCSTR lpPath, + LPCSTR lpFileName, + LPCSTR lpExtension, + DWORD nBufferLength, + LPSTR lpBuffer, + LPSTR *lpFilePart + ) { - UNICODE_STRING PathU; - UNICODE_STRING FileNameU; - UNICODE_STRING ExtensionU; - UNICODE_STRING BufferU; - ANSI_STRING Path; - ANSI_STRING FileName; - ANSI_STRING Extension; - ANSI_STRING Buffer; - PWCHAR FilePartW; - DWORD RetValue; + UNICODE_STRING PathU; + UNICODE_STRING FileNameU; + UNICODE_STRING ExtensionU; + UNICODE_STRING BufferU; + ANSI_STRING Path; + ANSI_STRING FileName; + ANSI_STRING Extension; + ANSI_STRING Buffer; + PWCHAR FilePartW; + DWORD RetValue; - RtlInitAnsiString (&Path, - (LPSTR)lpPath); - RtlInitAnsiString (&FileName, - (LPSTR)lpFileName); - RtlInitAnsiString (&Extension, - (LPSTR)lpExtension); + RtlInitAnsiString (&Path, + (LPSTR)lpPath); + RtlInitAnsiString (&FileName, + (LPSTR)lpFileName); + RtlInitAnsiString (&Extension, + (LPSTR)lpExtension); - /* convert ansi (or oem) strings to unicode */ - if (bIsFileApiAnsi) - { - RtlAnsiStringToUnicodeString (&PathU, - &Path, - TRUE); - RtlAnsiStringToUnicodeString (&FileNameU, - &FileName, - TRUE); - RtlAnsiStringToUnicodeString (&ExtensionU, - &Extension, - TRUE); - } - else - { - RtlOemStringToUnicodeString (&PathU, - &Path, - TRUE); - RtlOemStringToUnicodeString (&FileNameU, - &FileName, - TRUE); - RtlOemStringToUnicodeString (&ExtensionU, - &Extension, - TRUE); - } + /* convert ansi (or oem) strings to unicode */ + if (bIsFileApiAnsi) + { + RtlAnsiStringToUnicodeString (&PathU, + &Path, + TRUE); + RtlAnsiStringToUnicodeString (&FileNameU, + &FileName, + TRUE); + RtlAnsiStringToUnicodeString (&ExtensionU, + &Extension, + TRUE); + } + else + { + RtlOemStringToUnicodeString (&PathU, + &Path, + TRUE); + RtlOemStringToUnicodeString (&FileNameU, + &FileName, + TRUE); + RtlOemStringToUnicodeString (&ExtensionU, + &Extension, + TRUE); + } - BufferU.Length = 0; - BufferU.MaximumLength = nBufferLength * sizeof(WCHAR); - BufferU.Buffer = RtlAllocateHeap (RtlGetProcessHeap (), - 0, - BufferU.MaximumLength); + BufferU.Length = 0; + BufferU.MaximumLength = nBufferLength * sizeof(WCHAR); + BufferU.Buffer = RtlAllocateHeap (RtlGetProcessHeap (), + 0, + BufferU.MaximumLength); - Buffer.Length = 0; - Buffer.MaximumLength = nBufferLength; - Buffer.Buffer = lpBuffer; + Buffer.Length = 0; + Buffer.MaximumLength = nBufferLength; + Buffer.Buffer = lpBuffer; - RetValue = SearchPathW (NULL == lpPath ? NULL : PathU.Buffer, - NULL == lpFileName ? NULL : FileNameU.Buffer, - NULL == lpExtension ? NULL : ExtensionU.Buffer, - nBufferLength, - BufferU.Buffer, - &FilePartW); + RetValue = SearchPathW (NULL == lpPath ? NULL : PathU.Buffer, + NULL == lpFileName ? NULL : FileNameU.Buffer, + NULL == lpExtension ? NULL : ExtensionU.Buffer, + nBufferLength, + BufferU.Buffer, + &FilePartW); - RtlFreeHeap (RtlGetProcessHeap (), - 0, - PathU.Buffer); - RtlFreeHeap (RtlGetProcessHeap (), - 0, - FileNameU.Buffer); - RtlFreeHeap (RtlGetProcessHeap (), - 0, - ExtensionU.Buffer); + RtlFreeHeap (RtlGetProcessHeap (), + 0, + PathU.Buffer); + RtlFreeHeap (RtlGetProcessHeap (), + 0, + FileNameU.Buffer); + RtlFreeHeap (RtlGetProcessHeap (), + 0, + ExtensionU.Buffer); - if (0 != RetValue) - { - BufferU.Length = wcslen(BufferU.Buffer) * sizeof(WCHAR); - /* convert ansi (or oem) string to unicode */ - if (bIsFileApiAnsi) - RtlUnicodeStringToAnsiString (&Buffer, - &BufferU, - FALSE); - else - RtlUnicodeStringToOemString (&Buffer, - &BufferU, - FALSE); - /* nul-terminate ascii string */ - Buffer.Buffer[BufferU.Length / sizeof(WCHAR)] = '\0'; - } + if (0 != RetValue) + { + BufferU.Length = wcslen(BufferU.Buffer) * sizeof(WCHAR); + /* convert ansi (or oem) string to unicode */ + if (bIsFileApiAnsi) + RtlUnicodeStringToAnsiString (&Buffer, + &BufferU, + FALSE); + else + RtlUnicodeStringToOemString (&Buffer, + &BufferU, + FALSE); + /* nul-terminate ascii string */ + Buffer.Buffer[BufferU.Length / sizeof(WCHAR)] = '\0'; + } - RtlFreeHeap (RtlGetProcessHeap (), - 0, - BufferU.Buffer); + RtlFreeHeap (RtlGetProcessHeap (), + 0, + BufferU.Buffer); - if (NULL != lpFilePart) - { - *lpFilePart = strrchr (lpBuffer, '\\') + 1; - } + if (NULL != lpFilePart) + { + *lpFilePart = strrchr (lpBuffer, '\\') + 1; + } - return RetValue; + return RetValue; } @@ -593,13 +593,13 @@ SearchPathA ( DWORD STDCALL SearchPathW ( - LPCWSTR lpPath, - LPCWSTR lpFileName, - LPCWSTR lpExtension, - DWORD nBufferLength, - LPWSTR lpBuffer, - LPWSTR *lpFilePart - ) + LPCWSTR lpPath, + LPCWSTR lpFileName, + LPCWSTR lpExtension, + DWORD nBufferLength, + LPWSTR lpBuffer, + LPWSTR *lpFilePart + ) /* * FUNCTION: Searches for the specified file * ARGUMENTS: @@ -620,7 +620,7 @@ SearchPathW ( * filename extension must be a period (.). The * extension is only added if the specified filename * doesn't end with an extension - * + * * If the filename extension is not required or if the * filename contains an extension, this parameters can be * NULL @@ -628,60 +628,88 @@ SearchPathW ( * lpBuffer = Points to the buffer for the valid path and filename of * file found * lpFilePart = Points to the last component of the valid path and - * filename + * filename * RETURNS: On success, the length, in characters, of the string copied to the * buffer * On failure, zero. */ { - DWORD retCode = 0; - ULONG pos, len; - PWCHAR EnvironmentBufferW = NULL; - WCHAR Buffer; + DWORD retCode = 0; + ULONG pos, len; + PWCHAR EnvironmentBufferW = NULL; + PWCHAR AppPathW = NULL; + WCHAR Buffer; + //WCHAR drv[3]; - DPRINT("SearchPath\n"); - if (lpPath == NULL) - { - len = GetEnvironmentVariableW(L"PATH", &Buffer, 0); - len += 1 + GetCurrentDirectoryW(0, &Buffer); - len += 1 + GetSystemDirectoryW(&Buffer, 0); - len += 1 + GetWindowsDirectoryW(&Buffer, 0); + DPRINT("SearchPath\n"); - EnvironmentBufferW = (PWCHAR) RtlAllocateHeap(GetProcessHeap(), - HEAP_GENERATE_EXCEPTIONS|HEAP_ZERO_MEMORY, - len * sizeof(WCHAR)); - if (EnvironmentBufferW == NULL) - { - SetLastError(ERROR_OUTOFMEMORY); - return 0; - } + if (lpPath == NULL) + { - pos = GetCurrentDirectoryW(len, EnvironmentBufferW); - EnvironmentBufferW[pos++] = L';'; - EnvironmentBufferW[pos] = 0; - pos += GetSystemDirectoryW(&EnvironmentBufferW[pos], len - pos); - EnvironmentBufferW[pos++] = L';'; - EnvironmentBufferW[pos] = 0; - pos += GetWindowsDirectoryW(&EnvironmentBufferW[pos], len - pos); - EnvironmentBufferW[pos++] = L';'; - EnvironmentBufferW[pos] = 0; - pos += GetEnvironmentVariableW(L"PATH", &EnvironmentBufferW[pos], len - pos); - lpPath = EnvironmentBufferW; - } + AppPathW = (PWCHAR) RtlAllocateHeap(GetProcessHeap(), + HEAP_GENERATE_EXCEPTIONS|HEAP_ZERO_MEMORY, + MAX_PATH * sizeof(WCHAR)); - retCode = RtlDosSearchPath_U ((PWCHAR)lpPath, (PWCHAR)lpFileName, (PWCHAR)lpExtension, - nBufferLength * sizeof(WCHAR), lpBuffer, lpFilePart); - if (EnvironmentBufferW != NULL) - { - RtlFreeHeap(GetProcessHeap(), 0, EnvironmentBufferW); - } - if (retCode == 0) - { - SetLastError(ERROR_FILE_NOT_FOUND); - } - return retCode / sizeof(WCHAR); + wcscat (AppPathW, NtCurrentPeb()->ProcessParameters->ImagePathName.Buffer); + + len = wcslen (AppPathW); + + while (len && AppPathW[len - 1] != L'\\') + len--; + + if (len) AppPathW[len-1] = L'\0'; + + len = GetEnvironmentVariableW(L"PATH", &Buffer, 0); + len += 1 + GetCurrentDirectoryW(0, &Buffer); + len += 1 + GetSystemDirectoryW(&Buffer, 0); + len += 1 + GetWindowsDirectoryW(&Buffer, 0); + len += 1 + wcslen(AppPathW) * sizeof(WCHAR); + + EnvironmentBufferW = (PWCHAR) RtlAllocateHeap(GetProcessHeap(), + HEAP_GENERATE_EXCEPTIONS|HEAP_ZERO_MEMORY, + len * sizeof(WCHAR)); + if (EnvironmentBufferW == NULL) + { + SetLastError(ERROR_OUTOFMEMORY); + return 0; + } + + pos = GetCurrentDirectoryW(len, EnvironmentBufferW); + EnvironmentBufferW[pos++] = L';'; + EnvironmentBufferW[pos] = 0; + pos += GetSystemDirectoryW(&EnvironmentBufferW[pos], len - pos); + EnvironmentBufferW[pos++] = L';'; + EnvironmentBufferW[pos] = 0; + pos += GetWindowsDirectoryW(&EnvironmentBufferW[pos], len - pos); + EnvironmentBufferW[pos++] = L';'; + EnvironmentBufferW[pos] = 0; + pos += GetEnvironmentVariableW(L"PATH", &EnvironmentBufferW[pos], len - pos); + EnvironmentBufferW[pos++] = L';'; + EnvironmentBufferW[pos] = 0; + wcscat (EnvironmentBufferW, AppPathW); + + RtlFreeHeap (RtlGetProcessHeap (), + 0, + AppPathW); + + lpPath = EnvironmentBufferW; + + } + + retCode = RtlDosSearchPath_U ((PWCHAR)lpPath, (PWCHAR)lpFileName, (PWCHAR)lpExtension, + nBufferLength * sizeof(WCHAR), lpBuffer, lpFilePart); + + if (EnvironmentBufferW != NULL) + { + RtlFreeHeap(GetProcessHeap(), 0, EnvironmentBufferW); + } + if (retCode == 0) + { + SetLastError(ERROR_FILE_NOT_FOUND); + } + return retCode / sizeof(WCHAR); } /* EOF */ diff --git a/reactos/lib/ntdll/ldr/utils.c b/reactos/lib/ntdll/ldr/utils.c index 02e2365f377..1ef60632967 100644 --- a/reactos/lib/ntdll/ldr/utils.c +++ b/reactos/lib/ntdll/ldr/utils.c @@ -1,4 +1,4 @@ -/* $Id: utils.c,v 1.86 2004/05/13 20:30:26 navaraf Exp $ +/* $Id: utils.c,v 1.87 2004/06/02 18:26:58 gvg Exp $ * * COPYRIGHT: See COPYING in the top level directory * PROJECT: ReactOS kernel @@ -59,10 +59,10 @@ extern PLDR_MODULE ExeModule; static NTSTATUS LdrFindEntryForName(PUNICODE_STRING Name, PLDR_MODULE *Module, BOOL Ref); static PVOID LdrFixupForward(PCHAR ForwardName); static PVOID LdrGetExportByName(PVOID BaseAddress, PUCHAR SymbolName, USHORT Hint); -static NTSTATUS LdrpLoadModule(IN PWSTR SearchPath OPTIONAL, - IN ULONG LoadFlags, - IN PUNICODE_STRING Name, - OUT PLDR_MODULE *Module); +static NTSTATUS LdrpLoadModule(IN PWSTR SearchPath OPTIONAL, + IN ULONG LoadFlags, + IN PUNICODE_STRING Name, + OUT PLDR_MODULE *Module); static NTSTATUS LdrpAttachProcess(VOID); static VOID LdrpDetachProcess(BOOL UnloadAll); @@ -147,12 +147,12 @@ static inline VOID LdrpTlsCallback(PLDR_MODULE Module, ULONG dwReason) { while (*TlsCallback) { - TRACE_LDR("%wZ - Calling tls callback at %x\n", - &Module->BaseDllName, TlsCallback); - TlsCallback(Module->BaseAddress, dwReason, NULL); - TlsCallback++; - } - } + TRACE_LDR("%wZ - Calling tls callback at %x\n", + &Module->BaseDllName, TlsCallback); + TlsCallback(Module->BaseAddress, dwReason, NULL); + TlsCallback++; + } + } } } @@ -181,32 +181,32 @@ LdrpInitializeTlsForThread(VOID) { TlsPointers = RtlAllocateHeap(RtlGetProcessHeap(), 0, - LdrpTlsCount * sizeof(PVOID) + LdrpTlsSize); + LdrpTlsCount * sizeof(PVOID) + LdrpTlsSize); if (TlsPointers == NULL) { - DPRINT1("failed to allocate thread tls data\n"); - return STATUS_NO_MEMORY; - } - + DPRINT1("failed to allocate thread tls data\n"); + return STATUS_NO_MEMORY; + } + TlsData = (PVOID)TlsPointers + LdrpTlsCount * sizeof(PVOID); NtCurrentTeb()->ThreadLocalStoragePointer = TlsPointers; TlsInfo = LdrpTlsArray; for (i = 0; i < LdrpTlsCount; i++, TlsInfo++) { - TRACE_LDR("Initialize tls data for %wZ\n", &TlsInfo->Module->BaseDllName); - TlsPointers[i] = TlsData; - if (TlsInfo->TlsDataSize) - { - memcpy(TlsData, TlsInfo->StartAddressOfRawData, TlsInfo->TlsDataSize); - TlsData += TlsInfo->TlsDataSize; - } - if (TlsInfo->TlsZeroSize) - { - memset(TlsData, 0, TlsInfo->TlsZeroSize); - TlsData += TlsInfo->TlsZeroSize; - } - } + TRACE_LDR("Initialize tls data for %wZ\n", &TlsInfo->Module->BaseDllName); + TlsPointers[i] = TlsData; + if (TlsInfo->TlsDataSize) + { + memcpy(TlsData, TlsInfo->StartAddressOfRawData, TlsInfo->TlsDataSize); + TlsData += TlsInfo->TlsDataSize; + } + if (TlsInfo->TlsZeroSize) + { + memset(TlsData, 0, TlsInfo->TlsZeroSize); + TlsData += TlsInfo->TlsZeroSize; + } + } } DPRINT("LdrpInitializeTlsForThread() done\n"); return STATUS_SUCCESS; @@ -222,57 +222,57 @@ LdrpInitializeTlsForProccess(VOID) PTLS_DATA TlsData; DPRINT("LdrpInitializeTlsForProccess() called for %wZ\n", &ExeModule->BaseDllName); - + if (LdrpTlsCount > 0) { LdrpTlsArray = RtlAllocateHeap(RtlGetProcessHeap(), 0, - LdrpTlsCount * sizeof(TLS_DATA)); + LdrpTlsCount * sizeof(TLS_DATA)); if (LdrpTlsArray == NULL) { - DPRINT1("Failed to allocate global tls data\n"); + DPRINT1("Failed to allocate global tls data\n"); return STATUS_NO_MEMORY; } - + ModuleListHead = &NtCurrentPeb()->Ldr->InLoadOrderModuleList; Entry = ModuleListHead->Flink; while (Entry != ModuleListHead) { Module = CONTAINING_RECORD(Entry, LDR_MODULE, InLoadOrderModuleList); - if (Module->LoadCount == -1 && - Module->TlsIndex >= 0) + if (Module->LoadCount == -1 && + Module->TlsIndex >= 0) { TlsDirectory = (PIMAGE_TLS_DIRECTORY) - RtlImageDirectoryEntryToData(Module->BaseAddress, + RtlImageDirectoryEntryToData(Module->BaseAddress, TRUE, IMAGE_DIRECTORY_ENTRY_TLS, NULL); assert(Module->TlsIndex < LdrpTlsCount); - TlsData = &LdrpTlsArray[Module->TlsIndex]; - TlsData->StartAddressOfRawData = (PVOID)TlsDirectory->StartAddressOfRawData; - TlsData->TlsDataSize = TlsDirectory->EndAddressOfRawData - TlsDirectory->StartAddressOfRawData; - TlsData->TlsZeroSize = TlsDirectory->SizeOfZeroFill; - TlsData->TlsAddressOfCallBacks = *TlsDirectory->AddressOfCallBacks; - TlsData->Module = Module; + TlsData = &LdrpTlsArray[Module->TlsIndex]; + TlsData->StartAddressOfRawData = (PVOID)TlsDirectory->StartAddressOfRawData; + TlsData->TlsDataSize = TlsDirectory->EndAddressOfRawData - TlsDirectory->StartAddressOfRawData; + TlsData->TlsZeroSize = TlsDirectory->SizeOfZeroFill; + TlsData->TlsAddressOfCallBacks = *TlsDirectory->AddressOfCallBacks; + TlsData->Module = Module; #if 0 DbgPrint("TLS directory for %wZ\n", &Module->BaseDllName); - DbgPrint("StartAddressOfRawData: %x\n", TlsDirectory->StartAddressOfRawData); - DbgPrint("EndAddressOfRawData: %x\n", TlsDirectory->EndAddressOfRawData); - DbgPrint("SizeOfRawData: %d\n", TlsDirectory->EndAddressOfRawData - TlsDirectory->StartAddressOfRawData); - DbgPrint("AddressOfIndex: %x\n", TlsDirectory->AddressOfIndex); - DbgPrint("AddressOfCallBacks: %x (%x)\n", TlsDirectory->AddressOfCallBacks, *TlsDirectory->AddressOfCallBacks); - DbgPrint("SizeOfZeroFill: %d\n", TlsDirectory->SizeOfZeroFill); - DbgPrint("Characteristics: %x\n", TlsDirectory->Characteristics); + DbgPrint("StartAddressOfRawData: %x\n", TlsDirectory->StartAddressOfRawData); + DbgPrint("EndAddressOfRawData: %x\n", TlsDirectory->EndAddressOfRawData); + DbgPrint("SizeOfRawData: %d\n", TlsDirectory->EndAddressOfRawData - TlsDirectory->StartAddressOfRawData); + DbgPrint("AddressOfIndex: %x\n", TlsDirectory->AddressOfIndex); + DbgPrint("AddressOfCallBacks: %x (%x)\n", TlsDirectory->AddressOfCallBacks, *TlsDirectory->AddressOfCallBacks); + DbgPrint("SizeOfZeroFill: %d\n", TlsDirectory->SizeOfZeroFill); + DbgPrint("Characteristics: %x\n", TlsDirectory->Characteristics); #endif - /* - * FIXME: - * Is this region allways writable ? - */ + /* + * FIXME: + * Is this region allways writable ? + */ *(PULONG)TlsDirectory->AddressOfIndex = Module->TlsIndex; - CHECKPOINT1; - } - Entry = Entry->Flink; - } + CHECKPOINT1; + } + Entry = Entry->Flink; + } } DPRINT("LdrpInitializeTlsForProccess() done\n"); return STATUS_SUCCESS; @@ -292,15 +292,15 @@ LdrpInitLoader(VOID) /* Get handle to the 'KnownDlls' directory */ RtlInitUnicodeString(&Name, - L"\\KnownDlls"); + L"\\KnownDlls"); InitializeObjectAttributes(&ObjectAttributes, - &Name, - OBJ_CASE_INSENSITIVE, - NULL, - NULL); + &Name, + OBJ_CASE_INSENSITIVE, + NULL, + NULL); Status = NtOpenDirectoryObject(&LdrpKnownDllsDirHandle, - DIRECTORY_QUERY | DIRECTORY_TRAVERSE, - &ObjectAttributes); + DIRECTORY_QUERY | DIRECTORY_TRAVERSE, + &ObjectAttributes); if (!NT_SUCCESS(Status)) { DPRINT("NtOpenDirectoryObject() failed (Status %lx)\n", Status); @@ -312,8 +312,8 @@ LdrpInitLoader(VOID) LinkTarget.Length = 0; LinkTarget.MaximumLength = MAX_PATH * sizeof(WCHAR); LinkTarget.Buffer = RtlAllocateHeap(RtlGetProcessHeap(), - 0, - MAX_PATH * sizeof(WCHAR)); + 0, + MAX_PATH * sizeof(WCHAR)); if (LinkTarget.Buffer == NULL) { NtClose(LdrpKnownDllsDirHandle); @@ -322,15 +322,15 @@ LdrpInitLoader(VOID) } RtlInitUnicodeString(&Name, - L"KnownDllPath"); + L"KnownDllPath"); InitializeObjectAttributes(&ObjectAttributes, - &Name, - OBJ_CASE_INSENSITIVE | OBJ_OPENLINK, - LdrpKnownDllsDirHandle, - NULL); + &Name, + OBJ_CASE_INSENSITIVE | OBJ_OPENLINK, + LdrpKnownDllsDirHandle, + NULL); Status = NtOpenSymbolicLinkObject(&LinkHandle, - SYMBOLIC_LINK_ALL_ACCESS, - &ObjectAttributes); + SYMBOLIC_LINK_ALL_ACCESS, + &ObjectAttributes); if (!NT_SUCCESS(Status)) { RtlFreeUnicodeString(&LinkTarget); @@ -340,8 +340,8 @@ LdrpInitLoader(VOID) } Status = NtQuerySymbolicLinkObject(LinkHandle, - &LinkTarget, - &Length); + &LinkTarget, + &Length); NtClose(LinkHandle); if (!NT_SUCCESS(Status)) { @@ -351,7 +351,7 @@ LdrpInitLoader(VOID) } RtlCreateUnicodeString(&LdrpKnownDllPath, - LinkTarget.Buffer); + LinkTarget.Buffer); RtlFreeUnicodeString(&LinkTarget); @@ -441,8 +441,8 @@ LdrAdjustDllName (PUNICODE_STRING FullDllName, PLDR_MODULE LdrAddModuleEntry(PVOID ImageBase, - PIMAGE_NT_HEADERS NTHeaders, - PWSTR FullDosName) + PIMAGE_NT_HEADERS NTHeaders, + PWSTR FullDosName) { PLDR_MODULE Module; @@ -476,7 +476,7 @@ LdrAddModuleEntry(PVOID ImageBase, RtlCreateUnicodeString (&Module->BaseDllName, wcsrchr(FullDosName, L'\\') + 1); DPRINT ("BaseDllName %wZ\n", &Module->BaseDllName); - + RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock); InsertTailList(&NtCurrentPeb()->Ldr->InLoadOrderModuleList, &Module->InLoadOrderModuleList); @@ -488,8 +488,8 @@ LdrAddModuleEntry(PVOID ImageBase, static NTSTATUS LdrpMapKnownDll(IN PUNICODE_STRING DllName, - OUT PUNICODE_STRING FullDosName, - OUT PHANDLE SectionHandle) + OUT PUNICODE_STRING FullDosName, + OUT PHANDLE SectionHandle) { OBJECT_ATTRIBUTES ObjectAttributes; NTSTATUS Status; @@ -505,13 +505,13 @@ LdrpMapKnownDll(IN PUNICODE_STRING DllName, DPRINT("LdrpKnownDllPath '%wZ'\n", &LdrpKnownDllPath); InitializeObjectAttributes(&ObjectAttributes, - DllName, - OBJ_CASE_INSENSITIVE, - LdrpKnownDllsDirHandle, - NULL); + DllName, + OBJ_CASE_INSENSITIVE, + LdrpKnownDllsDirHandle, + NULL); Status = NtOpenSection(SectionHandle, - SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE, - &ObjectAttributes); + SECTION_MAP_READ | SECTION_MAP_WRITE | SECTION_MAP_EXECUTE, + &ObjectAttributes); if (!NT_SUCCESS(Status)) { DPRINT("NtOpenSection() failed for '%wZ' (Status %lx)\n", DllName, Status); @@ -521,8 +521,8 @@ LdrpMapKnownDll(IN PUNICODE_STRING DllName, FullDosName->Length = LdrpKnownDllPath.Length + DllName->Length + sizeof(WCHAR); FullDosName->MaximumLength = FullDosName->Length + sizeof(WCHAR); FullDosName->Buffer = RtlAllocateHeap(RtlGetProcessHeap(), - 0, - FullDosName->MaximumLength); + 0, + FullDosName->MaximumLength); if (FullDosName->Buffer == NULL) { FullDosName->Length = 0; @@ -544,9 +544,9 @@ LdrpMapKnownDll(IN PUNICODE_STRING DllName, static NTSTATUS LdrpMapDllImageFile(IN PWSTR SearchPath OPTIONAL, - IN PUNICODE_STRING DllName, - OUT PUNICODE_STRING FullDosName, - OUT PHANDLE SectionHandle) + IN PUNICODE_STRING DllName, + OUT PUNICODE_STRING FullDosName, + OUT PHANDLE SectionHandle) { WCHAR SearchPathBuffer[MAX_PATH]; WCHAR DosName[MAX_PATH]; @@ -559,20 +559,33 @@ LdrpMapDllImageFile(IN PWSTR SearchPath OPTIONAL, PVOID ImageBase; ULONG ImageSize; IO_STATUS_BLOCK IoStatusBlock; - NTSTATUS Status; + NTSTATUS Status; + ULONG len; DPRINT("LdrpMapDllImageFile() called\n"); if (SearchPath == NULL) { - SearchPath = SearchPathBuffer; - wcscpy (SearchPathBuffer, SharedUserData->NtSystemRoot); + /* get application running path */ + + wcscpy (SearchPathBuffer, NtCurrentPeb()->ProcessParameters->ImagePathName.Buffer); + + len = wcslen (SearchPathBuffer); + + while (len && SearchPathBuffer[len - 1] != L'\\') + len--; + + if (len) SearchPathBuffer[len-1] = L'\0'; + + wcscat (SearchPathBuffer, L";"); + + wcscat (SearchPathBuffer, SharedUserData->NtSystemRoot); wcscat (SearchPathBuffer, L"\\system32;"); wcscat (SearchPathBuffer, SharedUserData->NtSystemRoot); wcscat (SearchPathBuffer, L";."); - } - DPRINT("SearchPath %S\n", SearchPath); + SearchPath = SearchPathBuffer; + } if (RtlDosSearchPath_U (SearchPath, DllName->Buffer, @@ -582,7 +595,6 @@ LdrpMapDllImageFile(IN PWSTR SearchPath OPTIONAL, NULL) == 0) return STATUS_DLL_NOT_FOUND; - DPRINT("DosName %S\n", DosName); if (!RtlDosPathNameToNtPathName_U (DosName, &FullNtFileName, @@ -608,7 +620,7 @@ LdrpMapDllImageFile(IN PWSTR SearchPath OPTIONAL, FILE_SYNCHRONOUS_IO_NONALERT); if (!NT_SUCCESS(Status)) { - DPRINT1("Dll open of %wZ failed: Status = 0x%08x\n", + DPRINT1("Dll open of %wZ failed: Status = 0x%08x\n", &FullNtFileName, Status); RtlFreeUnicodeString (&FullNtFileName); return Status; @@ -631,7 +643,7 @@ LdrpMapDllImageFile(IN PWSTR SearchPath OPTIONAL, return Status; } /* - * Overlay DOS and NT headers structures to the + * Overlay DOS and NT headers structures to the * buffer with DLL's header raw data. */ DosHeader = (PIMAGE_DOS_HEADER) BlockBuffer; @@ -645,15 +657,15 @@ LdrpMapDllImageFile(IN PWSTR SearchPath OPTIONAL, { DPRINT("NTDLL format invalid\n"); NtClose(FileHandle); - + return STATUS_UNSUCCESSFUL; } - + ImageBase = (PVOID) NTHeaders->OptionalHeader.ImageBase; ImageSize = NTHeaders->OptionalHeader.SizeOfImage; - + DPRINT("ImageBase 0x%08x\n", ImageBase); - + /* * Create a section for dll. */ @@ -673,7 +685,7 @@ LdrpMapDllImageFile(IN PWSTR SearchPath OPTIONAL, } RtlCreateUnicodeString(FullDosName, - DosName); + DosName); return Status; } @@ -778,7 +790,7 @@ LdrFindEntryForAddress(PVOID Address, (Address <= (ModulePtr->BaseAddress + ModulePtr->SizeOfImage))) { *Module = ModulePtr; - RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock); + RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock); return(STATUS_SUCCESS); } @@ -810,7 +822,7 @@ LdrFindEntryForAddress(PVOID Address, static NTSTATUS LdrFindEntryForName(PUNICODE_STRING Name, PLDR_MODULE *Module, - BOOL Ref) + BOOL Ref) { PLIST_ENTRY ModuleListHead; PLIST_ENTRY Entry; @@ -849,7 +861,7 @@ LdrFindEntryForName(PUNICODE_STRING Name, ContainsPath = L'\\' == AdjustedName.Buffer[i] || L'/' == AdjustedName.Buffer[i]; } - + if (LdrpLastModule) { if ((! ContainsPath && @@ -858,12 +870,12 @@ LdrFindEntryForName(PUNICODE_STRING Name, 0 == RtlCompareUnicodeString(&LdrpLastModule->FullDllName, &AdjustedName, TRUE))) { *Module = LdrpLastModule; - if (Ref && (*Module)->LoadCount != -1) - { + if (Ref && (*Module)->LoadCount != -1) + { (*Module)->LoadCount++; - } + } RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock); - RtlFreeUnicodeString(&AdjustedName); + RtlFreeUnicodeString(&AdjustedName); return(STATUS_SUCCESS); } } @@ -879,12 +891,12 @@ LdrFindEntryForName(PUNICODE_STRING Name, 0 == RtlCompareUnicodeString(&ModulePtr->FullDllName, &AdjustedName, TRUE))) { *Module = LdrpLastModule = ModulePtr; - if (Ref && ModulePtr->LoadCount != -1) - { + if (Ref && ModulePtr->LoadCount != -1) + { ModulePtr->LoadCount++; } RtlLeaveCriticalSection(NtCurrentPeb()->LoaderLock); - RtlFreeUnicodeString(&AdjustedName); + RtlFreeUnicodeString(&AdjustedName); return(STATUS_SUCCESS); } @@ -933,8 +945,8 @@ LdrFixupForward(PCHAR ForwardName) NameBuffer); Status = LdrFindEntryForName (&DllName, &Module, FALSE); - /* FIXME: - * The caller (or the image) is responsible for loading of the dll, where the function is forwarded. + /* FIXME: + * The caller (or the image) is responsible for loading of the dll, where the function is forwarded. */ if (!NT_SUCCESS(Status)) { @@ -942,11 +954,11 @@ LdrFixupForward(PCHAR ForwardName) LDRP_PROCESS_CREATION_TIME, &DllName, &BaseAddress); - if (NT_SUCCESS(Status)) - { - Status = LdrFindEntryForName (&DllName, &Module, FALSE); - } - } + if (NT_SUCCESS(Status)) + { + Status = LdrFindEntryForName (&DllName, &Module, FALSE); + } + } RtlFreeUnicodeString (&DllName); if (!NT_SUCCESS(Status)) { @@ -955,7 +967,7 @@ LdrFixupForward(PCHAR ForwardName) } DPRINT("BaseAddress: %p\n", Module->BaseAddress); - + return LdrGetExportByName(Module->BaseAddress, p+1, -1); } @@ -966,7 +978,7 @@ LdrFixupForward(PCHAR ForwardName) /********************************************************************** * NAME LOCAL * LdrGetExportByOrdinal - * + * * DESCRIPTION * * ARGUMENTS @@ -980,7 +992,7 @@ LdrFixupForward(PCHAR ForwardName) */ static PVOID LdrGetExportByOrdinal ( - PVOID BaseAddress, + PVOID BaseAddress, ULONG Ordinal ) { @@ -1029,7 +1041,7 @@ LdrGetExportByOrdinal ( /********************************************************************** * NAME LOCAL * LdrGetExportByName - * + * * DESCRIPTION * * ARGUMENTS @@ -1039,7 +1051,7 @@ LdrGetExportByOrdinal ( * REVISIONS * * NOTE - * AddressOfNames and AddressOfNameOrdinals are paralell tables, + * AddressOfNames and AddressOfNameOrdinals are paralell tables, * both with NumberOfNames entries. * */ @@ -1058,7 +1070,7 @@ LdrGetExportByName(PVOID BaseAddress, PVOID Function; LONG minn, maxn; ULONG ExportDirSize; - + DPRINT("LdrGetExportByName %x %s %hu\n", BaseAddress, SymbolName, Hint); ExportDir = (PIMAGE_EXPORT_DIRECTORY) @@ -1076,7 +1088,7 @@ LdrGetExportByName(PVOID BaseAddress, //The symbol names may be missing entirely if (ExportDir->AddressOfNames == 0) { - DPRINT("LdrGetExportByName(): symbol names missing entirely\n"); + DPRINT("LdrGetExportByName(): symbol names missing entirely\n"); return NULL; } @@ -1089,7 +1101,7 @@ LdrGetExportByName(PVOID BaseAddress, ExportDir->AddressOfNameOrdinals); ExFunctions = (PDWORD *)RVA(BaseAddress, ExportDir->AddressOfFunctions); - + /* * Check the hint first */ @@ -1105,17 +1117,17 @@ LdrGetExportByName(PVOID BaseAddress, { DPRINT("Forward: %s\n", (PCHAR)Function); Function = LdrFixupForward((PCHAR)Function); - if (Function == NULL) - { + if (Function == NULL) + { DPRINT1("LdrGetExportByName(): failed to find %s\n",SymbolName); - } - return Function; + } + return Function; } if (Function != NULL) return Function; } } - + /* * Try a binary search first */ @@ -1139,11 +1151,11 @@ LdrGetExportByName(PVOID BaseAddress, { DPRINT("Forward: %s\n", (PCHAR)Function); Function = LdrFixupForward((PCHAR)Function); - if (Function == NULL) - { + if (Function == NULL) + { DPRINT1("LdrGetExportByName(): failed to find %s\n",SymbolName); - } - return Function; + } + return Function; } if (Function != NULL) return Function; @@ -1162,7 +1174,7 @@ LdrGetExportByName(PVOID BaseAddress, minn = mid + 1; } } - + /* * Fall back on a linear search */ @@ -1180,12 +1192,12 @@ LdrGetExportByName(PVOID BaseAddress, { DPRINT("Forward: %s\n", (PCHAR)Function); Function = LdrFixupForward((PCHAR)Function); - } - if (Function == NULL) - { - break; - } - return Function; + } + if (Function == NULL) + { + break; + } + return Function; } } DPRINT1("LdrGetExportByName(): failed to find %s\n",SymbolName); @@ -1196,10 +1208,10 @@ LdrGetExportByName(PVOID BaseAddress, /********************************************************************** * NAME LOCAL * LdrPerformRelocations - * + * * DESCRIPTION * Relocate a DLL's memory image. - * + * * ARGUMENTS * * RETURN VALUE @@ -1233,7 +1245,7 @@ static NTSTATUS LdrPerformRelocations (PIMAGE_NT_HEADERS NTHeaders, return STATUS_UNSUCCESSFUL; } - Sections = + Sections = (PIMAGE_SECTION_HEADER)((PVOID)NTHeaders + sizeof(IMAGE_NT_HEADERS)); MaxExtend = 0; for (i = 0; i < NTHeaders->FileHeader.NumberOfSections; i++) @@ -1241,19 +1253,19 @@ static NTSTATUS LdrPerformRelocations (PIMAGE_NT_HEADERS NTHeaders, if (!(Sections[i].Characteristics & IMAGE_SECTION_NOLOAD)) { ULONG Extend; - Extend = + Extend = (ULONG)(Sections[i].VirtualAddress + Sections[i].Misc.VirtualSize); MaxExtend = max(MaxExtend, Extend); } } - - RelocationDDir = + + RelocationDDir = &NTHeaders->OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_BASERELOC]; RelocationRVA = RelocationDDir->VirtualAddress; if (RelocationRVA) { - RelocationDir = + RelocationDir = (PRELOCATION_DIRECTORY)((PCHAR)ImageBase + RelocationRVA); while (RelocationDir->SizeOfBlock) @@ -1261,21 +1273,21 @@ static NTSTATUS LdrPerformRelocations (PIMAGE_NT_HEADERS NTHeaders, if (RelocationDir->VirtualAddress > MaxExtend) { RelocationRVA += RelocationDir->SizeOfBlock; - RelocationDir = + RelocationDir = (PRELOCATION_DIRECTORY) (ImageBase + RelocationRVA); continue; } Delta32 = (ULONG)(ImageBase - NTHeaders->OptionalHeader.ImageBase); - RelocationBlock = - (PRELOCATION_ENTRY) (RelocationRVA + ImageBase + - sizeof (RELOCATION_DIRECTORY)); - NumberOfEntries = + RelocationBlock = + (PRELOCATION_ENTRY) (RelocationRVA + ImageBase + + sizeof (RELOCATION_DIRECTORY)); + NumberOfEntries = RelocationDir->SizeOfBlock - sizeof (RELOCATION_DIRECTORY); NumberOfEntries = NumberOfEntries / sizeof (RELOCATION_ENTRY); Status = NtProtectVirtualMemory(NtCurrentProcess(), - ImageBase + + ImageBase + RelocationDir->VirtualAddress, PAGE_SIZE, PAGE_READWRITE, @@ -1288,25 +1300,25 @@ static NTSTATUS LdrPerformRelocations (PIMAGE_NT_HEADERS NTHeaders, if (RelocationDir->VirtualAddress + PAGE_SIZE < MaxExtend) { - Status = NtProtectVirtualMemory(NtCurrentProcess(), - ImageBase + - RelocationDir->VirtualAddress + PAGE_SIZE, - PAGE_SIZE, - PAGE_READWRITE, - &OldProtect2); - if (!NT_SUCCESS(Status)) - { - DPRINT1("Failed to unprotect relocation target (2).\n"); + Status = NtProtectVirtualMemory(NtCurrentProcess(), + ImageBase + + RelocationDir->VirtualAddress + PAGE_SIZE, + PAGE_SIZE, + PAGE_READWRITE, + &OldProtect2); + if (!NT_SUCCESS(Status)) + { + DPRINT1("Failed to unprotect relocation target (2).\n"); NtProtectVirtualMemory(NtCurrentProcess(), - ImageBase + + ImageBase + RelocationDir->VirtualAddress, PAGE_SIZE, OldProtect, &OldProtect); - return(Status); - } + return(Status); + } } - + for (i = 0; i < NumberOfEntries; i++) { Offset = (RelocationBlock[i].TypeOffset & 0xfff); @@ -1320,35 +1332,35 @@ static NTSTATUS LdrPerformRelocations (PIMAGE_NT_HEADERS NTHeaders, { case TYPE_RELOC_ABSOLUTE: break; - + case TYPE_RELOC_HIGH: pValue16 = (PUSHORT)Offset; *pValue16 += Delta32 >> 16; break; - + case TYPE_RELOC_LOW: pValue16 = (PUSHORT)Offset; *pValue16 += Delta32 & 0xffff; break; - + case TYPE_RELOC_HIGHLOW: pValue32 = (PULONG)Offset; *pValue32 += Delta32; break; - + case TYPE_RELOC_HIGHADJ: /* FIXME: do the highadjust fixup */ DPRINT("TYPE_RELOC_HIGHADJ fixup not implemented, sorry\n"); return(STATUS_UNSUCCESSFUL); - + default: DPRINT("unexpected fixup type\n"); return STATUS_UNSUCCESSFUL; - } + } } Status = NtProtectVirtualMemory(NtCurrentProcess(), - ImageBase + + ImageBase + RelocationDir->VirtualAddress, PAGE_SIZE, OldProtect, @@ -1361,32 +1373,32 @@ static NTSTATUS LdrPerformRelocations (PIMAGE_NT_HEADERS NTHeaders, if (RelocationDir->VirtualAddress + PAGE_SIZE < MaxExtend) { - Status = NtProtectVirtualMemory(NtCurrentProcess(), - ImageBase + - RelocationDir->VirtualAddress + PAGE_SIZE, - PAGE_SIZE, - OldProtect2, - &OldProtect2); - if (!NT_SUCCESS(Status)) - { - DPRINT1("Failed to protect relocation target2.\n"); - return(Status); - } + Status = NtProtectVirtualMemory(NtCurrentProcess(), + ImageBase + + RelocationDir->VirtualAddress + PAGE_SIZE, + PAGE_SIZE, + OldProtect2, + &OldProtect2); + if (!NT_SUCCESS(Status)) + { + DPRINT1("Failed to protect relocation target2.\n"); + return(Status); + } } RelocationRVA += RelocationDir->SizeOfBlock; - RelocationDir = + RelocationDir = (PRELOCATION_DIRECTORY) (ImageBase + RelocationRVA); } } return STATUS_SUCCESS; } - -static NTSTATUS + +static NTSTATUS LdrpGetOrLoadModule(PWCHAR SerachPath, - PCHAR Name, - PLDR_MODULE* Module, - BOOL Load) + PCHAR Name, + PLDR_MODULE* Module, + BOOL Load) { UNICODE_STRING DllName; NTSTATUS Status; @@ -1394,18 +1406,18 @@ LdrpGetOrLoadModule(PWCHAR SerachPath, DPRINT("LdrpGetOrLoadModule() called for %s\n", Name); RtlCreateUnicodeStringFromAsciiz (&DllName, Name); - + Status = LdrFindEntryForName (&DllName, Module, Load); if (Load && !NT_SUCCESS(Status)) { - Status = LdrpLoadModule(SerachPath, - NtCurrentPeb()->Ldr->Initialized ? 0 : LDRP_PROCESS_CREATION_TIME, - &DllName, - Module); + Status = LdrpLoadModule(SerachPath, + NtCurrentPeb()->Ldr->Initialized ? 0 : LDRP_PROCESS_CREATION_TIME, + &DllName, + Module); if (NT_SUCCESS(Status)) { - Status = LdrFindEntryForName (&DllName, Module, FALSE); - } + Status = LdrFindEntryForName (&DllName, Module, FALSE); + } if (!NT_SUCCESS(Status)) { DPRINT1("failed to load %wZ\n", &DllName); @@ -1417,8 +1429,8 @@ LdrpGetOrLoadModule(PWCHAR SerachPath, static NTSTATUS LdrpProcessImportDirectoryEntry( - PLDR_MODULE Module, - PLDR_MODULE ImportedModule, + PLDR_MODULE Module, + PLDR_MODULE ImportedModule, PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory) { NTSTATUS Status; @@ -1466,7 +1478,7 @@ LdrpProcessImportDirectoryEntry( DPRINT1("Failed to unprotect IAT.\n"); return(Status); } - + /* Walk through function list and fixup addresses. */ while (*FunctionNameList != 0L) { @@ -1490,7 +1502,7 @@ LdrpProcessImportDirectoryEntry( DPRINT1("Failed to import %s from %wZ\n", pe_name->Name, &ImportedModule->FullDllName); return STATUS_UNSUCCESSFUL; } - } + } ImportAddressList++; FunctionNameList++; } @@ -1512,8 +1524,8 @@ LdrpProcessImportDirectoryEntry( static NTSTATUS LdrpProcessImportDirectory( - PLDR_MODULE Module, - PLDR_MODULE ImportedModule, + PLDR_MODULE Module, + PLDR_MODULE ImportedModule, PCHAR ImportedName) { NTSTATUS Status; @@ -1525,9 +1537,9 @@ LdrpProcessImportDirectory( ImportModuleDirectory = (PIMAGE_IMPORT_MODULE_DIRECTORY) - RtlImageDirectoryEntryToData(Module->BaseAddress, - TRUE, - IMAGE_DIRECTORY_ENTRY_IMPORT, + RtlImageDirectoryEntryToData(Module->BaseAddress, + TRUE, + IMAGE_DIRECTORY_ENTRY_IMPORT, NULL); if (ImportModuleDirectory == NULL) { @@ -1539,13 +1551,13 @@ LdrpProcessImportDirectory( Name = (PCHAR)Module->BaseAddress + ImportModuleDirectory->dwRVAModuleName; if (0 == _stricmp(Name, ImportedName)) { - Status = LdrpProcessImportDirectoryEntry(Module, - ImportedModule, + Status = LdrpProcessImportDirectoryEntry(Module, + ImportedModule, ImportModuleDirectory); - if (!NT_SUCCESS(Status)) - { - return Status; - } + if (!NT_SUCCESS(Status)) + { + return Status; + } } ImportModuleDirectory++; } @@ -1555,10 +1567,10 @@ LdrpProcessImportDirectory( } -static NTSTATUS -LdrpAdjustImportDirectory(PLDR_MODULE Module, - PLDR_MODULE ImportedModule, - PUCHAR ImportedName) +static NTSTATUS +LdrpAdjustImportDirectory(PLDR_MODULE Module, + PLDR_MODULE ImportedModule, + PUCHAR ImportedName) { PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory; NTSTATUS Status; @@ -1577,10 +1589,10 @@ LdrpAdjustImportDirectory(PLDR_MODULE Module, Module, &Module->BaseDllName, ImportedModule, &ImportedModule->BaseDllName, ImportedName); ImportModuleDirectory = (PIMAGE_IMPORT_MODULE_DIRECTORY) - RtlImageDirectoryEntryToData(Module->BaseAddress, - TRUE, - IMAGE_DIRECTORY_ENTRY_IMPORT, - NULL); + RtlImageDirectoryEntryToData(Module->BaseAddress, + TRUE, + IMAGE_DIRECTORY_ENTRY_IMPORT, + NULL); if (ImportModuleDirectory == NULL) { return STATUS_UNSUCCESSFUL; @@ -1624,7 +1636,7 @@ LdrpAdjustImportDirectory(PLDR_MODULE Module, DPRINT1("Failed to unprotect IAT.\n"); return(Status); } - + NTHeaders = RtlImageNtHeader (ImportedModule->BaseAddress); Start = (PVOID)NTHeaders->OptionalHeader.ImageBase; End = Start + ImportedModule->SizeOfImage; @@ -1635,8 +1647,8 @@ LdrpAdjustImportDirectory(PLDR_MODULE Module, { if (*ImportAddressList >= Start && *ImportAddressList < End) { - (*ImportAddressList) += Offset; - } + (*ImportAddressList) += Offset; + } ImportAddressList++; FunctionNameList++; } @@ -1652,7 +1664,7 @@ LdrpAdjustImportDirectory(PLDR_MODULE Module, DPRINT1("Failed to protect IAT.\n"); return(Status); } - } + } ImportModuleDirectory++; } return STATUS_SUCCESS; @@ -1662,7 +1674,7 @@ LdrpAdjustImportDirectory(PLDR_MODULE Module, /********************************************************************** * NAME LOCAL * LdrFixupImports - * + * * DESCRIPTION * Compute the entry point for every symbol the DLL imports * from other modules. @@ -1676,9 +1688,9 @@ LdrpAdjustImportDirectory(PLDR_MODULE Module, * NOTE * */ -static NTSTATUS +static NTSTATUS LdrFixupImports(IN PWSTR SearchPath OPTIONAL, - IN PLDR_MODULE Module) + IN PLDR_MODULE Module) { PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory; PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectoryCurrent; @@ -1689,42 +1701,42 @@ LdrFixupImports(IN PWSTR SearchPath OPTIONAL, NTSTATUS Status; PLDR_MODULE ImportedModule; PCHAR ImportedName; - + DPRINT("LdrFixupImports(SearchPath %x, Module %x)\n", SearchPath, Module); - + /* Check for tls data */ TlsDirectory = (PIMAGE_TLS_DIRECTORY) - RtlImageDirectoryEntryToData(Module->BaseAddress, - TRUE, - IMAGE_DIRECTORY_ENTRY_TLS, - NULL); + RtlImageDirectoryEntryToData(Module->BaseAddress, + TRUE, + IMAGE_DIRECTORY_ENTRY_TLS, + NULL); if (TlsDirectory) { - TlsSize = TlsDirectory->EndAddressOfRawData - - TlsDirectory->StartAddressOfRawData - + TlsDirectory->SizeOfZeroFill; + TlsSize = TlsDirectory->EndAddressOfRawData + - TlsDirectory->StartAddressOfRawData + + TlsDirectory->SizeOfZeroFill; if (TlsSize > 0 && - NtCurrentPeb()->Ldr->Initialized) + NtCurrentPeb()->Ldr->Initialized) { TRACE_LDR("Trying to load dynamicly %wZ which contains a tls directory\n", - &Module->BaseDllName); - return STATUS_UNSUCCESSFUL; - } + &Module->BaseDllName); + return STATUS_UNSUCCESSFUL; + } } /* * Process each import module. */ ImportModuleDirectory = (PIMAGE_IMPORT_MODULE_DIRECTORY) - RtlImageDirectoryEntryToData(Module->BaseAddress, - TRUE, - IMAGE_DIRECTORY_ENTRY_IMPORT, - NULL); + RtlImageDirectoryEntryToData(Module->BaseAddress, + TRUE, + IMAGE_DIRECTORY_ENTRY_IMPORT, + NULL); BoundImportDescriptor = (PIMAGE_BOUND_IMPORT_DESCRIPTOR) - RtlImageDirectoryEntryToData(Module->BaseAddress, - TRUE, - IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT, - NULL); + RtlImageDirectoryEntryToData(Module->BaseAddress, + TRUE, + IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT, + NULL); if (BoundImportDescriptor != NULL && ImportModuleDirectory == NULL) { @@ -1738,123 +1750,123 @@ LdrFixupImports(IN PWSTR SearchPath OPTIONAL, BoundImportDescriptorCurrent = BoundImportDescriptor; while (BoundImportDescriptorCurrent->OffsetModuleName) { - ImportedName = (PCHAR)BoundImportDescriptor + BoundImportDescriptorCurrent->OffsetModuleName; - TRACE_LDR("%wZ bound to %s\n", &Module->BaseDllName, ImportedName); - Status = LdrpGetOrLoadModule(SearchPath, ImportedName, &ImportedModule, TRUE); - if (!NT_SUCCESS(Status)) - { - DPRINT1("failed to load %s\n", ImportedName); - return Status; - } - if (Module == ImportedModule) - { - LdrpDecrementLoadCount(Module, FALSE); - } - if (ImportedModule->TimeDateStamp != BoundImportDescriptorCurrent->TimeDateStamp) - { - TRACE_LDR("%wZ has stale binding to %wZ\n", - &Module->BaseDllName, &ImportedModule->BaseDllName); - Status = LdrpProcessImportDirectory(Module, ImportedModule, ImportedName); - if (!NT_SUCCESS(Status)) - { - DPRINT1("failed to import %s\n", ImportedName); - return Status; - } - } - else - { - BOOL WrongForwarder; - WrongForwarder = FALSE; - if (ImportedModule->Flags & IMAGE_NOT_AT_BASE) - { - TRACE_LDR("%wZ has stale binding to %s\n", - &Module->BaseDllName, ImportedName); - } - else - { - TRACE_LDR("%wZ has correct binding to %wZ\n", - &Module->BaseDllName, &ImportedModule->BaseDllName); - } - if (BoundImportDescriptorCurrent->NumberOfModuleForwarderRefs) - { - PIMAGE_BOUND_FORWARDER_REF BoundForwarderRef; - ULONG i; - PLDR_MODULE ForwarderModule; - PUCHAR ForwarderName; + ImportedName = (PCHAR)BoundImportDescriptor + BoundImportDescriptorCurrent->OffsetModuleName; + TRACE_LDR("%wZ bound to %s\n", &Module->BaseDllName, ImportedName); + Status = LdrpGetOrLoadModule(SearchPath, ImportedName, &ImportedModule, TRUE); + if (!NT_SUCCESS(Status)) + { + DPRINT1("failed to load %s\n", ImportedName); + return Status; + } + if (Module == ImportedModule) + { + LdrpDecrementLoadCount(Module, FALSE); + } + if (ImportedModule->TimeDateStamp != BoundImportDescriptorCurrent->TimeDateStamp) + { + TRACE_LDR("%wZ has stale binding to %wZ\n", + &Module->BaseDllName, &ImportedModule->BaseDllName); + Status = LdrpProcessImportDirectory(Module, ImportedModule, ImportedName); + if (!NT_SUCCESS(Status)) + { + DPRINT1("failed to import %s\n", ImportedName); + return Status; + } + } + else + { + BOOL WrongForwarder; + WrongForwarder = FALSE; + if (ImportedModule->Flags & IMAGE_NOT_AT_BASE) + { + TRACE_LDR("%wZ has stale binding to %s\n", + &Module->BaseDllName, ImportedName); + } + else + { + TRACE_LDR("%wZ has correct binding to %wZ\n", + &Module->BaseDllName, &ImportedModule->BaseDllName); + } + if (BoundImportDescriptorCurrent->NumberOfModuleForwarderRefs) + { + PIMAGE_BOUND_FORWARDER_REF BoundForwarderRef; + ULONG i; + PLDR_MODULE ForwarderModule; + PUCHAR ForwarderName; BoundForwarderRef = (PIMAGE_BOUND_FORWARDER_REF)(BoundImportDescriptorCurrent + 1); - for (i = 0; i < BoundImportDescriptorCurrent->NumberOfModuleForwarderRefs; i++, BoundForwarderRef++) - { - ForwarderName = (PCHAR)BoundImportDescriptor + BoundForwarderRef->OffsetModuleName; - TRACE_LDR("%wZ bound to %s via forwardes from %s\n", - &Module->BaseDllName, ForwarderName, ImportedName); - Status = LdrpGetOrLoadModule(SearchPath, ForwarderName, &ForwarderModule, TRUE); - if (!NT_SUCCESS(Status)) - { - DPRINT1("failed to load %s\n", ForwarderName); - return Status; - } - if (Module == ImportedModule) - { - LdrpDecrementLoadCount(Module, FALSE); - } - if (ForwarderModule->TimeDateStamp != BoundForwarderRef->TimeDateStamp || - ForwarderModule->Flags & IMAGE_NOT_AT_BASE) - { - TRACE_LDR("%wZ has stale binding to %s\n", - &Module->BaseDllName, ForwarderName); - WrongForwarder = TRUE; - } - else - { - TRACE_LDR("%wZ has correct binding to %s\n", - &Module->BaseDllName, ForwarderName); - } - } - } - if (WrongForwarder || - ImportedModule->Flags & IMAGE_NOT_AT_BASE) - { + for (i = 0; i < BoundImportDescriptorCurrent->NumberOfModuleForwarderRefs; i++, BoundForwarderRef++) + { + ForwarderName = (PCHAR)BoundImportDescriptor + BoundForwarderRef->OffsetModuleName; + TRACE_LDR("%wZ bound to %s via forwardes from %s\n", + &Module->BaseDllName, ForwarderName, ImportedName); + Status = LdrpGetOrLoadModule(SearchPath, ForwarderName, &ForwarderModule, TRUE); + if (!NT_SUCCESS(Status)) + { + DPRINT1("failed to load %s\n", ForwarderName); + return Status; + } + if (Module == ImportedModule) + { + LdrpDecrementLoadCount(Module, FALSE); + } + if (ForwarderModule->TimeDateStamp != BoundForwarderRef->TimeDateStamp || + ForwarderModule->Flags & IMAGE_NOT_AT_BASE) + { + TRACE_LDR("%wZ has stale binding to %s\n", + &Module->BaseDllName, ForwarderName); + WrongForwarder = TRUE; + } + else + { + TRACE_LDR("%wZ has correct binding to %s\n", + &Module->BaseDllName, ForwarderName); + } + } + } + if (WrongForwarder || + ImportedModule->Flags & IMAGE_NOT_AT_BASE) + { Status = LdrpProcessImportDirectory(Module, ImportedModule, ImportedName); - if (!NT_SUCCESS(Status)) - { - DPRINT1("failed to import %s\n", ImportedName); - return Status; - } - } - else if (ImportedModule->Flags & IMAGE_NOT_AT_BASE) - { - TRACE_LDR("Adjust imports for %s from %wZ\n", - ImportedName, &Module->BaseDllName); + if (!NT_SUCCESS(Status)) + { + DPRINT1("failed to import %s\n", ImportedName); + return Status; + } + } + else if (ImportedModule->Flags & IMAGE_NOT_AT_BASE) + { + TRACE_LDR("Adjust imports for %s from %wZ\n", + ImportedName, &Module->BaseDllName); Status = LdrpAdjustImportDirectory(Module, ImportedModule, ImportedName); - if (!NT_SUCCESS(Status)) - { - DPRINT1("failed to adjust import entries for %s\n", ImportedName); - return Status; - } - } - else if (WrongForwarder) - { - /* - * FIXME: - * Update only forwarders - */ - TRACE_LDR("Stale BIND %s from %wZ\n", - ImportedName, &Module->BaseDllName); + if (!NT_SUCCESS(Status)) + { + DPRINT1("failed to adjust import entries for %s\n", ImportedName); + return Status; + } + } + else if (WrongForwarder) + { + /* + * FIXME: + * Update only forwarders + */ + TRACE_LDR("Stale BIND %s from %wZ\n", + ImportedName, &Module->BaseDllName); Status = LdrpProcessImportDirectory(Module, ImportedModule, ImportedName); - if (!NT_SUCCESS(Status)) - { - DPRINT1("faild to import %s\n", ImportedName); - return Status; - } - } - else - { - /* nothing to do */ - } - } + if (!NT_SUCCESS(Status)) + { + DPRINT1("faild to import %s\n", ImportedName); + return Status; + } + } + else + { + /* nothing to do */ + } + } BoundImportDescriptorCurrent += BoundImportDescriptorCurrent->NumberOfModuleForwarderRefs + 1; - } + } } else if (ImportModuleDirectory) { @@ -1863,8 +1875,8 @@ LdrFixupImports(IN PWSTR SearchPath OPTIONAL, ImportModuleDirectoryCurrent = ImportModuleDirectory; while (ImportModuleDirectoryCurrent->dwRVAModuleName) { - ImportedName = (PCHAR)Module->BaseAddress + ImportModuleDirectoryCurrent->dwRVAModuleName; - TRACE_LDR("%wZ imports functions from %s\n", &Module->BaseDllName, ImportedName); + ImportedName = (PCHAR)Module->BaseAddress + ImportModuleDirectoryCurrent->dwRVAModuleName; + TRACE_LDR("%wZ imports functions from %s\n", &Module->BaseDllName, ImportedName); Status = LdrpGetOrLoadModule(SearchPath, ImportedName, &ImportedModule, TRUE); if (!NT_SUCCESS(Status)) @@ -1877,16 +1889,16 @@ LdrFixupImports(IN PWSTR SearchPath OPTIONAL, LdrpDecrementLoadCount(Module, FALSE); } - TRACE_LDR("Initializing imports for %wZ from %s\n", - &Module->BaseDllName, ImportedName); + TRACE_LDR("Initializing imports for %wZ from %s\n", + &Module->BaseDllName, ImportedName); Status = LdrpProcessImportDirectoryEntry(Module, ImportedModule, ImportModuleDirectoryCurrent); - if (!NT_SUCCESS(Status)) - { - DPRINT1("failed to import %s\n", ImportedName); - return Status; - } - ImportModuleDirectoryCurrent++; - } + if (!NT_SUCCESS(Status)) + { + DPRINT1("failed to import %s\n", ImportedName); + return Status; + } + ImportModuleDirectoryCurrent++; + } } if (TlsDirectory && TlsSize > 0) @@ -1911,7 +1923,7 @@ LdrFixupImports(IN PWSTR SearchPath OPTIONAL, * ImageBase * Address at which the EXE's image * is loaded. - * + * * SectionHandle * Handle of the section that contains * the EXE's image. @@ -1974,10 +1986,10 @@ PEPFUNC LdrPEStartup (PVOID ImageBase, Status = LdrFindEntryForAddress(ImageBase, Module); if (!NT_SUCCESS(Status)) { - return NULL; - } + return NULL; + } } - + if (ImageBase != (PVOID) NTHeaders->OptionalHeader.ImageBase) { (*Module)->Flags |= IMAGE_NOT_AT_BASE; @@ -2028,11 +2040,11 @@ PEPFUNC LdrPEStartup (PVOID ImageBase, return EntryPoint; } -static NTSTATUS +static NTSTATUS LdrpLoadModule(IN PWSTR SearchPath OPTIONAL, - IN ULONG LoadFlags, - IN PUNICODE_STRING Name, - PLDR_MODULE *Module) + IN ULONG LoadFlags, + IN PUNICODE_STRING Name, + PLDR_MODULE *Module) { UNICODE_STRING AdjustedName; UNICODE_STRING FullDosName; @@ -2062,21 +2074,21 @@ LdrpLoadModule(IN PWSTR SearchPath OPTIONAL, { /* Open or create dll image section */ Status = LdrpMapKnownDll(&AdjustedName, &FullDosName, &SectionHandle); - if (!NT_SUCCESS(Status)) - { + if (!NT_SUCCESS(Status)) + { Status = LdrpMapDllImageFile(SearchPath, &AdjustedName, &FullDosName, &SectionHandle); - } - if (!NT_SUCCESS(Status)) - { + } + if (!NT_SUCCESS(Status)) + { DPRINT1("Failed to create or open dll section of '%wZ' (Status %lx)\n", &AdjustedName, Status); RtlFreeUnicodeString(&AdjustedName); RtlFreeUnicodeString(&FullDosName); return Status; - } + } RtlFreeUnicodeString(&AdjustedName); - /* Map the dll into the process */ - ViewSize = 0; - ImageBase = 0; + /* Map the dll into the process */ + ViewSize = 0; + ImageBase = 0; Status = NtMapViewOfSection(SectionHandle, NtCurrentProcess(), &ImageBase, @@ -2115,43 +2127,43 @@ LdrpLoadModule(IN PWSTR SearchPath OPTIONAL, if (!NT_SUCCESS(Status)) { DPRINT1("LdrPerformRelocations() failed\n"); - NtUnmapViewOfSection (NtCurrentProcess (), ImageBase); + NtUnmapViewOfSection (NtCurrentProcess (), ImageBase); NtClose (SectionHandle); RtlFreeUnicodeString(&FullDosName); return STATUS_UNSUCCESSFUL; - } + } } *Module = LdrAddModuleEntry(ImageBase, NtHeaders, FullDosName.Buffer); (*Module)->SectionHandle = SectionHandle; if (ImageBase != (PVOID) NtHeaders->OptionalHeader.ImageBase) { - (*Module)->Flags |= IMAGE_NOT_AT_BASE; - } + (*Module)->Flags |= IMAGE_NOT_AT_BASE; + } if (NtHeaders->FileHeader.Characteristics & IMAGE_FILE_DLL) { - (*Module)->Flags |= IMAGE_DLL; - } + (*Module)->Flags |= IMAGE_DLL; + } /* fixup the imported calls entry points */ Status = LdrFixupImports(SearchPath, *Module); if (!NT_SUCCESS(Status)) { - DPRINT1("LdrFixupImports failed for %wZ, status=%x\n", &(*Module)->BaseDllName, Status); - return Status; - } + DPRINT1("LdrFixupImports failed for %wZ, status=%x\n", &(*Module)->BaseDllName, Status); + return Status; + } #ifdef KDBG LdrpLoadUserModuleSymbols(*Module); #endif RtlEnterCriticalSection(NtCurrentPeb()->LoaderLock); - InsertTailList(&NtCurrentPeb()->Ldr->InInitializationOrderModuleList, - &(*Module)->InInitializationOrderModuleList); + InsertTailList(&NtCurrentPeb()->Ldr->InInitializationOrderModuleList, + &(*Module)->InInitializationOrderModuleList); RtlLeaveCriticalSection (NtCurrentPeb()->LoaderLock); } return STATUS_SUCCESS; } -static NTSTATUS -LdrpUnloadModule(PLDR_MODULE Module, - BOOL Unload) +static NTSTATUS +LdrpUnloadModule(PLDR_MODULE Module, + BOOL Unload) { PIMAGE_IMPORT_MODULE_DIRECTORY ImportModuleDirectory; PIMAGE_BOUND_IMPORT_DESCRIPTOR BoundImportDescriptor; @@ -2170,7 +2182,7 @@ LdrpUnloadModule(PLDR_MODULE Module, LoadCount = LdrpDecrementLoadCount(Module, Unload); TRACE_LDR("Unload %wZ, LoadCount %d\n", &Module->BaseDllName, LoadCount); - + if (LoadCount == 0) { /* ?????????????????? */ @@ -2179,70 +2191,70 @@ LdrpUnloadModule(PLDR_MODULE Module, else if (LoadCount == 1) { BoundImportDescriptor = (PIMAGE_BOUND_IMPORT_DESCRIPTOR) - RtlImageDirectoryEntryToData(Module->BaseAddress, - TRUE, - IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT, - NULL); + RtlImageDirectoryEntryToData(Module->BaseAddress, + TRUE, + IMAGE_DIRECTORY_ENTRY_BOUND_IMPORT, + NULL); if (BoundImportDescriptor) { - /* dereferencing all imported modules, use the bound import descriptor */ + /* dereferencing all imported modules, use the bound import descriptor */ BoundImportDescriptorCurrent = BoundImportDescriptor; while (BoundImportDescriptorCurrent->OffsetModuleName) { - ImportedName = (PCHAR)BoundImportDescriptor + BoundImportDescriptorCurrent->OffsetModuleName; - TRACE_LDR("%wZ trys to unload %s\n", &Module->BaseDllName, ImportedName); + ImportedName = (PCHAR)BoundImportDescriptor + BoundImportDescriptorCurrent->OffsetModuleName; + TRACE_LDR("%wZ trys to unload %s\n", &Module->BaseDllName, ImportedName); Status = LdrpGetOrLoadModule(NULL, ImportedName, &ImportedModule, FALSE); - if (!NT_SUCCESS(Status)) - { - DPRINT1("unable to found imported modul %s\n", ImportedName); - } - else - { - if (Module != ImportedModule) - { + if (!NT_SUCCESS(Status)) + { + DPRINT1("unable to found imported modul %s\n", ImportedName); + } + else + { + if (Module != ImportedModule) + { Status = LdrpUnloadModule(ImportedModule, FALSE); if (!NT_SUCCESS(Status)) - { - DPRINT1("unable to unload %s\n", ImportedName); - } - } - } - BoundImportDescriptorCurrent++; - } + { + DPRINT1("unable to unload %s\n", ImportedName); + } + } + } + BoundImportDescriptorCurrent++; + } } - else + else { ImportModuleDirectory = (PIMAGE_IMPORT_MODULE_DIRECTORY) - RtlImageDirectoryEntryToData(Module->BaseAddress, - TRUE, - IMAGE_DIRECTORY_ENTRY_IMPORT, - NULL); + RtlImageDirectoryEntryToData(Module->BaseAddress, + TRUE, + IMAGE_DIRECTORY_ENTRY_IMPORT, + NULL); if (ImportModuleDirectory) - { - /* dereferencing all imported modules, use the import descriptor */ - while (ImportModuleDirectory->dwRVAModuleName) - { - ImportedName = (PCHAR)Module->BaseAddress + ImportModuleDirectory->dwRVAModuleName; - TRACE_LDR("%wZ trys to unload %s\n", &Module->BaseDllName, ImportedName); + { + /* dereferencing all imported modules, use the import descriptor */ + while (ImportModuleDirectory->dwRVAModuleName) + { + ImportedName = (PCHAR)Module->BaseAddress + ImportModuleDirectory->dwRVAModuleName; + TRACE_LDR("%wZ trys to unload %s\n", &Module->BaseDllName, ImportedName); Status = LdrpGetOrLoadModule(NULL, ImportedName, &ImportedModule, FALSE); - if (!NT_SUCCESS(Status)) - { - DPRINT1("unable to found imported modul %s\n", ImportedName); - } - else - { - if (Module != ImportedModule) - { - Status = LdrpUnloadModule(ImportedModule, FALSE); - if (!NT_SUCCESS(Status)) - { - DPRINT1("unable to unload %s\n", ImportedName); - } - } - } - ImportModuleDirectory++; - } - } + if (!NT_SUCCESS(Status)) + { + DPRINT1("unable to found imported modul %s\n", ImportedName); + } + else + { + if (Module != ImportedModule) + { + Status = LdrpUnloadModule(ImportedModule, FALSE); + if (!NT_SUCCESS(Status)) + { + DPRINT1("unable to unload %s\n", ImportedName); + } + } + } + ImportModuleDirectory++; + } + } } } @@ -2293,16 +2305,16 @@ LdrDisableThreadCalloutsForDll(IN PVOID BaseAddress) RtlEnterCriticalSection (NtCurrentPeb()->LoaderLock); ModuleListHead = &NtCurrentPeb()->Ldr->InLoadOrderModuleList; Entry = ModuleListHead->Flink; - while (Entry != ModuleListHead) + while (Entry != ModuleListHead) { Module = CONTAINING_RECORD(Entry, LDR_MODULE, InLoadOrderModuleList); DPRINT("BaseDllName %wZ BaseAddress %x\n", &Module->BaseDllName, Module->BaseAddress); - if (Module->BaseAddress == BaseAddress) - { - if (Module->TlsIndex == -1) - { + if (Module->BaseAddress == BaseAddress) + { + if (Module->TlsIndex == -1) + { Module->Flags |= DONT_CALL_FOR_THREAD; Status = STATUS_SUCCESS; } @@ -2329,7 +2341,7 @@ LdrGetDllHandle(IN PWCHAR Path OPTIONAL, TRACE_LDR("LdrGetDllHandle, searching for %wZ from %S\n", DllName, Path ? Path : L""); /* NULL is the current executable */ - if (DllName == NULL) + if (DllName == NULL) { *BaseAddress = ExeModule->BaseAddress; DPRINT("BaseAddress %x\n", *BaseAddress); @@ -2375,7 +2387,7 @@ LdrGetProcedureAddress (IN PVOID BaseAddress, /* by name */ *ProcedureAddress = LdrGetExportByName(BaseAddress, Name->Buffer, 0xffff); if (*ProcedureAddress != NULL) - { + { return STATUS_SUCCESS; } DPRINT("LdrGetProcedureAddress: Can't resolve symbol '%Z'\n", Name); @@ -2397,7 +2409,7 @@ LdrGetProcedureAddress (IN PVOID BaseAddress, /********************************************************************** * NAME LOCAL * LdrpDetachProcess - * + * * DESCRIPTION * Unload dll's which are no longer referenced from others dll's * @@ -2410,9 +2422,9 @@ LdrGetProcedureAddress (IN PVOID BaseAddress, * REVISIONS * * NOTE - * The loader lock must be held on enty. + * The loader lock must be held on enty. */ -static VOID +static VOID LdrpDetachProcess(BOOL UnloadAll) { PLIST_ENTRY ModuleListHead; @@ -2431,56 +2443,56 @@ LdrpDetachProcess(BOOL UnloadAll) { Module = CONTAINING_RECORD(Entry, LDR_MODULE, InInitializationOrderModuleList); if (((UnloadAll && Module->LoadCount <= 0) || Module->LoadCount == 0) && - Module->Flags & ENTRY_PROCESSED && - !(Module->Flags & UNLOAD_IN_PROGRESS)) + Module->Flags & ENTRY_PROCESSED && + !(Module->Flags & UNLOAD_IN_PROGRESS)) { Module->Flags |= UNLOAD_IN_PROGRESS; - if (Module == LdrpLastModule) - { - LdrpLastModule = NULL; - } + if (Module == LdrpLastModule) + { + LdrpLastModule = NULL; + } if (Module->Flags & PROCESS_ATTACH_CALLED) - { - TRACE_LDR("Unload %wZ - Calling entry point at %x\n", - &Module->BaseDllName, Module->EntryPoint); - LdrpCallDllEntry(Module, DLL_PROCESS_DETACH, (PVOID)(Module->LoadCount == -1 ? 1 : 0)); - } - else - { + { + TRACE_LDR("Unload %wZ - Calling entry point at %x\n", + &Module->BaseDllName, Module->EntryPoint); + LdrpCallDllEntry(Module, DLL_PROCESS_DETACH, (PVOID)(Module->LoadCount == -1 ? 1 : 0)); + } + else + { TRACE_LDR("Unload %wZ\n", &Module->BaseDllName); - } - Entry = ModuleListHead->Blink; - } + } + Entry = ModuleListHead->Blink; + } else { - Entry = Entry->Blink; - } + Entry = Entry->Blink; + } } - + if (CallingCount == 1) { Entry = ModuleListHead->Blink; while (Entry != ModuleListHead) { Module = CONTAINING_RECORD(Entry, LDR_MODULE, InInitializationOrderModuleList); - Entry = Entry->Blink; - if (Module->Flags & UNLOAD_IN_PROGRESS && - ((UnloadAll && Module->LoadCount >= 0) || Module->LoadCount == 0)) - { + Entry = Entry->Blink; + if (Module->Flags & UNLOAD_IN_PROGRESS && + ((UnloadAll && Module->LoadCount >= 0) || Module->LoadCount == 0)) + { /* remove the module entry from the list */ RemoveEntryList (&Module->InLoadOrderModuleList) RemoveEntryList (&Module->InInitializationOrderModuleList); - NtUnmapViewOfSection (NtCurrentProcess (), Module->BaseAddress); + NtUnmapViewOfSection (NtCurrentProcess (), Module->BaseAddress); NtClose (Module->SectionHandle); - TRACE_LDR("%wZ unloaded\n", &Module->BaseDllName); + TRACE_LDR("%wZ unloaded\n", &Module->BaseDllName); RtlFreeUnicodeString (&Module->FullDllName); RtlFreeUnicodeString (&Module->BaseDllName); RtlFreeHeap (RtlGetProcessHeap (), 0, Module); - } + } } } CallingCount--; @@ -2490,7 +2502,7 @@ LdrpDetachProcess(BOOL UnloadAll) /********************************************************************** * NAME LOCAL * LdrpAttachProcess - * + * * DESCRIPTION * Initialize all dll's which are prepered for loading * @@ -2524,26 +2536,26 @@ LdrpAttachProcess(VOID) { Module = CONTAINING_RECORD(Entry, LDR_MODULE, InInitializationOrderModuleList); if (!(Module->Flags & (LOAD_IN_PROGRESS|UNLOAD_IN_PROGRESS|ENTRY_PROCESSED))) - { - Module->Flags |= LOAD_IN_PROGRESS; + { + Module->Flags |= LOAD_IN_PROGRESS; TRACE_LDR("%wZ loaded - Calling init routine at %x for process attaching\n", - &Module->BaseDllName, Module->EntryPoint); - Result = LdrpCallDllEntry(Module, DLL_PROCESS_ATTACH, (PVOID)(Module->LoadCount == -1 ? 1 : 0)); + &Module->BaseDllName, Module->EntryPoint); + Result = LdrpCallDllEntry(Module, DLL_PROCESS_ATTACH, (PVOID)(Module->LoadCount == -1 ? 1 : 0)); if (!Result) { - Status = STATUS_DLL_INIT_FAILED; - break; - } - if (Module->Flags & IMAGE_DLL && Module->EntryPoint != 0) - { - Module->Flags |= PROCESS_ATTACH_CALLED|ENTRY_PROCESSED; - } - else - { - Module->Flags |= ENTRY_PROCESSED; - } + Status = STATUS_DLL_INIT_FAILED; + break; + } + if (Module->Flags & IMAGE_DLL && Module->EntryPoint != 0) + { + Module->Flags |= PROCESS_ATTACH_CALLED|ENTRY_PROCESSED; + } + else + { + Module->Flags |= ENTRY_PROCESSED; + } Module->Flags &= ~LOAD_IN_PROGRESS; - } + } Entry = Entry->Flink; } @@ -2566,7 +2578,7 @@ LdrShutdownProcess (VOID) * @implemented */ -NTSTATUS +NTSTATUS LdrpAttachThread (VOID) { PLIST_ENTRY ModuleListHead; @@ -2590,13 +2602,13 @@ LdrpAttachThread (VOID) while (Entry != ModuleListHead) { Module = CONTAINING_RECORD(Entry, LDR_MODULE, InInitializationOrderModuleList); - if (Module->Flags & PROCESS_ATTACH_CALLED && - !(Module->Flags & DONT_CALL_FOR_THREAD) && + if (Module->Flags & PROCESS_ATTACH_CALLED && + !(Module->Flags & DONT_CALL_FOR_THREAD) && !(Module->Flags & UNLOAD_IN_PROGRESS)) - { - TRACE_LDR("%wZ - Calling entry point at %x for thread attaching\n", - &Module->BaseDllName, Module->EntryPoint); - LdrpCallDllEntry(Module, DLL_THREAD_ATTACH, NULL); + { + TRACE_LDR("%wZ - Calling entry point at %x for thread attaching\n", + &Module->BaseDllName, Module->EntryPoint); + LdrpCallDllEntry(Module, DLL_THREAD_ATTACH, NULL); } Entry = Entry->Flink; } @@ -2636,11 +2648,11 @@ LdrShutdownThread (VOID) Module = CONTAINING_RECORD(Entry, LDR_MODULE, InInitializationOrderModuleList); if (Module->Flags & PROCESS_ATTACH_CALLED && - !(Module->Flags & DONT_CALL_FOR_THREAD) && + !(Module->Flags & DONT_CALL_FOR_THREAD) && !(Module->Flags & UNLOAD_IN_PROGRESS)) { - TRACE_LDR("%wZ - Calling entry point at %x for thread detaching\n", - &Module->BaseDllName, Module->EntryPoint); + TRACE_LDR("%wZ - Calling entry point at %x for thread detaching\n", + &Module->BaseDllName, Module->EntryPoint); LdrpCallDllEntry(Module, DLL_THREAD_DETACH, NULL); } Entry = Entry->Blink; @@ -2762,7 +2774,7 @@ LdrQueryProcessModuleInformation(IN PMODULE_INFORMATION ModuleInformation OPTION static BOOLEAN LdrpCheckImageChecksum (IN PVOID BaseAddress, - IN ULONG ImageSize) + IN ULONG ImageSize) { PIMAGE_NT_HEADERS Header; PUSHORT Ptr; @@ -2785,9 +2797,9 @@ LdrpCheckImageChecksum (IN PVOID BaseAddress, { Sum += (ULONG)*Ptr; if (HIWORD(Sum) != 0) - { - Sum = LOWORD(Sum) + HIWORD(Sum); - } + { + Sum = LOWORD(Sum) + HIWORD(Sum); + } Ptr++; } @@ -2795,9 +2807,9 @@ LdrpCheckImageChecksum (IN PVOID BaseAddress, { Sum += (ULONG)*((PUCHAR)Ptr); if (HIWORD(Sum) != 0) - { - Sum = LOWORD(Sum) + HIWORD(Sum); - } + { + Sum = LOWORD(Sum) + HIWORD(Sum); + } } CalcSum = (USHORT)(LOWORD(Sum) + HIWORD(Sum)); @@ -2848,9 +2860,9 @@ LdrpCheckImageChecksum (IN PVOID BaseAddress, */ NTSTATUS STDCALL LdrVerifyImageMatchesChecksum (IN HANDLE FileHandle, - ULONG Unknown1, - ULONG Unknown2, - ULONG Unknown3) + ULONG Unknown1, + ULONG Unknown2, + ULONG Unknown3) { FILE_STANDARD_INFORMATION FileInfo; IO_STATUS_BLOCK IoStatusBlock; @@ -2863,12 +2875,12 @@ LdrVerifyImageMatchesChecksum (IN HANDLE FileHandle, DPRINT ("LdrVerifyImageMatchesChecksum() called\n"); Status = NtCreateSection (&SectionHandle, - SECTION_MAP_EXECUTE, - NULL, - NULL, - PAGE_EXECUTE, - SEC_COMMIT, - FileHandle); + SECTION_MAP_EXECUTE, + NULL, + NULL, + PAGE_EXECUTE, + SEC_COMMIT, + FileHandle); if (!NT_SUCCESS(Status)) { DPRINT1 ("NtCreateSection() failed (Status %lx)\n", Status); @@ -2878,15 +2890,15 @@ LdrVerifyImageMatchesChecksum (IN HANDLE FileHandle, ViewSize = 0; BaseAddress = NULL; Status = NtMapViewOfSection (SectionHandle, - NtCurrentProcess (), - &BaseAddress, - 0, - 0, - NULL, - &ViewSize, - ViewShare, - 0, - PAGE_EXECUTE); + NtCurrentProcess (), + &BaseAddress, + 0, + 0, + NULL, + &ViewSize, + ViewShare, + 0, + PAGE_EXECUTE); if (!NT_SUCCESS(Status)) { DPRINT1 ("NtMapViewOfSection() failed (Status %lx)\n", Status); @@ -2895,28 +2907,28 @@ LdrVerifyImageMatchesChecksum (IN HANDLE FileHandle, } Status = NtQueryInformationFile (FileHandle, - &IoStatusBlock, - &FileInfo, - sizeof (FILE_STANDARD_INFORMATION), - FileStandardInformation); + &IoStatusBlock, + &FileInfo, + sizeof (FILE_STANDARD_INFORMATION), + FileStandardInformation); if (!NT_SUCCESS(Status)) { DPRINT1 ("NtMapViewOfSection() failed (Status %lx)\n", Status); NtUnmapViewOfSection (NtCurrentProcess (), - BaseAddress); + BaseAddress); NtClose (SectionHandle); return Status; } Result = LdrpCheckImageChecksum (BaseAddress, - FileInfo.EndOfFile.u.LowPart); + FileInfo.EndOfFile.u.LowPart); if (Result == FALSE) { Status = STATUS_IMAGE_CHECKSUM_MISMATCH; } NtUnmapViewOfSection (NtCurrentProcess (), - BaseAddress); + BaseAddress); NtClose (SectionHandle); @@ -2942,11 +2954,11 @@ LdrVerifyImageMatchesChecksum (IN HANDLE FileHandle, */ NTSTATUS STDCALL LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey, - IN PCWSTR ValueName, - IN ULONG Type, - OUT PVOID Buffer, - IN ULONG BufferSize, - OUT PULONG ReturnedLength OPTIONAL) + IN PCWSTR ValueName, + IN ULONG Type, + OUT PVOID Buffer, + IN ULONG BufferSize, + OUT PULONG ReturnedLength OPTIONAL) { PKEY_VALUE_PARTIAL_INFORMATION KeyInfo; OBJECT_ATTRIBUTES ObjectAttributes; @@ -2960,7 +2972,7 @@ LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey, NTSTATUS Status; wcscpy (NameBuffer, - L"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\"); + L"\\Registry\\Machine\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Image File Execution Options\\"); Ptr = wcsrchr (SubKey->Buffer, L'\\'); if (Ptr == NULL) { @@ -2972,17 +2984,17 @@ LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey, } wcscat (NameBuffer, Ptr); RtlInitUnicodeString (&KeyName, - NameBuffer); + NameBuffer); InitializeObjectAttributes (&ObjectAttributes, - &KeyName, - OBJ_CASE_INSENSITIVE, - NULL, - NULL); + &KeyName, + OBJ_CASE_INSENSITIVE, + NULL, + NULL); Status = NtOpenKey (&KeyHandle, - KEY_READ, - &ObjectAttributes); + KEY_READ, + &ObjectAttributes); if (!NT_SUCCESS(Status)) { DPRINT ("NtOpenKey() failed (Status %lx)\n", Status); @@ -2991,57 +3003,57 @@ LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey, KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + 32; KeyInfo = RtlAllocateHeap (RtlGetProcessHeap(), - HEAP_ZERO_MEMORY, - KeyInfoSize); + HEAP_ZERO_MEMORY, + KeyInfoSize); RtlInitUnicodeString (&ValueNameString, - (PWSTR)ValueName); + (PWSTR)ValueName); Status = NtQueryValueKey (KeyHandle, - &ValueNameString, - KeyValuePartialInformation, - KeyInfo, - KeyInfoSize, - &ResultSize); + &ValueNameString, + KeyValuePartialInformation, + KeyInfo, + KeyInfoSize, + &ResultSize); if (Status == STATUS_BUFFER_OVERFLOW) { KeyInfoSize = sizeof(KEY_VALUE_PARTIAL_INFORMATION) + KeyInfo->DataLength; RtlFreeHeap (RtlGetProcessHeap(), - 0, - KeyInfo); + 0, + KeyInfo); KeyInfo = RtlAllocateHeap (RtlGetProcessHeap(), - HEAP_ZERO_MEMORY, - KeyInfoSize); + HEAP_ZERO_MEMORY, + KeyInfoSize); if (KeyInfo == NULL) - { - NtClose (KeyHandle); - return Status; - } + { + NtClose (KeyHandle); + return Status; + } Status = NtQueryValueKey (KeyHandle, - &ValueNameString, - KeyValuePartialInformation, - KeyInfo, - KeyInfoSize, - &ResultSize); + &ValueNameString, + KeyValuePartialInformation, + KeyInfo, + KeyInfoSize, + &ResultSize); } NtClose (KeyHandle); if (!NT_SUCCESS(Status)) { if (KeyInfo != NULL) - { - RtlFreeHeap (RtlGetProcessHeap(), - 0, - KeyInfo); - } + { + RtlFreeHeap (RtlGetProcessHeap(), + 0, + KeyInfo); + } return Status; } if (KeyInfo->Type != Type) { RtlFreeHeap (RtlGetProcessHeap(), - 0, - KeyInfo); + 0, + KeyInfo); return STATUS_OBJECT_TYPE_MISMATCH; } @@ -3055,12 +3067,12 @@ LdrQueryImageFileExecutionOptions (IN PUNICODE_STRING SubKey, ResultSize = KeyInfo->DataLength; } RtlCopyMemory (Buffer, - &KeyInfo->Data, - ResultSize); + &KeyInfo->Data, + ResultSize); RtlFreeHeap (RtlGetProcessHeap(), - 0, - KeyInfo); + 0, + KeyInfo); if (ReturnedLength != NULL) {