示例#1
0
void GenericWindow::AttachHandle (HWND hWnd) {
	// handle can only be attached if window is not yet created
	WINGUI_ASSERT ( m_hWnd == NULL );

	// verify if the window is valid
	WINGUI_ASSERT ( ::IsWindow(hWnd) );

	m_hWnd = hWnd;
	
	MapHandle ();
}
示例#2
0
文件: XMemmap.cpp 项目: fre2003/l3220
BOOL CXMemMapFile::MapMemory(LPCTSTR lpszName, 
							 DWORD dwBytes, 
							 BOOL bReadOnly, 
							 LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
	m_dwLength = dwBytes;
	m_bReadOnly = bReadOnly;
	m_bAppendNull = FALSE;
	CreateMappingName(lpszName, 
		m_szMappingName, sizeof(m_szMappingName)/sizeof(TCHAR), TRUE);
	TRACE(_T("m_szMappingName=%s\n"), m_szMappingName);
	return MapHandle((HANDLE)0xFFFFFFFF, lpSecurityAttributes, 0);
}
示例#3
0
文件: XMemmap.cpp 项目: fre2003/l3220
BOOL CXMemMapFile::MapFile(LPCTSTR lpszFilename, 
						   BOOL bReadOnly, 
						   DWORD dwShareMode, 
						   BOOL bAppendNull, 
						   BOOL bNamed, 
						   BOOL bGrowable, 
						   DWORD dwStartOffset, 
						   DWORD dwNumberOfBytesToMap, 
						   LPSECURITY_ATTRIBUTES lpSecurityAttributes)
{
	//Work out the file access flags
	m_bReadOnly = bReadOnly;
	DWORD dwDesiredFileAccess = GENERIC_READ;
	if (!m_bReadOnly)
		dwDesiredFileAccess |= GENERIC_WRITE;

	//store away the append Null flag
	m_bAppendNull = bAppendNull;

	//Open the real file on the file system
	m_hFile = CreateFile(lpszFilename, dwDesiredFileAccess, dwShareMode, NULL, 
	OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (m_hFile == INVALID_HANDLE_VALUE)
	{
		TRACE(_T("Failed in call to CreateFile, GetLastError returned %d\n"), 
			GetLastError());
		UnMap();
		return FALSE;
	}

	// Make the file sparse, if requested to make the memory mapped file growable
	if (bGrowable)
	{
		DWORD dw;
		if (!::DeviceIoControl(m_hFile, FSCTL_SET_SPARSE, NULL, 0, NULL, 0, &dw, NULL))
		{
			TRACE(_T("Failed in call to make file sparse, You need Windows 2000 ")
				  _T("and an NTFS 5 volume for this !!, GetLastError returned %d\n"), 
				  GetLastError());
			UnMap();
			return FALSE;
		}
	}

	// Get the size of the file we are mapping
	DWORD dwFileSizeHigh=0;
	DWORD dwLength = GetFileSize(m_hFile, &dwFileSizeHigh);
	if (dwLength == 0xFFFFFFFF)
	{
		//There was an error calling GetFileSize
		TRACE(_T("Failed in call to GetFileSize, GetLastError returned %d\n"), 
			GetLastError());
		UnMap();
		return FALSE;
	}

	//Fail if file is greater than 4GB in size
	if (dwFileSizeHigh)
	{
		//There was an error calling GetFileSize
		TRACE(_T("File size is greater than 4GB, Memory mapping this file ")
			  _T("size will not work until CXMemMapFile supports Win64 !!\n"));
		UnMap();
		return FALSE;
	}

	//Fail if file is 0 length in size, calling CreateFileMapping on a 
	//zero length file on 95/98 can cause problems
	if (dwFileSizeHigh == 0 && dwLength == 0)
	{
		TRACE(_T("File size is 0, not attempting to memory map the file\n"));
		UnMap();
		return FALSE;
	}

	//Now work out the size of the mapping we will be performing
	if (dwNumberOfBytesToMap)
		m_dwLength = dwNumberOfBytesToMap;
	else
		m_dwLength = dwLength;

	//Do the actual mapping
	CreateMappingName(lpszFilename, 
		m_szMappingName, sizeof(m_szMappingName)/sizeof(TCHAR), bNamed);

	return MapHandle(m_hFile, lpSecurityAttributes, dwStartOffset);
}
示例#4
0
文件: handle.c 项目: shuowen/OpenNT
NTSTATUS
AllocateConsole(
    IN HANDLE ConsoleHandle,
    IN LPWSTR Title,
    IN USHORT TitleLength,
    IN HANDLE ClientProcessHandle,
    OUT PHANDLE StdIn,
    OUT PHANDLE StdOut,
    OUT PHANDLE StdErr,
    OUT PCONSOLE_PER_PROCESS_DATA ProcessData,
    IN OUT PCONSOLE_INFO ConsoleInfo,
    IN BOOLEAN WindowVisible,
    IN DWORD dwConsoleThreadId,
    IN HDESK Desktop
)

/*++

Routine Description:

    This routine allocates and initialized a console and its associated
    data - input buffer and screen buffer.

Arguments:

    ConsoleHandle - Handle of console to allocate.

    dwWindowSize - Initial size of screen buffer window, in rows and columns.

    nFont - Initial number of font text is displayed in.

    dwScreenBufferSize - Initial size of screen buffer, in rows and columns.

    nInputBufferSize - Initial size of input buffer, in events.

    dwWindowFlags -

    StdIn - On return, contains handle to stdin.

    StdOut - On return, contains handle to stdout.

    StdErr - On return, contains handle to stderr.

    ProcessData - On return, contains the initialized per-process data.

Return Value:

Note:

    The console handle table lock must be held when calling this routine.

--*/

{
    PCONSOLE_INFORMATION Console;
    NTSTATUS Status;
    BOOL Success;

    //
    // allocate console data
    //

    Console = (PCONSOLE_INFORMATION)HeapAlloc(pConHeap, MAKE_TAG( CONSOLE_TAG ) | HEAP_ZERO_MEMORY,
              sizeof(CONSOLE_INFORMATION));
    if (Console == NULL) {
        return STATUS_NO_MEMORY;
    }
    ConsoleHandles[IndexFromHandle(ConsoleHandle)] = Console;

    Console->Flags = WindowVisible ? 0 : CONSOLE_NO_WINDOW;
    Console->hIcon = ConsoleInfo->hIcon;
    Console->iIconId = ConsoleInfo->iIconId;
    Console->dwHotKey = ConsoleInfo->dwHotKey;
    Console->CP = OEMCP;
    Console->OutputCP = ConsoleOutputCP;
    Console->ReserveKeys = CONSOLE_NOSHORTCUTKEY;
    Console->ConsoleHandle = ConsoleHandle;
    Console->bIconInit = TRUE;
    Console->VerticalClientToWindow = VerticalClientToWindow;
    Console->HorizontalClientToWindow = HorizontalClientToWindow;

    //
    // must wait for window to be destroyed or client impersonation won't
    // work.
    //

    Status = NtDuplicateObject(NtCurrentProcess(),
                               CONSOLE_CLIENTTHREADHANDLE(CSR_SERVER_QUERYCLIENTTHREAD()),
                               NtCurrentProcess(),
                               &Console->ClientThreadHandle,
                               0,
                               FALSE,
                               DUPLICATE_SAME_ACCESS
                              );
    if (!NT_SUCCESS(Status)) {
        goto ErrorExit5;
    }

#if DBG
    //
    // Make sure the handle isn't protected so we can close it later
    //
    UnProtectHandle(Console->ClientThreadHandle);
#endif // DBG

    InitializeListHead(&Console->OutputQueue);
    InitializeListHead(&Console->ProcessHandleList);
    InitializeListHead(&Console->ExeAliasList);

    Status = NtCreateEvent(&Console->InitEvents[INITIALIZATION_SUCCEEDED],
                           EVENT_ALL_ACCESS, NULL, NotificationEvent, FALSE);
    if (!NT_SUCCESS(Status)) {
        goto ErrorExit4a;
    }
    Status = NtCreateEvent(&Console->InitEvents[INITIALIZATION_FAILED],
                           EVENT_ALL_ACCESS, NULL, NotificationEvent, FALSE);
    if (!NT_SUCCESS(Status)) {
        goto ErrorExit4;
    }
    Status = RtlInitializeCriticalSection(&Console->ConsoleLock);
    if (!NT_SUCCESS(Status)) {
        goto ErrorExit3a;
    }
    InitializeConsoleCommandData(Console);

    //
    // initialize input buffer
    //

    Status = CreateInputBuffer(ConsoleInfo->nInputBufferSize,
                               &Console->InputBuffer);
    if (!NT_SUCCESS(Status)) {
        goto ErrorExit3;
    }

    Console->Title = (PWCHAR)HeapAlloc(pConHeap,MAKE_TAG( TITLE_TAG ),TitleLength+sizeof(WCHAR));
    if (Console->Title == NULL) {
        Status = STATUS_NO_MEMORY;
        goto ErrorExit2;
    }
    RtlCopyMemory(Console->Title,Title,TitleLength);
    Console->Title[TitleLength/sizeof(WCHAR)] = (WCHAR)0;   // NULL terminate
    Console->TitleLength = TitleLength;

    Console->OriginalTitle = (PWCHAR)HeapAlloc(pConHeap,MAKE_TAG( TITLE_TAG ),TitleLength+sizeof(WCHAR));
    if (Console->OriginalTitle == NULL) {
        Status = STATUS_NO_MEMORY;
        goto ErrorExit1;
    }
    RtlCopyMemory(Console->OriginalTitle,Title,TitleLength);
    Console->OriginalTitle[TitleLength/sizeof(WCHAR)] = (WCHAR)0;   // NULL terminate
    Console->OriginalTitleLength = TitleLength;

    Status = NtCreateEvent(&Console->TerminationEvent,
                           EVENT_ALL_ACCESS, NULL, NotificationEvent, FALSE);
    if (!NT_SUCCESS(Status)) {
        goto ErrorExit1a;
    }

    //
    // initialize screen buffer. we don't call OpenConsole to do this
    // because we need to specify the font, windowsize, etc.
    //

    Status = DoCreateScreenBuffer(Console,
                                  ConsoleInfo,
                                  Console->Title);
    if (!NT_SUCCESS(Status)) {
        goto ErrorExit1b;
    }


    Console->CurrentScreenBuffer = Console->ScreenBuffers;
    Status = InitializeIoHandleTable(Console,
                                     ProcessData,
                                     StdIn,
                                     StdOut,
                                     StdErr
                                    );
    if (!NT_SUCCESS(Status)) {
        goto ErrorExit0;
    }

    //
    // map event handles
    //

    if (!MapHandle(ClientProcessHandle,
                   Console->InitEvents[INITIALIZATION_SUCCEEDED],
                   &ConsoleInfo->InitEvents[INITIALIZATION_SUCCEEDED]
                  )) {
        Status = STATUS_NO_MEMORY;
        goto ErrorExit0;
    }
    if (!MapHandle(ClientProcessHandle,
                   Console->InitEvents[INITIALIZATION_FAILED],
                   &ConsoleInfo->InitEvents[INITIALIZATION_FAILED]
                  )) {
        Status = STATUS_NO_MEMORY;
        goto ErrorExit0;
    }
    if (!MapHandle(ClientProcessHandle,
                   Console->InputBuffer.InputWaitEvent,
                   &ConsoleInfo->InputWaitHandle
                  )) {
        Status = STATUS_NO_MEMORY;
        goto ErrorExit0;
    }

    Success = PostThreadMessage(dwConsoleThreadId,
                                CM_CREATE_CONSOLE_WINDOW,
                                (DWORD)Console,
                                (LONG)ClientProcessHandle
                               );
    if (!Success) {
        KdPrint(("CONSRV: PostThreadMessage failed %d\n",GetLastError()));
        Status = STATUS_UNSUCCESSFUL;
        goto ErrorExit0;
    }

    return STATUS_SUCCESS;

ErrorExit0:
    Console->ScreenBuffers->RefCount = 0;
    FreeScreenBuffer(Console->ScreenBuffers);
ErrorExit1b:
    NtClose(Console->TerminationEvent);
ErrorExit1a:
    HeapFree(pConHeap,0,Console->OriginalTitle);
ErrorExit1:
    HeapFree(pConHeap,0,Console->Title);
ErrorExit2:
    Console->InputBuffer.RefCount = 0;
    FreeInputBuffer(&Console->InputBuffer);
ErrorExit3:
    RtlDeleteCriticalSection(&Console->ConsoleLock);

ErrorExit3a:
    NtClose(Console->InitEvents[INITIALIZATION_FAILED]);
ErrorExit4:
    NtClose(Console->InitEvents[INITIALIZATION_SUCCEEDED]);
ErrorExit4a:
    NtClose(Console->ClientThreadHandle);
ErrorExit5:
    HeapFree(pConHeap,0,Console);
    return Status;
}