* halfplement reading console properties

* thnx to Alex_Ionescu for advice on how get HKCU path

svn path=/trunk/; revision=22922
This commit is contained in:
Johannes Anderwald 2006-07-08 16:00:08 +00:00
parent 386a355fb8
commit 56fd6ce005
3 changed files with 172 additions and 2 deletions

View file

@ -53,6 +53,158 @@ GuiConsoleGetDataPointers(HWND hWnd, PCSRSS_CONSOLE *Console, PGUI_CONSOLE_DATA
*GuiData = (NULL == *Console ? NULL : (*Console)->PrivateData);
}
static BOOL FASTCALL
GuiConsoleOpenUserRegistryPathPerProcessId(DWORD ProcessId, PHANDLE hProcHandle, PHKEY hResult, REGSAM samDesired)
{
HANDLE hProcessToken = NULL;
HANDLE hProcess;
BYTE Buffer[256];
DWORD Length = 0;
UNICODE_STRING SidName;
LONG res;
PTOKEN_USER TokUser;
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ | READ_CONTROL, FALSE, ProcessId);
if (!hProcess)
{
DPRINT("Error: OpenProcess failed(0x%x)\n", GetLastError());
return FALSE;
}
if (!OpenProcessToken(hProcess, TOKEN_QUERY, &hProcessToken))
{
DPRINT("Error: OpenProcessToken failed(0x%x)\n", GetLastError());
CloseHandle(hProcess);
return FALSE;
}
if (!GetTokenInformation(hProcessToken, TokenUser, (PVOID)Buffer, sizeof(Buffer), &Length))
{
DPRINT("Error: GetTokenInformation failed(0x%x)\n",GetLastError());
CloseHandle(hProcess);
CloseHandle(hProcessToken);
return FALSE;
}
TokUser = ((PTOKEN_USER)Buffer)->User.Sid;
if (!NT_SUCCESS(RtlConvertSidToUnicodeString(&SidName, TokUser, TRUE)))
{
DPRINT("Error: RtlConvertSidToUnicodeString failed(0x%x)\n", GetLastError());
return FALSE;
}
res = RegOpenKeyExW(HKEY_USERS, SidName.Buffer, 0, samDesired, hResult);
RtlFreeUnicodeString(&SidName);
CloseHandle(hProcessToken);
if (hProcHandle)
*hProcHandle = hProcess;
else
CloseHandle(hProcess);
if (res != ERROR_SUCCESS)
return FALSE;
else
return TRUE;
}
static BOOL FASTCALL
GuiConsoleOpenUserSettings(HWND hWnd, DWORD ProcessId, PHKEY hSubKey, REGSAM samDesired)
{
WCHAR szProcessName[MAX_PATH];
WCHAR szBuffer[MAX_PATH];
UINT fLength, wLength;
DWORD dwBitmask, dwLength;
WCHAR CurDrive[] = { 'A',':', 0 };
HANDLE hProcess;
HKEY hKey;
WCHAR * ptr, *res;
static const WCHAR szSystemRoot[] = { '%','S','y','s','t','e','m','R','o','o','t','%', 0 };
/*
* console properties are stored under
* HKCU\Console\*
*
* There are 3 ways to store console properties
*
* 1. use console title as subkey name
* i.e. cmd.exe
*
* 2. use application name as subkey name
*
* 3. use unexpanded path to console application.
* i.e. %SystemRoot%_system32_cmd.exe
*/
if (!GuiConsoleOpenUserRegistryPathPerProcessId(ProcessId, &hProcess, &hKey, samDesired))
return FALSE;
fLength = GetProcessImageFileNameW(hProcess, szProcessName, MAX_PATH);
CloseHandle(hProcess);
if (!fLength)
{
DPRINT1("GetProcessImageFileNameW failed(0x%x)ProcessId %d\n", GetLastError(),hProcess);
return FALSE;
}
ptr = wcsrchr(szProcessName, L'\\');
swprintf(szBuffer, L"Console%s",ptr);
if (RegOpenKeyExW(hKey, szBuffer, 0, samDesired, hSubKey) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
return TRUE;
}
dwBitmask = GetLogicalDrives();
while(dwBitmask)
{
if (dwBitmask & 0x1)
{
dwLength = QueryDosDeviceW(CurDrive, szBuffer, MAX_PATH);
if (dwLength)
{
if (!memcmp(szBuffer, szProcessName, (dwLength-2)*sizeof(WCHAR)))
{
wcscpy(szBuffer, CurDrive);
wcscat(&szBuffer[(sizeof(CurDrive)/sizeof(WCHAR))-1], &szProcessName[dwLength-2]);
break;
}
}
}
dwBitmask = (dwBitmask >> 1);
CurDrive[0]++;
}
wLength = GetWindowsDirectoryW(szProcessName, MAX_PATH);
if (!wcsncmp(szProcessName, szBuffer, wLength))
{
wcscpy(szProcessName, szSystemRoot);
wcscpy(&szProcessName[(sizeof(szSystemRoot) / sizeof(WCHAR))-1], &szBuffer[wLength]);
ptr = res = szProcessName;
}
else
{
ptr = res = szBuffer;
}
while((ptr = wcschr(szProcessName, L'\\')))
ptr[0] = L'_';
if (RegOpenKeyExW(hKey, res, 0, samDesired, hSubKey) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
return TRUE;
}
RegCloseKey(hKey);
return FALSE;
}
static BOOL FASTCALL
GuiConsoleHandleNcCreate(HWND hWnd, CREATESTRUCTW *Create)
{
@ -62,7 +214,20 @@ GuiConsoleHandleNcCreate(HWND hWnd, CREATESTRUCTW *Create)
HDC Dc;
HFONT OldFont;
TEXTMETRICW Metrics;
PCSRSS_PROCESS_DATA ProcessData;
HKEY hKey;
if (Console->ProcessList.Flink != &Console->ProcessList)
{
ProcessData = CONTAINING_RECORD(Console->ProcessList.Flink, CSRSS_PROCESS_DATA, ProcessEntry);
if (GuiConsoleOpenUserSettings(hWnd, PtrToUlong(ProcessData->ProcessId), &hKey, KEY_READ))
{
// TODO
// read registry settings
// and store them in GuiData struct
RegCloseKey(hKey);
}
}
GuiData = HeapAlloc(Win32CsrApiHeap, HEAP_ZERO_MEMORY,
sizeof(GUI_CONSOLE_DATA) +
(Console->Size.X + 1) * sizeof(WCHAR));

View file

@ -4,6 +4,9 @@
#define NTOS_MODE_USER
#include <ndk/ntndk.h>
#include <accctrl.h>
#include <psapi.h>
/* Our own BLUE.SYS Driver for Console Output */
#include <blue/ntddblue.h>

View file

@ -6,12 +6,14 @@
<include base="ReactOS">include/reactos/drivers</include>
<define name="_DISABLE_TIDENTS" />
<define name="__USE_W32API" />
<define name="_WIN32_WINNT">0x0500</define>
<define name="_WIN32_WINNT">0x0501</define>
<library>ntdll</library>
<library>kernel32</library>
<library>user32</library>
<library>gdi32</library>
<pch>w32csr.h</pch>
<library>advapi32</library>
<library>psapi</library>
<pch>w32csr.h</pch>
<file>conio.c</file>
<file>desktopbg.c</file>
<file>dllmain.c</file>