int WINAPI OnStretchDIBits(HDC hdc, int XDest, int YDest, int nDestWidth, int nDestHeight, int XSrc, int YSrc, int nSrcWidth, int nSrcHeight, const VOID *lpBits, const BITMAPINFO *lpBitsInfo, UINT iUsage, DWORD dwRop) { //typedef int (WINAPI* OnStretchDIBits_t)(HDC hdc, int XDest, int YDest, int nDestWidth, int nDestHeight, int XSrc, int YSrc, int nSrcWidth, int nSrcHeight, const VOID *lpBits, const BITMAPINFO *lpBitsInfo, UINT iUsage, DWORD dwRop); ORIGINAL_EX(StretchDIBits); int iRc = 0; if (F(StretchDIBits)) iRc = F(StretchDIBits)(hdc, XDest, YDest, nDestWidth, nDestHeight, XSrc, YSrc, nSrcWidth, nSrcHeight, lpBits, lpBitsInfo, iUsage, dwRop); // Если рисуют _прямо_ на канвасе ConEmu if (iRc != (int)GDI_ERROR && hdc && hdc == ghTempHDC) { // Уведомить GUI, что у него прямо на канвасе кто-то что-то нарисовал :) CESERVER_REQ* pIn = ExecuteNewCmd(CECMD_LOCKDC, sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_LOCKDC)); if (pIn) { pIn->LockDc.hDcWnd = ghConEmuWndDC; // На всякий случай pIn->LockDc.bLock = TRUE; pIn->LockDc.Rect.left = XDest; pIn->LockDc.Rect.top = YDest; pIn->LockDc.Rect.right = XDest+nDestWidth-1; pIn->LockDc.Rect.bottom = YDest+nDestHeight-1; CESERVER_REQ* pOut = ExecuteGuiCmd(ghConWnd, pIn, ghConWnd); if (pOut) ExecuteFreeResult(pOut); ExecuteFreeResult(pIn); } } return iRc; }
void CorrectGuiChildRect(DWORD anStyle, DWORD anStyleEx, RECT& rcGui) { RECT rcShift = {}; if ((anStyle != gGuiClientStyles.nStyle) || (anStyleEx != gGuiClientStyles.nStyleEx)) { DWORD nSize = sizeof(CESERVER_REQ_HDR)+sizeof(GuiStylesAndShifts); CESERVER_REQ *pIn = ExecuteNewCmd(CECMD_GUICLIENTSHIFT, nSize); if (pIn) { pIn->GuiAppShifts.nStyle = anStyle; pIn->GuiAppShifts.nStyleEx = anStyleEx; wchar_t szOurExe[MAX_PATH*2] = L""; GetModuleFileName(NULL, szOurExe, countof(szOurExe)); lstrcpyn(pIn->GuiAppShifts.szExeName, PointToName(szOurExe), countof(pIn->GuiAppShifts.szExeName)); wchar_t szGuiPipeName[128]; msprintf(szGuiPipeName, countof(szGuiPipeName), CEGUIPIPENAME, L".", (DWORD)ghConEmuWnd); CESERVER_REQ* pOut = ExecuteCmd(szGuiPipeName, pIn, 10000, NULL); if (pOut) { gGuiClientStyles = pOut->GuiAppShifts; ExecuteFreeResult(pOut); } ExecuteFreeResult(pIn); } } rcShift = gGuiClientStyles.Shifts; rcGui.left += rcShift.left; rcGui.top += rcShift.top; rcGui.right += rcShift.right; rcGui.bottom += rcShift.bottom; }
// Undocumented function BOOL WINAPI OnSetConsoleKeyShortcuts(BOOL bSet, BYTE bReserveKeys, LPVOID p1, DWORD n1) { //typedef BOOL (WINAPI* OnSetConsoleKeyShortcuts_t)(BOOL,BYTE,LPVOID,DWORD); ORIGINALFASTEX(SetConsoleKeyShortcuts,NULL); BOOL lbRc = FALSE; if (F(SetConsoleKeyShortcuts)) lbRc = F(SetConsoleKeyShortcuts)(bSet, bReserveKeys, p1, n1); if (ghConEmuWnd && IsWindow(ghConEmuWnd)) { DWORD nLastErr = GetLastError(); DWORD nSize = sizeof(CESERVER_REQ_HDR)+sizeof(BYTE)*2; CESERVER_REQ *pIn = ExecuteNewCmd(CECMD_KEYSHORTCUTS, nSize); if (pIn) { pIn->Data[0] = bSet; pIn->Data[1] = bReserveKeys; wchar_t szGuiPipeName[128]; msprintf(szGuiPipeName, countof(szGuiPipeName), CEGUIPIPENAME, L".", LODWORD(ghConWnd)); CESERVER_REQ* pOut = ExecuteCmd(szGuiPipeName, pIn, 1000, NULL); if (pOut) ExecuteFreeResult(pOut); ExecuteFreeResult(pIn); } SetLastError(nLastErr); } return lbRc; }
BOOL WINAPI OnBitBlt(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc, DWORD dwRop) { //typedef int (WINAPI* OnBitBlt_t)(HDC hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, HDC hdcSrc, int nXSrc, int nYSrc, DWORD dwRop); ORIGINAL_EX(BitBlt); BOOL bRc = FALSE; if (F(BitBlt)) bRc = F(BitBlt)(hdcDest, nXDest, nYDest, nWidth, nHeight, hdcSrc, nXSrc, nYSrc, dwRop); // Если рисуют _прямо_ на канвасе ConEmu if (bRc && hdcDest && hdcDest == ghTempHDC) { // Уведомить GUI, что у него прямо на канвасе кто-то что-то нарисовал :) CESERVER_REQ* pIn = ExecuteNewCmd(CECMD_LOCKDC, sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_LOCKDC)); if (pIn) { pIn->LockDc.hDcWnd = ghConEmuWndDC; // На всякий случай pIn->LockDc.bLock = TRUE; pIn->LockDc.Rect.left = nXDest; pIn->LockDc.Rect.top = nYDest; pIn->LockDc.Rect.right = nXDest+nWidth-1; pIn->LockDc.Rect.bottom = nYDest+nHeight-1; CESERVER_REQ* pOut = ExecuteGuiCmd(ghConWnd, pIn, ghConWnd); if (pOut) ExecuteFreeResult(pOut); ExecuteFreeResult(pIn); } } return bRc; }
void PatchGuiMessage(bool bReceived, HWND& hWnd, UINT& Msg, WPARAM& wParam, LPARAM& lParam) { if (!ghAttachGuiClient) return; switch (Msg) { case WM_MOUSEACTIVATE: if (wParam == (WPARAM)ghConEmuWnd) { wParam = (WPARAM)ghAttachGuiClient; } break; case WM_LBUTTONDOWN: if (bReceived && ghConEmuWnd && ghConWnd) { HWND hActiveCon = (HWND)GetWindowLongPtr(ghConEmuWnd, GWLP_USERDATA); if (hActiveCon != ghConWnd) { CESERVER_REQ* pIn = ExecuteNewCmd(CECMD_ACTIVATECON, sizeof(CESERVER_REQ_HDR) + sizeof(CESERVER_REQ_ACTIVATECONSOLE)); if (pIn) { pIn->ActivateCon.hConWnd = ghConWnd; CESERVER_REQ* pOut = ExecuteGuiCmd(ghConWnd, pIn, ghConWnd); ExecuteFreeResult(pIn); ExecuteFreeResult(pOut); } } #if 0 // Если окно ChildGui активируется кликом - то ConEmu (holder) // может остаться "под" предыдущим окном бывшим в фокусе... // Победить пока не получается. DWORD nTID = 0, nPID = 0; HWND hFore = GetForegroundWindow(); if (hFore && ((nTID = GetWindowThreadProcessId(hFore, &nPID)) != 0)) { if ((nPID != GetCurrentProcessId()) && (nPID != gnGuiPID)) { SetForegroundWindow(ghConEmuWnd); } } #endif } break; #ifdef _DEBUG case WM_DESTROY: if (hWnd == ghAttachGuiClient) { int iDbg = -1; } break; #endif } }
void SendCurrentDirectory(HWND hConWnd, LPCWSTR asDirectory, LPCWSTR asPassiveDirectory /*= NULL*/) { CESERVER_REQ* pIn = NULL; DWORD cbSize = 0; if (!AllocateSendCurrentDirectory(pIn, cbSize, asDirectory, asPassiveDirectory)) return; CESERVER_REQ* pOut = ExecuteGuiCmd(hConWnd, pIn, hConWnd, TRUE); ExecuteFreeResult(pOut); ExecuteFreeResult(pIn); }
BOOL WINAPI OnStretchBlt(HDC hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, DWORD dwRop) { //typedef int (WINAPI* OnStretchBlt_t)(HDC hdcDest, int nXOriginDest, int nYOriginDest, int nWidthDest, int nHeightDest, HDC hdcSrc, int nXOriginSrc, int nYOriginSrc, int nWidthSrc, int nHeightSrc, DWORD dwRop); ORIGINAL_EX(StretchBlt); BOOL bRc = FALSE; //#ifdef _DEBUG //HWND h = WindowFromDC(hdcDest); //#endif // Если рисуют _прямо_ на канвасе ConEmu if (/*bRc &&*/ hdcDest && hdcDest == ghTempHDC) { if ( (!StretchBltBatch.bottom && !StretchBltBatch.top) || (nYOriginDest <= StretchBltBatch.top) || (nXOriginDest != StretchBltBatch.left) || (StretchBltBatch.right != (nXOriginDest+nWidthDest-1)) || (StretchBltBatch.bottom != (nYOriginDest-1)) ) { // Сброс батча StretchBltBatch.left = nXOriginDest; StretchBltBatch.top = nYOriginDest; StretchBltBatch.right = nXOriginDest+nWidthDest-1; StretchBltBatch.bottom = nYOriginDest+nHeightDest-1; } else { StretchBltBatch.bottom = nYOriginDest+nHeightDest-1; } // Уведомить GUI, что у него прямо на канвасе кто-то что-то нарисовал :) CESERVER_REQ* pIn = ExecuteNewCmd(CECMD_LOCKDC, sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_LOCKDC)); if (pIn) { pIn->LockDc.hDcWnd = ghConEmuWndDC; // На всякий случай pIn->LockDc.bLock = TRUE; pIn->LockDc.Rect = StretchBltBatch; CESERVER_REQ* pOut = ExecuteGuiCmd(ghConWnd, pIn, ghConWnd); if (pOut) ExecuteFreeResult(pOut); ExecuteFreeResult(pIn); } } if (F(StretchBlt)) bRc = F(StretchBlt)(hdcDest, nXOriginDest, nYOriginDest, nWidthDest, nHeightDest, hdcSrc, nXOriginSrc, nYOriginSrc, nWidthSrc, nHeightSrc, dwRop); return bRc; }
// When _st_ is 0: remove progress. // When _st_ is 1: set progress value to _pr_ (number, 0-100). // When _st_ is 2: set error state in progress on Windows 7 taskbar void GuiSetProgress(WORD st, WORD pr) { CESERVER_REQ* pIn = ExecuteNewCmd(CECMD_SETPROGRESS, sizeof(CESERVER_REQ_HDR)+sizeof(WORD)*2); if (pIn) { pIn->wData[0] = st; pIn->wData[1] = pr; CESERVER_REQ* pOut = ExecuteGuiCmd(ghConWnd, pIn, ghConWnd); ExecuteFreeResult(pIn); ExecuteFreeResult(pOut); } }
BOOL GuiSetForeground(HWND hWnd) { BOOL lbRc = FALSE; if (ghConEmuWndDC) { CESERVER_REQ *pIn = (CESERVER_REQ*)malloc(sizeof(*pIn)), *pOut; if (pIn) { ExecutePrepareCmd(pIn, CECMD_SETFOREGROUND, sizeof(CESERVER_REQ_HDR)+sizeof(u64)); //-V119 DWORD nConEmuPID = ASFW_ANY; GetWindowThreadProcessId(ghConEmuWndDC, &nConEmuPID); AllowSetForegroundWindow(nConEmuPID); pIn->qwData[0] = (u64)hWnd; HWND hConWnd = GetRealConsoleWindow(); pOut = ExecuteGuiCmd(hConWnd, pIn, hConWnd); if (pOut) { if (pOut->hdr.cbSize == (sizeof(CESERVER_REQ_HDR)+sizeof(DWORD))) lbRc = pOut->dwData[0]; ExecuteFreeResult(pOut); } free(pIn); } } return lbRc; }
int WINAPI RegisterPanelView(PanelViewInit *ppvi) { if (!ppvi) { _ASSERTE(ppvi != NULL); return -2; } if (ppvi->cbSize != sizeof(PanelViewInit)) { _ASSERTE(ppvi->cbSize == sizeof(PanelViewInit)); return -2; } PanelViewRegInfo *pp = (ppvi->bLeftPanel) ? &gPanelRegLeft : &gPanelRegRight; if (ppvi->bRegister) { pp->pfnPeekPreCall = ppvi->pfnPeekPreCall.f; pp->pfnPeekPostCall = ppvi->pfnPeekPostCall.f; pp->pfnReadPreCall = ppvi->pfnReadPreCall.f; pp->pfnReadPostCall = ppvi->pfnReadPostCall.f; pp->pfnWriteCall = ppvi->pfnWriteCall.f; } else { pp->pfnPeekPreCall = pp->pfnPeekPostCall = pp->pfnReadPreCall = pp->pfnReadPostCall = NULL; pp->pfnWriteCall = NULL; } pp->bRegister = ppvi->bRegister; CESERVER_REQ In; int nSize = sizeof(CESERVER_REQ_HDR) + sizeof(In.PVI); ExecutePrepareCmd(&In, CECMD_REGPANELVIEW, nSize); In.PVI = *ppvi; CESERVER_REQ* pOut = ExecuteGuiCmd(FarHwnd, &In, FarHwnd); if (!pOut) { pp->pfnPeekPreCall = pp->pfnPeekPostCall = pp->pfnReadPreCall = pp->pfnReadPostCall = NULL; pp->pfnWriteCall = NULL; pp->bRegister = FALSE; return -3; } *ppvi = pOut->PVI; ExecuteFreeResult(pOut); if (ppvi->cbSize == 0) { pp->pfnPeekPreCall = pp->pfnPeekPostCall = pp->pfnReadPreCall = pp->pfnReadPostCall = NULL; pp->pfnWriteCall = NULL; pp->bRegister = FALSE; return -1; } return 0; }
DWORD WINAPI OnGetConsoleAliasesW(LPWSTR AliasBuffer, DWORD AliasBufferLength, LPWSTR ExeName) { //typedef DWORD (WINAPI* OnGetConsoleAliasesW_t)(LPWSTR AliasBuffer, DWORD AliasBufferLength, LPWSTR ExeName); ORIGINALFAST(GetConsoleAliasesW); DWORD nError = 0; DWORD nRc = F(GetConsoleAliasesW)(AliasBuffer,AliasBufferLength,ExeName); if (!nRc) { nError = GetLastError(); // финт ушами if (nError == ERROR_NOT_ENOUGH_MEMORY) // && gdwServerPID) { DWORD nServerPID = gnServerPID; HWND hConWnd = GetRealConsoleWindow(); _ASSERTE(hConWnd == ghConWnd); //MFileMapping<CESERVER_CONSOLE_MAPPING_HDR> ConInfo; //ConInfo.InitName(CECONMAPNAME, (DWORD)hConWnd); //-V205 //CESERVER_CONSOLE_MAPPING_HDR *pInfo = ConInfo.Open(); //if (pInfo // && (pInfo->cbSize >= sizeof(CESERVER_CONSOLE_MAPPING_HDR)) // //&& (pInfo->nProtocolVersion == CESERVER_REQ_VER) // ) //{ // nServerPID = pInfo->nServerPID; // ConInfo.CloseMap(); //} if (nServerPID) { CESERVER_REQ_HDR In; ExecutePrepareCmd(&In, CECMD_GETALIASES, sizeof(CESERVER_REQ_HDR)); CESERVER_REQ* pOut = ExecuteSrvCmd(nServerPID/*gdwServerPID*/, (CESERVER_REQ*)&In, hConWnd); if (pOut) { size_t nData = min(AliasBufferLength,(pOut->hdr.cbSize-sizeof(pOut->hdr))); if (nData) { memmove(AliasBuffer, pOut->Data, nData); nRc = TRUE; } ExecuteFreeResult(pOut); } } } if (!nRc) SetLastError(nError); // вернуть, вдруг какая функция его поменяла } return nRc; }
void SendStopped() { if (gbNonGuiMode || !gnServerPID) return; CESERVER_REQ *pIn = NULL, *pOut = NULL; size_t nSize = sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_STARTSTOP); pIn = ExecuteNewCmd(CECMD_CMDSTARTSTOP,nSize); if (pIn) { pIn->StartStop.nStarted = sst_AppStop; if (!GetModuleFileName(NULL, pIn->StartStop.sModuleName, countof(pIn->StartStop.sModuleName))) pIn->StartStop.sModuleName[0] = 0; pIn->StartStop.hWnd = ghConWnd; pIn->StartStop.dwPID = gnSelfPID; pIn->StartStop.nSubSystem = gnImageSubsystem; pIn->StartStop.bWasBufferHeight = gbWasBufferHeight; pIn->StartStop.nOtherPID = gnPrevAltServerPID; HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE); // НЕ MyGet..., а то можем заблокироваться... // ghConOut может быть NULL, если ошибка произошла во время разбора аргументов GetConsoleScreenBufferInfo(hOut, &pIn->StartStop.sbi); pIn->StartStop.crMaxSize = MyGetLargestConsoleWindowSize(hOut); pOut = ExecuteSrvCmd(gnServerPID, pIn, ghConWnd, TRUE/*bAsyncNoResult*/); ExecuteFreeResult(pIn); pIn = NULL; if (pOut) { ExecuteFreeResult(pOut); pOut = NULL; } } }
int GuiMessageBox(HWND hConEmuWndRoot, LPCWSTR asText, LPCWSTR asTitle, int anBtns) { int nResult = 0; if (hConEmuWndRoot) { HWND hConWnd = myGetConsoleWindow(); CESERVER_REQ *pIn = (CESERVER_REQ*)malloc(sizeof(*pIn)); ExecutePrepareCmd(pIn, CECMD_ASSERT, sizeof(CESERVER_REQ_HDR)+sizeof(MyAssertInfo)); pIn->AssertInfo.nBtns = anBtns; _wcscpyn_c(pIn->AssertInfo.szTitle, countof(pIn->AssertInfo.szTitle), asTitle, countof(pIn->AssertInfo.szTitle)); //-V501 _wcscpyn_c(pIn->AssertInfo.szDebugInfo, countof(pIn->AssertInfo.szDebugInfo), asText, countof(pIn->AssertInfo.szDebugInfo)); //-V501 wchar_t szGuiPipeName[128]; msprintf(szGuiPipeName, countof(szGuiPipeName), CEGUIPIPENAME, L".", (DWORD)hConEmuWndRoot); //-V205 CESERVER_REQ* pOut = ExecuteCmd(szGuiPipeName, pIn, 1000, hConWnd); free(pIn); if (pOut) { if (pOut->hdr.cbSize > sizeof(CESERVER_REQ_HDR)) { nResult = pOut->dwData[0]; } ExecuteFreeResult(pOut); } } else { //_ASSERTE(hConEmuWndRoot!=NULL); // Избежать статической линковки к user32 HMODULE hUser32 = GetModuleHandle(L"User32.dll"); if (hUser32 == NULL) hUser32 = LoadLibrary(L"User32.dll"); typedef int (WINAPI* MessageBoxW_T)(HWND, LPCWSTR, LPCWSTR, UINT); MessageBoxW_T _MessageBoxW = hUser32 ? (MessageBoxW_T)GetProcAddress(hUser32, "MessageBoxW") : NULL; if (_MessageBoxW) { nResult = _MessageBoxW(NULL, asText, asTitle, MB_SYSTEMMODAL|anBtns); } else { #ifdef _DEBUG _CrtDbgBreak(); #endif } } return nResult; }
// export // Активировать текущую консоль в ConEmu int WINAPI ActivateConsole() { CESERVER_REQ In; int nSize = sizeof(CESERVER_REQ_HDR) + sizeof(In.ActivateCon); ExecutePrepareCmd(&In, CECMD_ACTIVATECON, nSize); In.ActivateCon.hConWnd = FarHwnd; CESERVER_REQ* pOut = ExecuteGuiCmd(FarHwnd, &In, FarHwnd); if (!pOut) { return FALSE; } BOOL lbSucceeded = (pOut->ActivateCon.hConWnd == FarHwnd); ExecuteFreeResult(pOut); return lbSucceeded; }
BOOL ExecuteNewCmd(CESERVER_REQ* &ppCmd, DWORD &pcbCurMaxSize, DWORD nCmd, size_t nSize) { if (!ppCmd || (pcbCurMaxSize < nSize)) { DWORD nErr = GetLastError(); ExecuteFreeResult(ppCmd); ppCmd = ExecuteNewCmd(nCmd, nSize); if (ppCmd != NULL) { // Обмен данными идет и между 32bit & 64bit процессами, размеры __int64 недопустимы _ASSERTE(nSize == (DWORD)nSize); pcbCurMaxSize = (DWORD)nSize; ppCmd->hdr.nLastError = nErr; } } else { ExecutePrepareCmd(ppCmd, nCmd, nSize); } return (ppCmd != NULL); }
void GuiFlashWindow(CEFlashType fType, HWND hWnd, BOOL bInvert, DWORD dwFlags, UINT uCount, DWORD dwTimeout) { if (ghConEmuWndDC) { CESERVER_REQ *pIn = (CESERVER_REQ*)malloc(sizeof(*pIn)), *pOut; if (pIn) { ExecutePrepareCmd(pIn, CECMD_FLASHWINDOW, sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_FLASHWINFO)); //-V119 pIn->Flash.fType = fType; pIn->Flash.hWnd = hWnd; pIn->Flash.bInvert = bInvert; pIn->Flash.dwFlags = dwFlags; pIn->Flash.uCount = uCount; pIn->Flash.dwTimeout = dwTimeout; HWND hConWnd = GetRealConsoleWindow(); pOut = ExecuteGuiCmd(hConWnd, pIn, hConWnd); if (pOut) ExecuteFreeResult(pOut); free(pIn); } } }
void WINAPI PlugServerFree(CESERVER_REQ* pReply, LPARAM lParam) { ExecuteFreeResult(pReply); }
// Helper function: Notification ‘Debug’ setting page in ConEmu void OnPeekReadConsoleInput(char acPeekRead/*'P'/'R'*/, char acUnicode/*'A'/'W'*/, HANDLE hConsoleInput, PINPUT_RECORD lpBuffer, DWORD nRead) { #ifdef TRAP_ON_MOUSE_0x0 if (lpBuffer && nRead) { for (UINT i = 0; i < nRead; i++) { if (lpBuffer[i].EventType == MOUSE_EVENT) { if (lpBuffer[i].Event.MouseEvent.dwMousePosition.X == 0 && lpBuffer[i].Event.MouseEvent.dwMousePosition.Y == 0) { _ASSERTE(!(lpBuffer[i].Event.MouseEvent.dwMousePosition.X == 0 && lpBuffer[i].Event.MouseEvent.dwMousePosition.Y == 0)); } //if (lpBuffer[i].Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED && lpBuffer[i].Event.MouseEvent.dwMousePosition.X != 5) //{ // _ASSERTE(!(lpBuffer[i].Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED && lpBuffer[i].Event.MouseEvent.dwMousePosition.X != 5)); //} } } } #endif DWORD nCurrentTID = GetCurrentThreadId(); gReadConsoleInfo.LastReadConsoleInputTID = nCurrentTID; gReadConsoleInfo.hConsoleInput2 = hConsoleInput; if (nRead) { if (!gbWasSucceededInRead) gbWasSucceededInRead = TRUE; // Сброс кешированных значений GetConsoleScreenBufferInfoCached(NULL, NULL); } if (!gFarMode.bFarHookMode || !gFarMode.bMonitorConsoleInput || !nRead || !lpBuffer) return; //// Пока - только Read. Peek игнорируем //if (acPeekRead != 'R') // return; CESERVER_REQ *pIn = ExecuteNewCmd(CECMD_PEEKREADINFO, sizeof(CESERVER_REQ_HDR) //-V119 +sizeof(CESERVER_REQ_PEEKREADINFO)+(nRead-1)*sizeof(INPUT_RECORD)); if (pIn) { pIn->PeekReadInfo.nCount = (WORD)nRead; pIn->PeekReadInfo.cPeekRead = acPeekRead; pIn->PeekReadInfo.cUnicode = acUnicode; pIn->PeekReadInfo.h = hConsoleInput; pIn->PeekReadInfo.nTID = nCurrentTID; pIn->PeekReadInfo.nPID = GetCurrentProcessId(); pIn->PeekReadInfo.bMainThread = (pIn->PeekReadInfo.nTID == gnHookMainThreadId); memmove(pIn->PeekReadInfo.Buffer, lpBuffer, nRead*sizeof(INPUT_RECORD)); CESERVER_REQ* pOut = ExecuteGuiCmd(ghConWnd, pIn, ghConWnd); if (pOut) ExecuteFreeResult(pOut); ExecuteFreeResult(pIn); } }
// Вызывается ТОЛЬКО в главной нити! void CPluginBackground::UpdateBackground() { if (!mn_BgPluginsCount) return; if (!ghConEmuWndDC || !IsWindow(ghConEmuWndDC)) return; if (mb_ThNeedLoad) { LoadThSet(TRUE/*Мы уже в главной нити*/); } //RECT rcClient; GetClientRect(ghConEmuWndDC, &rcClient); struct PaintBackgroundArg Arg = m_Default; Arg.cbSize = sizeof(struct PaintBackgroundArg); //m_Default.dcSizeX = Arg.dcSizeX = rcClient.right+1; //m_Default.dcSizeY = Arg.dcSizeY = rcClient.bottom+1; m_Default.dcSizeX = Arg.dcSizeX = (m_Default.rcConWorkspace.right-m_Default.rcConWorkspace.left+1)*m_Default.MainFont.nFontCellWidth; m_Default.dcSizeY = Arg.dcSizeY = (m_Default.rcConWorkspace.bottom-m_Default.rcConWorkspace.top+1)*m_Default.MainFont.nFontHeight; // ********************************************************************************** // запомнить данные из m_Default в m_Last, но т.к. там есть указатели - move не катит // ********************************************************************************** //memmove(&m_Last, &m_Default, sizeof(m_Last)); m_Last.MainFont = m_Default.MainFont; memmove(m_Last.crPalette, m_Default.crPalette, sizeof(m_Last.crPalette)); m_Last.dcSizeX = m_Default.dcSizeX; m_Last.dcSizeY = m_Default.dcSizeY; m_Last.rcDcLeft = m_Default.rcDcLeft; m_Last.rcDcRight = m_Default.rcDcRight; m_Last.rcConWorkspace = m_Default.rcConWorkspace; m_Last.conCursor = m_Default.conCursor; m_Last.FarInterfaceSettings.Raw = m_Default.FarInterfaceSettings.Raw; m_Last.FarPanelSettings.Raw = m_Default.FarPanelSettings.Raw; memmove(m_Last.nFarColors, m_Default.nFarColors, sizeof(m_Last.nFarColors)); m_Last.bPanelsAllowed = m_Default.bPanelsAllowed; // struct tag_BkPanelInfo m_Last.LeftPanel.bVisible = m_Default.LeftPanel.bVisible; m_Last.LeftPanel.bFocused = m_Default.LeftPanel.bFocused; m_Last.LeftPanel.bPlugin = m_Default.LeftPanel.bPlugin; m_Last.LeftPanel.rcPanelRect = m_Default.LeftPanel.rcPanelRect; m_Last.RightPanel.bVisible = m_Default.RightPanel.bVisible; m_Last.RightPanel.bFocused = m_Default.RightPanel.bFocused; m_Last.RightPanel.bPlugin = m_Default.RightPanel.bPlugin; m_Last.RightPanel.rcPanelRect = m_Default.RightPanel.rcPanelRect; // строки lstrcpyW(m_Last.LeftPanel.szCurDir, m_Default.LeftPanel.szCurDir); lstrcpyW(m_Last.LeftPanel.szFormat, m_Default.LeftPanel.szFormat); lstrcpyW(m_Last.LeftPanel.szHostFile, m_Default.LeftPanel.szHostFile); lstrcpyW(m_Last.RightPanel.szCurDir, m_Default.RightPanel.szCurDir); lstrcpyW(m_Last.RightPanel.szFormat, m_Default.RightPanel.szFormat); lstrcpyW(m_Last.RightPanel.szHostFile, m_Default.RightPanel.szHostFile); // ********************************************************************************** if (m_Default.dcSizeX < 1 || m_Default.dcSizeY < 1) { _ASSERTE(m_Default.dcSizeX >= 1 && m_Default.dcSizeY >= 1); return; } SetDcPanelRect(&Arg.rcDcLeft, &Arg.LeftPanel, &Arg); SetDcPanelRect(&Arg.rcDcRight, &Arg.RightPanel, &Arg); // gpTabs отстает от реальности. Arg.Place = m_Default.Place; //if (!gpTabs) // Arg.Place = pbp_Panels; //else if (gnCurrentWindowType == WTYPE_EDITOR) // Arg.Place = pbp_Editor; //else if (gnCurrentWindowType == WTYPE_VIEWER) // Arg.Place = pbp_Viewer; //else if (Arg.LeftPanel.bVisible || Arg.RightPanel.bVisible) //{ // _ASSERTE(gnCurrentWindowType == WTYPE_PANELS); // Arg.Place = pbp_Panels; //} BITMAPINFOHEADER bi = {sizeof(BITMAPINFOHEADER)}; bi.biWidth = Arg.dcSizeX; bi.biHeight = Arg.dcSizeY; bi.biPlanes = 1; bi.biBitCount = 32; //-V112 bi.biCompression = BI_RGB; //_ASSERTE(Arg.LeftPanel.bVisible || Arg.RightPanel.bVisible); HDC hScreen = GetDC(NULL); RECT rcMeta = {0,0, Arg.dcSizeX, Arg.dcSizeY}; // (in pixels) int iWidthMM = GetDeviceCaps(hScreen, HORZSIZE); if (iWidthMM <= 0) { _ASSERTE(iWidthMM>0); iWidthMM = 1024; } int iHeightMM = GetDeviceCaps(hScreen, VERTSIZE); if (iHeightMM <= 0) { _ASSERTE(iHeightMM>0); iHeightMM = 768; } int iWidthPels = GetDeviceCaps(hScreen, HORZRES); if (iWidthPels <= 0) { _ASSERTE(iWidthPels>0); iWidthPels = 0; } int iHeightPels = GetDeviceCaps(hScreen, VERTRES); if (iHeightPels <= 0) { _ASSERTE(iHeightPels>0); iHeightPels = 0; } RECT rcMetaMM = {0,0, (rcMeta.right * iWidthMM * 100)/iWidthPels, (rcMeta.bottom * iHeightMM * 100)/iHeightPels}; // (in .01-millimeter units) HDC hdc = NULL; HENHMETAFILE hEmf = NULL; COLORREF* pBits = NULL; HBITMAP hDib = NULL, hOld = NULL; #ifdef CREATE_EMF_TEMP_FILES wchar_t szEmfFile[MAX_PATH] = {}; #endif wchar_t *pszEmfFile = NULL; if (SETBACKGROUND_USE_EMF==1) { #ifdef CREATE_EMF_TEMP_FILES GetTempPath(MAX_PATH-32, szEmfFile); int nLen = lstrlen(szEmfFile); if (*szEmfFile && szEmfFile[nLen-1] != L'\\') { szEmfFile[nLen++] = L'\\'; szEmfFile[nLen] = 0; } _wsprintf(szEmfFile+nLen, SKIPLEN(31) L"CeBack%u.emf", GetCurrentProcessId()); pszEmfFile = szEmfFile; #endif hdc = CreateEnhMetaFile(hScreen, pszEmfFile, &rcMetaMM, L"ConEmu\0Far Background\0\0"); if (!hdc) { _ASSERTE(hdc!=NULL); return; } } else { hdc = CreateCompatibleDC(hScreen); if (!hdc) { _ASSERTE(hdc!=NULL); return; } _ASSERTE(pBits = NULL); hDib = CreateDIBSection(hScreen, (BITMAPINFO*)&bi, DIB_RGB_COLORS, (void**)&pBits, NULL, 0); } ReleaseDC(NULL, hScreen); hScreen = NULL; Arg.hdc = hdc; if (SETBACKGROUND_USE_EMF==1) { HBRUSH hFillBr = CreateSolidBrush ( #ifdef _DEBUG RGB(128,128,0) #else RGB(0,0,0) #endif ); FillRect(hdc, &rcMeta, hFillBr); DeleteObject(hFillBr); } else { if (!hDib || !pBits) { _ASSERTE(hDib && pBits); if (hDib) DeleteObject(hDib); if (hdc) DeleteDC(hdc); return; } hOld = (HBITMAP)SelectObject(hdc, hDib); // Залить черным - по умолчанию #ifdef _DEBUG memset(pBits, 128, bi.biWidth*bi.biHeight*4); //-V112 #else memset(pBits, 0, bi.biWidth*bi.biHeight*4); #endif } // Painting! int nProcessed = 0; MSectionLock SC; SC.Lock(csBgPlugins, TRUE); DWORD nFromLevel = 0, nNextLevel, nSuggested; DWORD dwDrawnPlaces = Arg.Place; while(true) { nNextLevel = nFromLevel; struct RegisterBackgroundArg *p = mp_BgPlugins; for(int i = 0; i < mn_BgPluginsCount; i++, p++) { if (p->Cmd != rbc_Register || !(p->dwPlaces & Arg.Place) || !(p->PaintConEmuBackground)) continue; // пустая, неактивная в данном контексте, или некорректная ячейка // Слои nSuggested = p->dwSuggestedLevel; if (nSuggested < nFromLevel) { continue; // Этот слой уже обработан } else if (nSuggested > nFromLevel) { // Этот слой нужно будет обработать в следующий раз if ((nNextLevel == nFromLevel) || (nSuggested < nNextLevel)) nNextLevel = nSuggested; continue; } // На уровне 0 (заливающий фон) должен быть только один плагин Arg.dwLevel = (nProcessed == 0) ? 0 : (nFromLevel == 0 && nProcessed) ? 1 : nFromLevel; Arg.lParam = mp_BgPlugins[i].lParam; Arg.dwDrawnPlaces = 0; //mp_BgPlugins[i].PaintConEmuBackground(&Arg); UpdateBackground_Exec(mp_BgPlugins+i, &Arg); // Что плагин покрасил (панели/редактор/вьювер считаются покрашенными по умолчанию) dwDrawnPlaces |= Arg.dwDrawnPlaces; // nProcessed++; } if (nNextLevel == nFromLevel) break; // больше слоев нет nFromLevel = nNextLevel; } SC.Unlock(); // Sending background to GUI! if (nProcessed < 1) { // Ситуация возникает при старте ConEmu, когда панелей "еще нет" //_ASSERTE(nProcessed >= 1); if (mb_BgWasSent) { mb_BgWasSent = FALSE; CESERVER_REQ *pIn = ExecuteNewCmd(CECMD_SETBACKGROUND, sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_SETBACKGROUND)); if (pIn) { pIn->Background.nType = 1; pIn->Background.bEnabled = FALSE; pIn->Background.dwDrawnPlaces = 0; CESERVER_REQ *pOut = ExecuteGuiCmd(FarHwnd, pIn, FarHwnd); if (pOut) { ExecuteFreeResult(pOut); } ExecuteFreeResult(pIn); } } } else // есть "отработавшие" плагины, обновить Background! { GdiFlush(); DWORD nBitSize = 0, nBitsError = 0; if (SETBACKGROUND_USE_EMF==1) { hEmf = CloseEnhMetaFile(hdc); hdc = NULL; nBitSize = GetEnhMetaFileBits(hEmf, 0, NULL); if (nBitSize == 0) { dwDrawnPlaces = 0; nBitsError = GetLastError(); _ASSERTE(nBitSize!=0); if (hEmf) { // В случае ошибки - сразу удаляем DeleteEnhMetaFile(hEmf); hEmf = NULL; } } } else { nBitSize = bi.biWidth*bi.biHeight*sizeof(COLORREF); } DWORD nWholeSize = sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_SETBACKGROUND)+nBitSize; //-V103 //-V119 CESERVER_REQ *pIn = ExecuteNewCmd(CECMD_SETBACKGROUND, nWholeSize); if (!pIn) { _ASSERTE(pIn); } else { pIn->Background.nType = 1; pIn->Background.bEnabled = TRUE; pIn->Background.dwDrawnPlaces = dwDrawnPlaces; if (SETBACKGROUND_USE_EMF==1) pIn->Background.bmp.bfType = 0x4645/*EF*/; else pIn->Background.bmp.bfType = 0x4D42/*BM*/; pIn->Background.bmp.bfSize = nBitSize+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); //-V119 pIn->Background.bmp.bfOffBits = sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER); //-V119 pIn->Background.bi = bi; if (SETBACKGROUND_USE_EMF==1) { LPBYTE pBits = ((LPBYTE)&pIn->Background)+sizeof(pIn->Background); DWORD nBitsRc = (nBitSize && hEmf) ? GetEnhMetaFileBits(hEmf, nBitSize, pBits) : 0; _ASSERTE(nBitsRc == nBitSize); if (!nBitsRc) { _ASSERTE(nBitsRc!=NULL); // Отключить нафиг ExecutePrepareCmd(&pIn->hdr, CECMD_SETBACKGROUND, nWholeSize-nBitSize); pIn->Background.nType = 1; pIn->Background.bEnabled = FALSE; pIn->Background.dwDrawnPlaces = 0; } } else { memmove(((LPBYTE)&pIn->Background)+sizeof(pIn->Background), pBits, bi.biWidth*bi.biHeight*sizeof(COLORREF)); } CESERVER_REQ *pOut = ExecuteGuiCmd(FarHwnd, pIn, FarHwnd); // Вызывается ТОЛЬКО в главной нити _ASSERTE(GetCurrentThreadId() == gnMainThreadId); if (pOut) { mb_BgWasSent = TRUE; // Сбросим флажок "Ошибка уже была показана" if (pOut->BackgroundRet.nResult == esbr_OK) { mb_BgErrorShown = FALSE; } // Показать ошибку, если есть else if ((pOut->BackgroundRet.nResult > esbr_OK) && (pOut->BackgroundRet.nResult <= esbr_LastErrorNo) && (pOut->BackgroundRet.nResult != esbr_ConEmuInShutdown) && !mb_BgErrorShown) { mb_BgErrorShown = TRUE; Plugin()->ShowMessage(CEBkError_ExecFailed+pOut->BackgroundRet.nResult, 0); } ExecuteFreeResult(pOut); } else if (!mb_BgErrorShown) { mb_BgErrorShown = TRUE; Plugin()->ShowMessage(CEBkError_ExecFailed, 0); } ExecuteFreeResult(pIn); } } if (SETBACKGROUND_USE_EMF == 0) { if (hdc && hOld) SelectObject(hdc, hOld); if (hDib) DeleteObject(hDib); if (hdc) DeleteDC(hdc); } else { if (hdc) { hEmf = CloseEnhMetaFile(hdc); hdc = NULL; } if (hEmf) { DeleteEnhMetaFile(hEmf); hEmf = NULL; } } }
bool CAttachDlg::StartAttach(HWND ahAttachWnd, DWORD anPID, DWORD anBits, AttachProcessType anType, BOOL abAltMode) { bool lbRc = false; // Тут нужно получить инфу из списка и дернуть собственно аттач wchar_t szPipe[MAX_PATH]; PROCESS_INFORMATION pi = {}; STARTUPINFO si = {sizeof(si)}; SHELLEXECUTEINFO sei = {sizeof(sei)}; CESERVER_REQ *pIn = NULL, *pOut = NULL; HANDLE hPipeTest = NULL, hProcTest = NULL; DWORD nErrCode = 0; bool lbCreate; CESERVER_CONSOLE_MAPPING_HDR srv; DWORD nWrapperWait = -1; DWORD nWrapperResult = -1; if (!ahAttachWnd || !anPID || !anBits || !anType) { MBoxAssert(ahAttachWnd && anPID && anBits && anType); goto wrap; } if (gpSetCls->isAdvLogging) { wchar_t szInfo[128]; _wsprintf(szInfo, SKIPLEN(countof(szInfo)) L"CAttachDlg::StartAttach HWND=x%08X, PID=%u, Bits%u, Type=%u, AltMode=%u", (DWORD)(DWORD_PTR)ahAttachWnd, anPID, anBits, (UINT)anType, abAltMode); gpConEmu->LogString(szInfo); } if (LoadSrvMapping(ahAttachWnd, srv)) { pIn = ExecuteNewCmd(CECMD_ATTACH2GUI, sizeof(CESERVER_REQ_HDR)); pOut = ExecuteSrvCmd(srv.nServerPID, pIn, ghWnd); if (pOut && (pOut->hdr.cbSize >= (sizeof(CESERVER_REQ_HDR)+sizeof(DWORD))) && (pOut->dwData[0] != 0)) { lbRc = true; // Успешно подцепились goto wrap; } ExecuteFreeResult(pIn); ExecuteFreeResult(pOut); } // Может быть в процессе уже есть ConEmuHk.dll? Или этот процесс вообще уже во вкладке другого ConEmu? _wsprintf(szPipe, SKIPLEN(countof(szPipe)) CEHOOKSPIPENAME, L".", anPID); hPipeTest = CreateFile(szPipe, GENERIC_READ|GENERIC_WRITE, 0, LocalSecurity(), OPEN_EXISTING, 0, NULL); if (hPipeTest && hPipeTest != INVALID_HANDLE_VALUE) { CloseHandle(hPipeTest); goto DoExecute; } wchar_t szSrv[MAX_PATH+64], szArgs[128]; wcscpy_c(szSrv, gpConEmu->ms_ConEmuBaseDir); wcscat_c(szSrv, (anBits==64) ? L"\\ConEmuC64.exe" : L"\\ConEmuC.exe"); if (abAltMode && (anType == apt_Console)) { _wsprintf(szArgs, SKIPLEN(countof(szArgs)) L" /ATTACH /CONPID=%u /GID=%u /GHWND=%08X", anPID, GetCurrentProcessId(), (DWORD)ghWnd); } else { _wsprintf(szArgs, SKIPLEN(countof(szArgs)) L" /INJECT=%u", anPID); abAltMode = FALSE; } TODO("Определить, может он уже под админом? Тогда и ConEmuC.exe под админом запускать нужно"); si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; if (anType == apt_Gui) { gpConEmu->CreateGuiAttachMapping(anPID); } hProcTest = OpenProcess(PROCESS_CREATE_THREAD|PROCESS_QUERY_INFORMATION|PROCESS_VM_OPERATION|PROCESS_VM_WRITE|PROCESS_VM_READ, FALSE, anPID); if (hProcTest == NULL) { nErrCode = GetLastError(); MBoxAssert(hProcTest!=NULL || nErrCode==ERROR_ACCESS_DENIED); sei.hwnd = ghWnd; sei.fMask = (abAltMode ? 0 : SEE_MASK_NO_CONSOLE)|SEE_MASK_NOCLOSEPROCESS|SEE_MASK_NOASYNC; sei.lpVerb = L"runas"; sei.lpFile = szSrv; sei.lpParameters = szArgs; sei.lpDirectory = gpConEmu->ms_ConEmuBaseDir; sei.nShow = SW_SHOWMINIMIZED; lbCreate = ShellExecuteEx(&sei); if (lbCreate) { MBoxAssert(sei.hProcess!=NULL); pi.hProcess = sei.hProcess; } } else { lbCreate = CreateProcess(szSrv, szArgs, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS|(abAltMode ? CREATE_NO_WINDOW : CREATE_NEW_CONSOLE), NULL, NULL, &si, &pi); } if (!lbCreate) { wchar_t szErrMsg[MAX_PATH+255], szTitle[128]; DWORD dwErr = GetLastError(); _wsprintf(szErrMsg, SKIPLEN(countof(szErrMsg)) L"Can't start %s server\n%s %s", abAltMode ? L"injection" : L"console", szSrv, szArgs); _wsprintf(szTitle, SKIPLEN(countof(szTitle)) L"ConEmu Attach, PID=%u, TID=%u", GetCurrentProcessId(), GetCurrentThreadId()); DisplayLastError(szErrMsg, dwErr, 0, szTitle); goto wrap; } if (abAltMode) { TODO("Подождать бы завершения процесса, или пока он подцепится к GUI"); lbRc = true; goto wrap; } nWrapperWait = WaitForSingleObject(pi.hProcess, INFINITE); nWrapperResult = -1; GetExitCodeProcess(pi.hProcess, &nWrapperResult); CloseHandle(pi.hProcess); if (pi.hThread) CloseHandle(pi.hThread); if (((int)nWrapperResult != CERR_HOOKS_WAS_SET) && ((int)nWrapperResult != CERR_HOOKS_WAS_ALREADY_SET)) { goto wrap; } DoExecute: // Теперь можно дернуть созданный в удаленном процессе пайп для запуска в той консоли сервера. pIn = ExecuteNewCmd(CECMD_STARTSERVER, sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_START)); pIn->NewServer.nGuiPID = GetCurrentProcessId(); pIn->NewServer.hGuiWnd = ghWnd; if (anType == apt_Gui) { _ASSERTE(ahAttachWnd && IsWindow(ahAttachWnd)); pIn->NewServer.hAppWnd = ahAttachWnd; } pOut = ExecuteCmd(szPipe, pIn, 500, ghWnd); if (!pOut || (pOut->hdr.cbSize < pIn->hdr.cbSize) || (pOut->dwData[0] == 0)) { _ASSERTE(pOut && pOut->hdr.cbSize == (sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_START))); wchar_t szMsg[255], szTitle[128]; wcscpy_c(szMsg, L"Failed to start console server in the remote process"); if (hPipeTest && hPipeTest != INVALID_HANDLE_VALUE) wcscat_c(szMsg, L"\nHooks already was set"); _wsprintf(szTitle, SKIPLEN(countof(szTitle)) L"ConEmu Attach, PID=%u, TID=%u", GetCurrentProcessId(), GetCurrentThreadId()); DisplayLastError(szMsg, (pOut && (pOut->hdr.cbSize >= pIn->hdr.cbSize)) ? pOut->dwData[1] : -1, 0, szTitle); goto wrap; } lbRc = true; wrap: UNREFERENCED_PARAMETER(nErrCode); UNREFERENCED_PARAMETER(nWrapperWait); ExecuteFreeResult(pIn); ExecuteFreeResult(pOut); return lbRc; }
//Arguments: // hConWnd - Хэндл КОНСОЛЬНОГО окна (по нему формируется имя пайпа для GUI) // pIn - выполняемая команда // nTimeout- таймаут подключения //Returns: // CESERVER_REQ. Его необходимо освободить через free(...); //WARNING!!! // Эта процедура не может получить с сервера более 600 байт данных! // В заголовке hOwner в дебаге может быть отображена ошибка CESERVER_REQ* ExecuteCmd(const wchar_t* szPipeName, CESERVER_REQ* pIn, DWORD nWaitPipe, HWND hOwner, BOOL bAsyncNoResult, DWORD nServerPID, BOOL bIgnoreAbsence /*= FALSE*/) { CESERVER_REQ* pOut = NULL; HANDLE hPipe = NULL; BYTE cbReadBuf[600]; // чтобы CESERVER_REQ_OUTPUTFILE поместился wchar_t szErr[MAX_PATH*2]; szErr[0] = 0; BOOL fSuccess = FALSE; DWORD cbRead = 0, /*dwMode = 0,*/ dwErr = 0; int nAllSize; LPBYTE ptrData; #ifdef _DEBUG bool bIsAltSrvCmd; wchar_t szDbgPrefix[64], szDbgResult[64], *pszDbgMsg = NULL; #endif if (!pIn || !szPipeName) { _ASSERTE(pIn && szPipeName); pOut = NULL; goto wrap; } #ifdef _DEBUG _wsprintf(szDbgPrefix, SKIPLEN(countof(szDbgPrefix)) L">> ExecCmd: PID=%5u TID=%5u Cmd=%3u ", GetCurrentProcessId(), GetCurrentThreadId(), pIn->hdr.nCmd); pszDbgMsg = lstrmerge(szDbgPrefix, szPipeName, L"\n"); if (pszDbgMsg) { DEBUGSTRCMD(pszDbgMsg); free(pszDbgMsg); } #endif pIn->hdr.bAsync = bAsyncNoResult; _ASSERTE(pIn->hdr.nSrcPID && pIn->hdr.nSrcThreadId); _ASSERTE(pIn->hdr.cbSize >= sizeof(pIn->hdr)); hPipe = ExecuteOpenPipe(szPipeName, szErr, NULL/*Сюда хорошо бы имя модуля подкрутить*/, nServerPID, nWaitPipe, FALSE, NULL, bIgnoreAbsence); if (hPipe == NULL || hPipe == INVALID_HANDLE_VALUE) { #ifdef _DEBUG dwErr = GetLastError(); // в заголовке "чисто" запущенного фара появляются отладочные(?) сообщения // по идее - не должны, т.к. все должно быть через мэппинг // *** _ASSERTEX(hPipe != NULL && hPipe != INVALID_HANDLE_VALUE); - no need in assert, it was already shown #ifdef CONEMU_MINIMAL SetConsoleTitle(szErr); #else if (hOwner) { if (hOwner == myGetConsoleWindow()) SetConsoleTitle(szErr); else SetWindowText(hOwner, szErr); } #endif #endif pOut = NULL; goto wrap; } #ifdef _DEBUG bIsAltSrvCmd = (pIn->hdr.nCmd==CECMD_ALTBUFFER || pIn->hdr.nCmd==CECMD_ALTBUFFERSTATE || pIn->hdr.nCmd==CECMD_SETCONSCRBUF || pIn->hdr.nCmd == CECMD_LOCKSTATION || pIn->hdr.nCmd == CECMD_UNLOCKSTATION); _ASSERTE(pIn->hdr.nSrcThreadId==GetCurrentThreadId() || (bIsAltSrvCmd && pIn->hdr.nSrcPID!=GetCurrentProcessId())); #endif if (bAsyncNoResult) { // Если нас не интересует возврат и нужно сразу вернуться fSuccess = WriteFile(hPipe, pIn, pIn->hdr.cbSize, &cbRead, NULL); #ifdef _DEBUG dwErr = GetLastError(); _ASSERTE(fSuccess && (cbRead == pIn->hdr.cbSize)); #endif // -- Do not close hPipe, otherwise the reader may fail with that packet // -- with error ‘pipe was closed before end’. // -- Handle leak, yeah, however this is rarely used op. // -- Must be refactored, but not so critical... // -- CloseHandle(hPipe); pOut = NULL; goto wrap; } else { WARNING("При Overlapped часто виснет в этом месте."); // Send a message to the pipe server and read the response. fSuccess = TransactNamedPipe( hPipe, // pipe handle (LPVOID)pIn, // message to server pIn->hdr.cbSize, // message length cbReadBuf, // buffer to receive reply sizeof(cbReadBuf), // size of read buffer &cbRead, // bytes read NULL); // not overlapped dwErr = GetLastError(); //CloseHandle(hPipe); if (!fSuccess && (dwErr != ERROR_MORE_DATA)) { //_ASSERTE(fSuccess || (dwErr == ERROR_MORE_DATA)); CloseHandle(hPipe); pOut = NULL; goto wrap; } } if (cbRead < sizeof(CESERVER_REQ_HDR)) { CloseHandle(hPipe); pOut = NULL; goto wrap; } pOut = (CESERVER_REQ*)cbReadBuf; // temporary if (pOut->hdr.cbSize < cbRead) { CloseHandle(hPipe); if (pOut->hdr.cbSize) { _ASSERTE(pOut->hdr.cbSize == 0 || pOut->hdr.cbSize >= cbRead); DEBUGSTR(L"!!! Wrong nSize received from GUI server !!!\n"); } pOut = NULL; goto wrap; } if (pOut->hdr.nVersion != CESERVER_REQ_VER) { CloseHandle(hPipe); DEBUGSTR(L"!!! Wrong nVersion received from GUI server !!!\n"); pOut = NULL; goto wrap; } nAllSize = pOut->hdr.cbSize; pOut = (CESERVER_REQ*)malloc(nAllSize); _ASSERTE(pOut); if (!pOut) { CloseHandle(hPipe); _ASSERTE(pOut == NULL); goto wrap; } memmove(pOut, cbReadBuf, cbRead); ptrData = ((LPBYTE)pOut)+cbRead; nAllSize -= cbRead; while (nAllSize>0) { // Break if TransactNamedPipe or ReadFile is successful if (fSuccess) break; // Read from the pipe if there is more data in the message. fSuccess = ReadFile( hPipe, // pipe handle ptrData, // buffer to receive reply nAllSize, // size of buffer &cbRead, // number of bytes read NULL); // not overlapped // Exit if an error other than ERROR_MORE_DATA occurs. if (!fSuccess && ((dwErr = GetLastError()) != ERROR_MORE_DATA)) break; ptrData += cbRead; nAllSize -= cbRead; } CloseHandle(hPipe); if (pOut && (pOut->hdr.nCmd != pIn->hdr.nCmd)) { _ASSERTE(pOut->hdr.nCmd == pIn->hdr.nCmd); if (pOut->hdr.nCmd == 0) { ExecuteFreeResult(pOut); pOut = NULL; } } wrap: #ifdef _DEBUG if (pOut) _wsprintf(szDbgResult, SKIPLEN(countof(szDbgResult)) L"- Data=%5u Err=%u\n", pOut->DataSize(), dwErr); else lstrcpyn(szDbgResult, L"[NULL]\n", countof(szDbgResult)); pszDbgMsg = lstrmerge(szDbgPrefix, szDbgResult); if (pszDbgMsg) { DEBUGSTRCMD(pszDbgMsg); free(pszDbgMsg); } #endif return pOut; }
void CGuiServer::GuiServerFree(CESERVER_REQ* pReply, LPARAM lParam) { ExecuteFreeResult(pReply); }
// Если (anFromShowWindow != -1), значит функу зовут из ShowWindow void OnGuiWindowAttached(HWND hWindow, HMENU hMenu, LPCSTR asClassA, LPCWSTR asClassW, DWORD anStyle, DWORD anStyleEx, BOOL abStyleHidden, int anFromShowWindow/*=-1*/) { DWORD nCurStyle = (DWORD)user->getWindowLongPtrW(hWindow, GWL_STYLE); DWORD nCurStyleEx = (DWORD)user->getWindowLongPtrW(hWindow, GWL_EXSTYLE); user->allowSetForegroundWindow(ASFW_ANY); // VLC создает несколько "подходящих" окон, но ShowWindow зовет // только для одного из них. Поэтому фактический аттач делаем // только в том случае, если окно "видимое" if ((!(nCurStyle & WS_VISIBLE)) && (anFromShowWindow <= SW_HIDE)) { // Значит потом, из ShowWindow return; } ghAttachGuiClient = hWindow; gnAttachGuiClientThreadId = user->getWindowThreadProcessId(hWindow, NULL); gbForceShowGuiClient = TRUE; gbAttachGuiClient = FALSE; // Только одно окно приложения. Пока? #if 0 // Для WS_CHILDWINDOW меню нельзя указать при создании окна if (!hMenu && !ghAttachGuiClientMenu && (asClassA || asClassW)) { BOOL lbRcClass; WNDCLASSEXA wca = {sizeof(WNDCLASSEXA)}; WNDCLASSEXW wcw = {sizeof(WNDCLASSEXW)}; if (asClassA) { lbRcClass = GetClassInfoExA(GetModuleHandle(NULL), asClassA, &wca); if (lbRcClass) ghAttachGuiClientMenu = LoadMenuA(wca.hInstance, wca.lpszMenuName); } else { lbRcClass = GetClassInfoExW(GetModuleHandle(NULL), asClassW, &wcw); if (lbRcClass) ghAttachGuiClientMenu = LoadMenuW(wca.hInstance, wcw.lpszMenuName); } hMenu = ghAttachGuiClientMenu; } if (hMenu) { // Для WS_CHILDWINDOW - не работает SetMenu(hWindow, hMenu); HMENU hSys = GetSystemMenu(hWindow, FALSE); TODO("Это в принципе прокатывает, но нужно транслировать WM_SYSCOMMAND -> WM_COMMAND, соответственно, перехватывать WndProc, или хук ставить"); if (hSys) { TODO("Хотя, хорошо бы не все в Popup засоывать, а извлечь ChildPopups из hMenu"); InsertMenu(hSys, 0, MF_BYPOSITION|MF_POPUP, (UINT_PTR)hMenu, L"Window menu"); InsertMenu(hSys, 1, MF_BYPOSITION|MF_SEPARATOR, NULL, NULL); } } #endif DWORD nSize = sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_ATTACHGUIAPP); CESERVER_REQ *pIn = ExecuteNewCmd(CECMD_ATTACHGUIAPP, nSize); gnAttachGuiClientFlags = agaf_Success; // С приложенями .Net - приходится работать как с WS_CHILD, // иначе в них "не нажимаются" тулбары и меню if (IsDotNetWindow(hWindow)) { gnAttachGuiClientFlags |= (agaf_DotNet|agaf_WS_CHILD); } // Если в окне нет меню - работаем с ним как с WS_CHILD // так не возникает проблем с активацией и т.д. else if (user->getMenu(hWindow) == NULL) { if (IsQtWindow(asClassA, asClassW)) gnAttachGuiClientFlags |= (agaf_NoMenu|agaf_QtWindow|agaf_WS_CHILD); else gnAttachGuiClientFlags |= (agaf_NoMenu|agaf_WS_CHILD); } pIn->AttachGuiApp.nFlags = gnAttachGuiClientFlags; pIn->AttachGuiApp.nPID = GetCurrentProcessId(); pIn->AttachGuiApp.hAppWindow = hWindow; pIn->AttachGuiApp.Styles.nStyle = nCurStyle; // стили могли измениться после создания окна, pIn->AttachGuiApp.Styles.nStyleEx = nCurStyleEx; // поэтому получим актуальные user->getWindowRect(hWindow, &pIn->AttachGuiApp.rcWindow); GetModuleFileName(NULL, pIn->AttachGuiApp.sAppFilePathName, countof(pIn->AttachGuiApp.sAppFilePathName)); pIn->AttachGuiApp.hkl = (DWORD)(LONG)(LONG_PTR)GetKeyboardLayout(0); wchar_t szGuiPipeName[128]; msprintf(szGuiPipeName, countof(szGuiPipeName), CEGUIPIPENAME, L".", (DWORD)ghConEmuWnd); // AttachThreadInput DWORD nConEmuTID = user->getWindowThreadProcessId(ghConEmuWnd, NULL); DWORD nTID = GetCurrentThreadId(); _ASSERTEX(nTID==gnHookMainThreadId || nTID==gnAttachGuiClientThreadId); BOOL bAttachRc = user->attachThreadInput(nTID, nConEmuTID, TRUE); DWORD nAttachErr = GetLastError(); UNREFERENCED_PARAMETER(bAttachRc); UNREFERENCED_PARAMETER(nAttachErr); HWND hPreFocus = user->getFocus(); CESERVER_REQ* pOut = ExecuteCmd(szGuiPipeName, pIn, 0/*Default timeout*/, NULL); ExecuteFreeResult(pIn); // abStyleHidden == TRUE, если окно при создании указало флаг WS_VISIBLE (т.е. не собиралось звать ShowWindow) if (pOut) { if (pOut->hdr.cbSize > sizeof(CESERVER_REQ_HDR)) { _ASSERTE((pOut->AttachGuiApp.nFlags & agaf_Success) == agaf_Success); BOOL lbRc = FALSE; _ASSERTE(pOut->AttachGuiApp.hConEmuBack && pOut->AttachGuiApp.hConEmuDc && (HWND)pOut->AttachGuiApp.hConEmuDc!=(HWND)pOut->AttachGuiApp.hConEmuBack); _ASSERTE((ghConEmuWndBack==NULL) || (pOut->AttachGuiApp.hConEmuBack==ghConEmuWndBack)); _ASSERTE(ghConEmuWnd && (ghConEmuWnd==pOut->AttachGuiApp.hConEmuWnd)); ghConEmuWnd = pOut->AttachGuiApp.hConEmuWnd; SetConEmuHkWindows(pOut->AttachGuiApp.hConEmuDc, pOut->AttachGuiApp.hConEmuBack); //gbGuiClientHideCaption = pOut->AttachGuiApp.bHideCaption; gGuiClientStyles = pOut->AttachGuiApp.Styles; #ifdef _DEBUG HWND hFocus = user->getFocus(); DWORD nFocusPID = 0; if (hFocus) { user->getWindowThreadProcessId(hFocus, &nFocusPID); DWORD nConEmuPID = 0; user->getWindowThreadProcessId(ghConEmuWnd, &nConEmuPID); if (nFocusPID != GetCurrentProcessId() && nFocusPID != nConEmuPID) { _ASSERTE(hFocus==NULL || (nFocusPID==GetCurrentProcessId() || nFocusPID == nConEmuPID)); hFocus = NULL; } } #endif if (pOut->AttachGuiApp.hkl) { LONG_PTR hkl = (LONG_PTR)(LONG)pOut->AttachGuiApp.hkl; lbRc = ActivateKeyboardLayout((HKL)hkl, KLF_SETFORPROCESS) != NULL; UNREFERENCED_PARAMETER(lbRc); } //grcAttachGuiClientPos = pOut->AttachGuiApp.rcWindow; ReplaceGuiAppWindow(abStyleHidden); //if (hPreFocus) //{ // user->setFocus(hPreFocus); //} UINT nMsgID = user->registerWindowMessageW(CONEMUMSG_RESTORECHILDFOCUS); user->postMessageW(ghConEmuWndBack, nMsgID, 0,0); //// !!! OnSetForegroundWindow не подходит - он дергает Cmd. ////user->setForegroundWindow(ghConEmuWnd); //#if 0 //wchar_t szClass[64] = {}; user->getClassNameW(hFocus, szClass, countof(szClass)); //MessageBox(NULL, szClass, L"WasFocused", MB_SYSTEMMODAL); //#endif ////if (!(nCurStyle & WS_CHILDWINDOW)) //{ // // Если ставить WS_CHILD - пропадет меню! // //nCurStyle = (nCurStyle | WS_CHILDWINDOW|WS_TABSTOP); // & ~(WS_THICKFRAME/*|WS_CAPTION|WS_MINIMIZEBOX|WS_MAXIMIZEBOX*/); // //user->setWindowLongPtrW(hWindow, GWL_STYLE, nCurStyle); // if (gnAttachGuiClientFlags & agaf_DotNet) // { // } // else // { // SetParent(hWindow, ghConEmuWndBack); // } //} // //RECT rcGui = grcAttachGuiClientPos = pOut->AttachGuiApp.rcWindow; //if (user->setWindowPos(hWindow, HWND_TOP, rcGui.left,rcGui.top, rcGui.right-rcGui.left, rcGui.bottom-rcGui.top, // SWP_DRAWFRAME | /*SWP_FRAMECHANGED |*/ (abStyleHidden ? SWP_SHOWWINDOW : 0))) //{ // if (abStyleHidden) // abStyleHidden = FALSE; //} // //// !!! OnSetForegroundWindow не подходит - он дергает Cmd. //user->setForegroundWindow(ghConEmuWnd); ////if (hFocus) ////SetFocus(hFocus ? hFocus : hWindow); // hFocus==NULL, эффекта нет ////OnSetForegroundWindow(hWindow); ////user->postMessage(ghConEmuWnd, WM_NCACTIVATE, TRUE, 0); ////user->postMessage(ghConEmuWnd, WM_NCPAINT, 0, 0); //user->postMessage(hWindow, WM_NCPAINT, 0, 0); } ExecuteFreeResult(pOut); } if (abStyleHidden) { user->showWindow(hWindow, SW_SHOW); } }
void ComspecDone(int aiRc) { #ifdef _DEBUG xf_dump_chk(); xf_validate(NULL); #endif //WARNING("Послать в GUI CONEMUCMDSTOPPED"); LogSize(NULL, 0, "ComspecDone"); // Это необходимо делать, т.к. при смене буфера (SetConsoleActiveScreenBuffer) приложением, // дескриптор нужно закрыть, иначе conhost может не вернуть предыдущий буфер //ConOutCloseHandle() // Поддержка алиасов if (gpSrv->szComSpecName[0] && gpSrv->szSelfName[0]) { // Скопировать алиасы из cmd.exe в conemuc.exe wchar_t *pszPostAliases = NULL; DWORD nPostAliasSize; BOOL lbChanged = (gpSrv->pszPreAliases == NULL); if (!GetAliases(gpSrv->szComSpecName, &pszPostAliases, &nPostAliasSize)) { if (pszPostAliases) _wprintf(pszPostAliases); } else { if (!lbChanged) { lbChanged = (gpSrv->nPreAliasSize!=nPostAliasSize); } if (!lbChanged && gpSrv->nPreAliasSize && gpSrv->pszPreAliases && pszPostAliases) { lbChanged = memcmp(gpSrv->pszPreAliases,pszPostAliases,gpSrv->nPreAliasSize)!=0; } if (lbChanged) { xf_dump_chk(); if (gnMainServerPID) { MCHKHEAP; CESERVER_REQ* pIn = ExecuteNewCmd(CECMD_SAVEALIASES,sizeof(CESERVER_REQ_HDR)+nPostAliasSize); if (pIn) { MCHKHEAP; memmove(pIn->Data, pszPostAliases, nPostAliasSize); MCHKHEAP; CESERVER_REQ* pOut = ExecuteSrvCmd(gnMainServerPID, pIn, GetConEmuHWND(2), FALSE, 0, TRUE); MCHKHEAP; if (pOut) ExecuteFreeResult(pOut); ExecuteFreeResult(pIn); MCHKHEAP; } } xf_dump_chk(); wchar_t *pszNewName = pszPostAliases, *pszNewTarget, *pszNewLine; while (pszNewName && *pszNewName) { pszNewLine = pszNewName + lstrlen(pszNewName); pszNewTarget = wcschr(pszNewName, L'='); if (pszNewTarget) { *pszNewTarget = 0; pszNewTarget++; } if (*pszNewTarget == 0) pszNewTarget = NULL; AddConsoleAlias(pszNewName, pszNewTarget, gpSrv->szSelfName); pszNewName = pszNewLine+1; } xf_dump_chk(); } } if (pszPostAliases) { free(pszPostAliases); pszPostAliases = NULL; } } xf_dump_chk(); //TODO("Уведомить плагин через пайп (если родитель - FAR) что процесс завершен. Плагин должен считать и запомнить содержимое консоли и только потом вернуть управление в ConEmuC!"); DWORD dwErr1 = 0; //, dwErr2 = 0; HANDLE hOut1 = NULL, hOut2 = NULL; BOOL lbRc1 = FALSE, lbRc2 = FALSE; CONSOLE_SCREEN_BUFFER_INFO sbi1 = {{0,0}}, sbi2 = {{0,0}}; #ifdef _DEBUG HWND hWndCon = GetConEmuHWND(2); #endif // Тут нужна реальная, а не скорректированная информация! if (!gbNonGuiMode) { // Если GUI не сможет через сервер вернуть высоту буфера - это нужно сделать нам! lbRc1 = GetConsoleScreenBufferInfo(hOut1 = GetStdHandle(STD_OUTPUT_HANDLE), &sbi1); if (!lbRc1) dwErr1 = GetLastError(); xf_dump_chk(); } //PRAGMA_ERROR("Размер должен возвращать сам GUI, через серверный ConEmuC!"); #ifdef SHOW_STARTED_MSGBOX MessageBox(GetConEmuHWND(2), L"ConEmuC (comspec mode) is about to TERMINATE", L"ConEmuC.ComSpec", 0); #endif #ifdef _DEBUG xf_dump_chk(); xf_validate(NULL); #endif if (!gbNonGuiMode && (gpSrv->dwParentFarPID != 0)) { //// Вернуть размер буфера (высота И ширина) //if (gpSrv->sbi.dwSize.X && gpSrv->sbi.dwSize.Y) { // SMALL_RECT rc = {0}; // SetConsoleSize(0, gpSrv->sbi.dwSize, rc, "ComspecDone"); //} //ConOutCloseHandle() CONSOLE_SCREEN_BUFFER_INFO l_csbi = {{0}}; lbRc2 = GetConsoleScreenBufferInfo(hOut2 = GetStdHandle(STD_OUTPUT_HANDLE), &l_csbi); CESERVER_REQ *pOut = SendStopped(&l_csbi); if (pOut) { if (!pOut->StartStopRet.bWasBufferHeight) { //gpSrv->sbi.dwSize = pIn->StartStop.sbi.dwSize; lbRc1 = FALSE; // Консольное приложение самостоятельно сбросило буферный режим. Не дергаться... } else { lbRc1 = TRUE; } ExecuteFreeResult(pOut); pOut = NULL; } if (!gbWasBufferHeight) { lbRc2 = GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &sbi2); #ifdef _DEBUG if (sbi2.dwSize.Y > 200) { wchar_t szTitle[128]; _wsprintf(szTitle, SKIPLEN(countof(szTitle)) L"ConEmuC (PID=%i)", GetCurrentProcessId()); MessageBox(NULL, L"BufferHeight was not turned OFF", szTitle, MB_SETFOREGROUND|MB_SYSTEMMODAL); } #endif if (lbRc1 && lbRc2 && sbi2.dwSize.Y == sbi1.dwSize.Y) { // GUI не смог вернуть высоту буфера... // Это плохо, т.к. фар высоту буфера не меняет и будет сильно глючить на N сотнях строк... int nNeedHeight = gpSrv->sbi.dwSize.Y; if (nNeedHeight < 10) { nNeedHeight = (sbi2.srWindow.Bottom-sbi2.srWindow.Top+1); } if (sbi2.dwSize.Y != nNeedHeight) { _ASSERTE(sbi2.dwSize.Y == nNeedHeight); PRINT_COMSPEC(L"Error: BufferHeight was not changed from %i\n", sbi2.dwSize.Y); SMALL_RECT rc = {0}; sbi2.dwSize.Y = nNeedHeight; if (gpLogSize) LogSize(&sbi2.dwSize, 0, ":ComspecDone.RetSize.before"); SetConsoleSize(0, sbi2.dwSize, rc, "ComspecDone.Force"); if (gpLogSize) LogSize(NULL, 0, ":ComspecDone.RetSize.after"); } } } } if (gpSrv->pszPreAliases) { free(gpSrv->pszPreAliases); gpSrv->pszPreAliases = NULL; } //SafeCloseHandle(ghCtrlCEvent); //SafeCloseHandle(ghCtrlBreakEvent); }
void OnShowGuiClientWindow(HWND hWnd, int &nCmdShow, BOOL &rbGuiAttach, BOOL &rbInactive) { #ifdef _DEBUG STARTUPINFO si = {sizeof(si)}; GetStartupInfo(&si); #endif rbInactive = FALSE; //if (ghConEmuWnd) //{ // DWORD nConEmuExStyle = user->getWindowLongPtrW(ghConEmuWnd, GWL_EXSTYLE); // if (nConEmuExStyle & WS_EX_TOPMOST) // { // DWORD nExtStyle = user->getWindowLongPtrW(hWnd, GWL_EXSTYLE); // if (!(nExtStyle & WS_EX_TOPMOST)) // { // nExtStyle |= WS_EX_TOPMOST; // user->setWindowLongPtrW(hWnd, GWL_EXSTYLE, nExtStyle); // } // } //} if ((!ghAttachGuiClient) && gbAttachGuiClient && (nCmdShow >= SW_SHOWNORMAL)) { // VLC создает несколько "подходящих" окон, но ShowWindow зовет // только для одного из них. Поэтому фактический аттач делаем // только в том случае, если окно "видимое" HMENU hMenu = user->getMenu(hWnd); wchar_t szClassName[255]; user->getClassNameW(hWnd, szClassName, countof(szClassName)); DWORD nCurStyle = (DWORD)user->getWindowLongPtrW(hWnd, GWL_STYLE); DWORD nCurStyleEx = (DWORD)user->getWindowLongPtrW(hWnd, GWL_EXSTYLE); BOOL bAttachGui = TRUE; if (ghAttachGuiClient == NULL) { HWND hWndParent = ::GetParent(hWnd); DWORD dwStyle = nCurStyle, dwExStyle = nCurStyleEx; BOOL bStyleHidden; CheckCanCreateWindow(NULL, szClassName, dwStyle, dwExStyle, hWndParent, bAttachGui, bStyleHidden); } // Пробуем if (bAttachGui) { OnGuiWindowAttached(hWnd, hMenu, NULL, szClassName, nCurStyle, nCurStyleEx, FALSE, nCmdShow); } } if (gbForceShowGuiClient && (ghAttachGuiClient == hWnd)) { //if (nCmdShow == SW_HIDE) // nCmdShow = SW_SHOWNORMAL; HWND hCurParent = user->getParent(hWnd); if (hCurParent != ghConEmuWndBack) { DWORD nCurStyle = (DWORD)user->getWindowLongPtrW(hWnd, GWL_STYLE); DWORD nCurStyleEx = (DWORD)user->getWindowLongPtrW(hWnd, GWL_EXSTYLE); DWORD nSize = sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_ATTACHGUIAPP); CESERVER_REQ *pIn = ExecuteNewCmd(CECMD_ATTACHGUIAPP, nSize); pIn->AttachGuiApp.nFlags = gnAttachGuiClientFlags; pIn->AttachGuiApp.nPID = GetCurrentProcessId(); pIn->AttachGuiApp.hAppWindow = hWnd; pIn->AttachGuiApp.Styles.nStyle = nCurStyle; // стили могли измениться после создания окна, pIn->AttachGuiApp.Styles.nStyleEx = nCurStyleEx; // поэтому получим актуальные user->getWindowRect(hWnd, &pIn->AttachGuiApp.rcWindow); GetModuleFileName(NULL, pIn->AttachGuiApp.sAppFilePathName, countof(pIn->AttachGuiApp.sAppFilePathName)); wchar_t szGuiPipeName[128]; msprintf(szGuiPipeName, countof(szGuiPipeName), CEGUIPIPENAME, L".", (DWORD)ghConEmuWnd); CESERVER_REQ* pOut = ExecuteCmd(szGuiPipeName, pIn, 0/*Default timeout*/, NULL); ExecuteFreeResult(pIn); if (pOut) { if (pOut->hdr.cbSize > sizeof(CESERVER_REQ_HDR) && (pOut->AttachGuiApp.nFlags & agaf_Success)) { //grcAttachGuiClientPos = pOut->AttachGuiApp.rcWindow; _ASSERTE((ghConEmuWndBack==NULL) || (pOut->AttachGuiApp.hConEmuBack==ghConEmuWndBack)); _ASSERTE(ghConEmuWnd && (ghConEmuWnd==pOut->AttachGuiApp.hConEmuWnd)); ghConEmuWnd = pOut->AttachGuiApp.hConEmuWnd; SetConEmuHkWindows(pOut->AttachGuiApp.hConEmuDc, pOut->AttachGuiApp.hConEmuBack); //gbGuiClientHideCaption = pOut->AttachGuiApp.bHideCaption; gGuiClientStyles = pOut->AttachGuiApp.Styles; //Если приложение создается в НЕ активной вкладке - фокус нужно вернуть в ConEmu rbInactive = (pOut->AttachGuiApp.nFlags & agaf_Inactive) == agaf_Inactive; } ExecuteFreeResult(pOut); } //OnSetParent(hWnd, ghConEmuWndBack); } ReplaceGuiAppWindow(FALSE); //RECT rcGui = grcAttachGuiClientPos; //user->setWindowPos(hWnd, HWND_TOP, rcGui.left,rcGui.top, rcGui.right-rcGui.left, rcGui.bottom-rcGui.top, // SWP_FRAMECHANGED); nCmdShow = SW_SHOWNORMAL; gbForceShowGuiClient = FALSE; // Один раз? rbGuiAttach = TRUE; } }
// Returns HWND of ... // aiType==0: Gui console DC window // ==1: Gui Main window // ==2: Console window HWND GetConEmuHWND(int aiType) { //CESERVER_REQ *pIn = NULL; //CESERVER_REQ *pOut = NULL; DWORD nLastErr = GetLastError(); HWND FarHwnd = NULL, ConEmuHwnd = NULL, ConEmuRoot = NULL; size_t cchMax = 128; wchar_t *szGuiPipeName = NULL; FarHwnd = myGetConsoleWindow(); if (!FarHwnd || (aiType == 2)) { goto wrap; //SetLastError(nLastErr); //return NULL; } szGuiPipeName = (wchar_t*)malloc(cchMax*sizeof(*szGuiPipeName)); if (!szGuiPipeName) { _ASSERTE(szGuiPipeName!=NULL); return NULL; } // Сначала пробуем Mapping консоли (вдруг есть?) if (!ConEmuRoot) { // создание этого объекта не позволяет отказаться от CRT (создается __chkstk) //MFileMapping<CESERVER_CONSOLE_MAPPING_HDR> ConMap; //ConMap.InitName(CECONMAPNAME, (DWORD)FarHwnd); //CESERVER_CONSOLE_MAPPING_HDR* p = ConMap.Open(); CESERVER_CONSOLE_MAPPING_HDR* p = NULL; msprintf(szGuiPipeName, cchMax, CECONMAPNAME, (DWORD)FarHwnd); //-V205 #ifdef _DEBUG size_t nSize = sizeof(*p); #endif HANDLE hMapping = OpenFileMapping(FILE_MAP_READ, FALSE, szGuiPipeName); if (hMapping) { DWORD nFlags = FILE_MAP_READ; p = (CESERVER_CONSOLE_MAPPING_HDR*)MapViewOfFile(hMapping, nFlags,0,0,0); } if (p && p->hConEmuRoot && isWindow(p->hConEmuRoot)) { // Успешно ConEmuRoot = p->hConEmuRoot; ConEmuHwnd = p->hConEmuWnd; } if (p) UnmapViewOfFile(p); if (hMapping) CloseHandle(hMapping); } #if 0 // Сервер не мог подцепиться БЕЗ создания мэппинга, поэтому CECMD_GETGUIHWND можно не делать if (!ConEmuRoot) { //BOOL lbRc = FALSE; pIn = (CESERVER_REQ*)calloc(1,sizeof(CESERVER_REQ)); ExecutePrepareCmd(pIn, CECMD_GETGUIHWND, sizeof(CESERVER_REQ_HDR)); //_wsprintf(szGuiPipeName, SKIPLEN(countof(szGuiPipeName)) CEGUIPIPENAME, L".", (DWORD)FarHwnd); msprintf(szGuiPipeName, cchMax, CEGUIPIPENAME, L".", (DWORD)FarHwnd); // Таймаут уменьшим, т.к. на результат не надеемся pOut = ExecuteCmd(szGuiPipeName, pIn, 250, FarHwnd); if (!pOut) { goto wrap; } if (pOut->hdr.cbSize != (sizeof(CESERVER_REQ_HDR)+2*sizeof(DWORD)) || pOut->hdr.nCmd != pIn->hdr.nCmd) { ExecuteFreeResult(pOut); pOut = NULL; goto wrap; } ConEmuRoot = (HWND)pOut->dwData[0]; ConEmuHwnd = (HWND)pOut->dwData[1]; // Сервер не мог подцепиться БЕЗ создания мэппинга, поэтому CECMD_GETGUIHWND не должен был пройти успешно _ASSERTE(ConEmuRoot == NULL); ExecuteFreeResult(pOut); pOut = NULL; } #endif wrap: SetLastError(nLastErr); //if (pIn) // free(pIn); if (szGuiPipeName) free(szGuiPipeName); if (aiType == 2) return FarHwnd; else if (aiType == 0) return ConEmuHwnd; else // aiType == 1 return ConEmuRoot; }
//Arguments: // hConWnd - Хэндл КОНСОЛЬНОГО окна (по нему формируется имя пайпа для GUI) // pIn - выполняемая команда // nTimeout- таймаут подключения //Returns: // CESERVER_REQ. Его необходимо освободить через free(...); //WARNING!!! // Эта процедура не может получить с сервера более 600 байт данных! // В заголовке hOwner в дебаге может быть отображена ошибка CESERVER_REQ* ExecuteCmd(const wchar_t* szPipeName, CESERVER_REQ* pIn, DWORD nWaitPipe, HWND hOwner, BOOL bAsyncNoResult, DWORD nServerPID) { CESERVER_REQ* pOut = NULL; HANDLE hPipe = NULL; BYTE cbReadBuf[600]; // чтобы CESERVER_REQ_OUTPUTFILE поместился wchar_t szErr[MAX_PATH*2]; szErr[0] = 0; BOOL fSuccess = FALSE; DWORD cbRead = 0, /*dwMode = 0,*/ dwErr = 0; if (!pIn || !szPipeName) { _ASSERTE(pIn && szPipeName); return NULL; } pIn->hdr.bAsync = bAsyncNoResult; _ASSERTE(pIn->hdr.nSrcPID && pIn->hdr.nSrcThreadId); _ASSERTE(pIn->hdr.cbSize >= sizeof(pIn->hdr)); hPipe = ExecuteOpenPipe(szPipeName, szErr, NULL/*Сюда хорошо бы имя модуля подкрутить*/, nServerPID, nWaitPipe); if (hPipe == NULL || hPipe == INVALID_HANDLE_VALUE) { #ifdef _DEBUG dwErr = GetLastError(); // в заголовке "чисто" запущенного фара появляются отладочные(?) сообщения // по идее - не должны, т.к. все должно быть через мэппинг _ASSERTEX(hPipe != NULL && hPipe != INVALID_HANDLE_VALUE); #ifdef CONEMU_MINIMAL SetConsoleTitle(szErr); #else if (hOwner) { if (hOwner == myGetConsoleWindow()) SetConsoleTitle(szErr); else SetWindowText(hOwner, szErr); } #endif #endif return NULL; } //// Try to open a named pipe; wait for it, if necessary. //while (1) //{ // hPipe = CreateFile( // szPipeName, // pipe name // GENERIC_READ | // read and write access // GENERIC_WRITE, // 0, // no sharing // NULL, // default security attributes // OPEN_EXISTING, // opens existing pipe // 0, // default attributes // NULL); // no template file // // // Break if the pipe handle is valid. // if (hPipe != INVALID_HANDLE_VALUE) // break; // // // Exit if an error other than ERROR_PIPE_BUSY occurs. // dwErr = GetLastError(); // if (dwErr != ERROR_PIPE_BUSY) // { // return NULL; // } // // // All pipe instances are busy, so wait for 1 second. // if (!WaitNamedPipe(szPipeName, nWaitPipe) ) // { // return NULL; // } //} // //// The pipe connected; change to message-read mode. //dwMode = PIPE_READMODE_MESSAGE; //fSuccess = SetNamedPipeHandleState( // hPipe, // pipe handle // &dwMode, // new pipe mode // NULL, // don't set maximum bytes // NULL); // don't set maximum time //if (!fSuccess) //{ // CloseHandle(hPipe); // return NULL; //} _ASSERTE(pIn->hdr.nSrcThreadId==GetCurrentThreadId()); if (bAsyncNoResult) { // Если нас не интересует возврат и нужно сразу вернуться fSuccess = WriteFile(hPipe, pIn, pIn->hdr.cbSize, &cbRead, NULL); #ifdef _DEBUG dwErr = GetLastError(); _ASSERTE(fSuccess && (cbRead == pIn->hdr.cbSize)); #endif return NULL; } else { WARNING("При Overlapped часто виснет в этом месте."); // Send a message to the pipe server and read the response. fSuccess = TransactNamedPipe( hPipe, // pipe handle (LPVOID)pIn, // message to server pIn->hdr.cbSize, // message length cbReadBuf, // buffer to receive reply sizeof(cbReadBuf), // size of read buffer &cbRead, // bytes read NULL); // not overlapped dwErr = GetLastError(); //CloseHandle(hPipe); if (!fSuccess && (dwErr != ERROR_MORE_DATA)) { //_ASSERTE(fSuccess || (dwErr == ERROR_MORE_DATA)); CloseHandle(hPipe); return NULL; } } if (cbRead < sizeof(CESERVER_REQ_HDR)) { CloseHandle(hPipe); return NULL; } pOut = (CESERVER_REQ*)cbReadBuf; // temporary if (pOut->hdr.cbSize < cbRead) { CloseHandle(hPipe); if (pOut->hdr.cbSize) { _ASSERTE(pOut->hdr.cbSize == 0 || pOut->hdr.cbSize >= cbRead); DEBUGSTR(L"!!! Wrong nSize received from GUI server !!!\n"); } return NULL; } if (pOut->hdr.nVersion != CESERVER_REQ_VER) { CloseHandle(hPipe); DEBUGSTR(L"!!! Wrong nVersion received from GUI server !!!\n"); return NULL; } int nAllSize = pOut->hdr.cbSize; pOut = (CESERVER_REQ*)malloc(nAllSize); _ASSERTE(pOut); if (!pOut) { CloseHandle(hPipe); return NULL; } memmove(pOut, cbReadBuf, cbRead); LPBYTE ptrData = ((LPBYTE)pOut)+cbRead; nAllSize -= cbRead; while (nAllSize>0) { // Break if TransactNamedPipe or ReadFile is successful if (fSuccess) break; // Read from the pipe if there is more data in the message. fSuccess = ReadFile( hPipe, // pipe handle ptrData, // buffer to receive reply nAllSize, // size of buffer &cbRead, // number of bytes read NULL); // not overlapped // Exit if an error other than ERROR_MORE_DATA occurs. if (!fSuccess && (GetLastError() != ERROR_MORE_DATA)) break; ptrData += cbRead; nAllSize -= cbRead; } CloseHandle(hPipe); if (pOut && (pOut->hdr.nCmd != pIn->hdr.nCmd)) { _ASSERTE(pOut->hdr.nCmd == pIn->hdr.nCmd); if (pOut->hdr.nCmd == 0) { ExecuteFreeResult(pOut); pOut = NULL; } } return pOut; }
//// Эта функция пайп не закрывает! //void CGuiServer::GuiServerThreadCommand(HANDLE hPipe) BOOL CGuiServer::GuiServerCommand(LPVOID pInst, CESERVER_REQ* pIn, CESERVER_REQ* &ppReply, DWORD &pcbReplySize, DWORD &pcbMaxReplySize, LPARAM lParam) { BOOL lbRc = FALSE; CGuiServer* pGSrv = (CGuiServer*)lParam; if (!pGSrv) { _ASSERTE(((CGuiServer*)lParam)!=NULL); pGSrv = &gpConEmu->m_GuiServer; } if (pIn->hdr.bAsync) pGSrv->mp_GuiServer->BreakConnection(pInst); gpSetCls->debugLogCommand(pIn, TRUE, timeGetTime(), 0, pGSrv ? pGSrv->ms_ServerPipe : NULL); #ifdef _DEBUG UINT nDataSize = pIn->hdr.cbSize - sizeof(CESERVER_REQ_HDR); #endif // Все данные из пайпа получены, обрабатываем команду и возвращаем (если нужно) результат #ifdef ALLOW_WINE_MSG if (gbIsWine) { wchar_t szMsg[128]; msprintf(szMsg, countof(szMsg), L"CGuiServer::GuiServerCommand.\nGUI TID=%u\nSrcPID=%u, SrcTID=%u, Cmd=%u", GetCurrentThreadId(), pIn->hdr.nSrcPID, pIn->hdr.nSrcThreadId, pIn->hdr.nCmd); MessageBox(szMsg, MB_ICONINFORMATION); } #endif switch (pIn->hdr.nCmd) { case CECMD_NEWCMD: { // Приходит из другой копии ConEmu.exe, когда она запущена с ключом /single, /showhide, /showhideTSA DEBUGSTRCMD(L"GUI recieved CECMD_NEWCMD\n"); LPCWSTR pszCommand = pIn->NewCmd.GetCommand(); _ASSERTE(pszCommand!=NULL && "Must be at least empty string but NOT NULL"); if (pIn->NewCmd.isAdvLogging && !gpSetCls->isAdvLogging) { gpSetCls->isAdvLogging = pIn->NewCmd.isAdvLogging; gpConEmu->CreateLog(); } if (gpSetCls->isAdvLogging && (pIn->NewCmd.isAdvLogging > gpSetCls->isAdvLogging)) { wchar_t szLogLevel[80]; _wsprintf(szLogLevel, SKIPLEN(countof(szLogLevel)) L"Changing log level! Old=%u, New=%u", (UINT)gpSetCls->isAdvLogging, (UINT)pIn->NewCmd.isAdvLogging); gpConEmu->LogString(szLogLevel); gpSetCls->isAdvLogging = pIn->NewCmd.isAdvLogging; } if (gpSetCls->isAdvLogging) { size_t cchAll = 120 + _tcslen(pIn->NewCmd.szConEmu) + _tcslen(pIn->NewCmd.szCurDir) + _tcslen(pszCommand); wchar_t* pszInfo = (wchar_t*)malloc(cchAll*sizeof(*pszInfo)); if (pszInfo) { _wsprintf(pszInfo, SKIPLEN(cchAll) L"CECMD_NEWCMD: Wnd=x%08X, Act=%u, ConEmu=%s, Dir=%s, Cmd=%s", (DWORD)(DWORD_PTR)pIn->NewCmd.hFromConWnd, pIn->NewCmd.ShowHide, pIn->NewCmd.szConEmu, pIn->NewCmd.szCurDir, pszCommand); gpConEmu->LogString(pszInfo); free(pszInfo); } } BOOL bAccepted = FALSE; if (pIn->NewCmd.szConEmu[0]) { bAccepted = (lstrcmpi(gpConEmu->ms_ConEmuExeDir, pIn->NewCmd.szConEmu) == 0); } else { bAccepted = TRUE; } if (bAccepted) { bool bCreateTab = (pIn->NewCmd.ShowHide == sih_None || pIn->NewCmd.ShowHide == sih_StartDetached) // Issue 1275: When minimized into TSA (on all VCon are closed) we need to restore and run new tab || (pszCommand[0] && !CVConGroup::isVConExists(0)); RConStartArgs rTest; if (pszCommand[0]) { rTest.pszSpecialCmd = lstrdup(pszCommand); rTest.ProcessNewConArg(); } bool bSkipActivation = false; // Может быть пусто if (bCreateTab && pszCommand[0]) { RConStartArgs *pArgs = new RConStartArgs; // New tab must be started with same credentials that calling tab if others was not specified { if (!rTest.HasInheritedArgs()) { CVConGuard VCon; if ((pIn->NewCmd.hFromConWnd || pIn->NewCmd.hFromDcWnd) && CVConGroup::GetVConByHWND(pIn->NewCmd.hFromConWnd, pIn->NewCmd.hFromDcWnd, &VCon)) { const RConStartArgs& r = VCon->RCon()->GetArgs(); if (r.HasInheritedArgs()) { pArgs->AssignInheritedArgs(&r); } } } } pArgs->Detached = (pIn->NewCmd.ShowHide == sih_StartDetached) ? crb_On : crb_Off; pArgs->pszSpecialCmd = lstrdup(pszCommand); if (pIn->NewCmd.szCurDir[0] == 0) { _ASSERTE(pIn->NewCmd.szCurDir[0] != 0); } else { pArgs->pszStartupDir = lstrdup(pIn->NewCmd.szCurDir); } LPCWSTR pszStrings = pIn->NewCmd.GetEnvStrings(); if (pszStrings && pIn->NewCmd.cchEnvStrings) { size_t cbBytes = pIn->NewCmd.cchEnvStrings*sizeof(*pArgs->pszEnvStrings); pArgs->pszEnvStrings = (wchar_t*)malloc(cbBytes); if (pArgs->pszEnvStrings) { memmove(pArgs->pszEnvStrings, pszStrings, cbBytes); pArgs->cchEnvStrings = pIn->NewCmd.cchEnvStrings; } } if (gpSetCls->IsMulti() || CVConGroup::isDetached()) { gpConEmu->PostCreateCon(pArgs); } else { // Если хотят в одном окне - только одну консоль gpConEmu->CreateWnd(pArgs); SafeDelete(pArgs); // New window created? Don't activate this one. bSkipActivation = true; } } else { _ASSERTE(pIn->NewCmd.ShowHide==sih_ShowMinimize || pIn->NewCmd.ShowHide==sih_ShowHideTSA || pIn->NewCmd.ShowHide==sih_Show); } // gh#151: Do animation after starting tab creation if (!bSkipActivation && (rTest.BackgroundTab != crb_On)) { gpConEmu->DoMinimizeRestore(bCreateTab ? sih_SetForeground : pIn->NewCmd.ShowHide); } } pcbReplySize = sizeof(CESERVER_REQ_HDR)+sizeof(BYTE); lbRc = ExecuteNewCmd(ppReply, pcbMaxReplySize, pIn->hdr.nCmd, pcbReplySize); if (lbRc) { ppReply->Data[0] = bAccepted; } break; } //CECMD_NEWCMD case CECMD_TABSCMD: { // 0: спрятать/показать табы, 1: перейти на следующую, 2: перейти на предыдущую, 3: commit switch DEBUGSTRCMD(L"GUI recieved CECMD_TABSCMD\n"); _ASSERTE(nDataSize>=1); DWORD nTabCmd = pIn->Data[0]; gpConEmu->TabCommand((ConEmuTabCommand)nTabCmd); pcbReplySize = sizeof(CESERVER_REQ_HDR)+sizeof(BYTE); if (ExecuteNewCmd(ppReply, pcbMaxReplySize, pIn->hdr.nCmd, pcbReplySize)) { lbRc = TRUE; ppReply->Data[0] = TRUE; } break; } // CECMD_TABSCMD case CECMD_ATTACH2GUI: { // Получен запрос на Attach из сервера MCHKHEAP; CESERVER_REQ_SRVSTARTSTOPRET Ret = {}; lbRc = CVConGroup::AttachRequested(pIn->StartStop.hWnd, &(pIn->StartStop), Ret); pcbReplySize = sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_SRVSTARTSTOPRET)+(Ret.cchEnvCommands*sizeof(wchar_t)); if (!ExecuteNewCmd(ppReply, pcbMaxReplySize, pIn->hdr.nCmd, pcbReplySize)) { SafeFree(Ret.pszCommands); goto wrap; } if (lbRc) { _ASSERTE(sizeof(ppReply->SrvStartStopRet) == sizeof(Ret)); memmove(&ppReply->SrvStartStopRet, &Ret, sizeof(Ret)); // Environment strings (inherited from parent console) if (Ret.cchEnvCommands && Ret.pszCommands) { memmove(ppReply->SrvStartStopRet.szCommands, Ret.pszCommands, Ret.cchEnvCommands*sizeof(wchar_t)); ppReply->SrvStartStopRet.cchEnvCommands = Ret.cchEnvCommands; } else { ppReply->SrvStartStopRet.cchEnvCommands = 0; } SafeFree(Ret.pszCommands); _ASSERTE((ppReply->StartStopRet.nBufferHeight == 0) || ((int)ppReply->StartStopRet.nBufferHeight > (pIn->StartStop.sbi.srWindow.Bottom-pIn->StartStop.sbi.srWindow.Top))); } MCHKHEAP; break; } // CECMD_ATTACH2GUI case CECMD_SRVSTARTSTOP: { MCHKHEAP; // SRVSTART не приходит если запускается cmd под админом или из Win+G bool lbAllocated = false; if (pIn->SrvStartStop.Started == srv_Started) { // Запущен процесс сервера HWND hConWnd = (HWND)pIn->dwData[1]; _ASSERTE(hConWnd && IsWindow(hConWnd)); DWORD nStartTick = timeGetTime(); struct MsgSrvStartedArg { HWND hConWnd; DWORD nSrcPID; DWORD dwKeybLayout; DWORD timeStart; DWORD timeRecv; DWORD timeFin; CESERVER_REQ_SRVSTARTSTOPRET Ret; //111002 - вернуть должен HWND окна отрисовки (дочернее окно ConEmu) static LRESULT OnSrvStarted(LPARAM lParam) { MsgSrvStartedArg *pArg = (MsgSrvStartedArg*)lParam; HWND hWndDC = NULL; DWORD nServerPID = pArg->nSrcPID; HWND hWndCon = pArg->hConWnd; DWORD dwKeybLayout = pArg->dwKeybLayout; pArg->timeRecv = timeGetTime(); DWORD t1, t2; int iFound = -1; hWndDC = CVConGroup::DoSrvCreated(nServerPID, hWndCon, dwKeybLayout, t1, t2, iFound, pArg->Ret); UNREFERENCED_PARAMETER(dwKeybLayout); UNREFERENCED_PARAMETER(hWndCon); pArg->timeFin = timeGetTime(); if (hWndDC == NULL) { _ASSERTE(hWndDC!=NULL); } else { #ifdef _DEBUG DWORD nRecvDur = pArg->timeRecv - pArg->timeStart; DWORD nProcDur = pArg->timeFin - pArg->timeRecv; #define MSGSTARTED_TIMEOUT 10000 if ((nRecvDur > MSGSTARTED_TIMEOUT) || (nProcDur > MSGSTARTED_TIMEOUT)) { _ASSERTE((nRecvDur <= MSGSTARTED_TIMEOUT) && (nProcDur <= MSGSTARTED_TIMEOUT)); } #endif } return (LRESULT)hWndDC; }; } arg = {hConWnd, pIn->hdr.nSrcPID, pIn->SrvStartStop.dwKeybLayout, nStartTick}; gpConEmu->CallMainThread(true, arg.OnSrvStarted, (LPARAM)&arg); HWND hWndDC = arg.Ret.Info.hWndDc; HWND hWndBack = arg.Ret.Info.hWndBack; _ASSERTE(hWndDC!=NULL); #ifdef _DEBUG DWORD dwErr = GetLastError(), nEndTick = timeGetTime(), nDelta = nEndTick - nStartTick; if (hWndDC && nDelta >= EXECUTE_CMD_WARN_TIMEOUT) { if (!IsDebuggerPresent()) { //_ASSERTE(nDelta <= EXECUTE_CMD_WARN_TIMEOUT || (pIn->hdr.nCmd == CECMD_CMDSTARTSTOP && nDelta <= EXECUTE_CMD_WARN_TIMEOUT2)); _ASSERTEX(nDelta <= EXECUTE_CMD_WARN_TIMEOUT); } } #endif pcbReplySize = sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_SRVSTARTSTOPRET)+(arg.Ret.cchEnvCommands*sizeof(wchar_t)); if (!ExecuteNewCmd(ppReply, pcbMaxReplySize, pIn->hdr.nCmd, pcbReplySize)) { SafeFree(arg.Ret.pszCommands); goto wrap; } lbAllocated = true; _ASSERTE(sizeof(ppReply->SrvStartStopRet) == sizeof(arg.Ret)); memmove(&ppReply->SrvStartStopRet, &arg.Ret, sizeof(arg.Ret)); // Environment strings (inherited from parent console) if (arg.Ret.cchEnvCommands && arg.Ret.pszCommands) { memmove(ppReply->SrvStartStopRet.szCommands, arg.Ret.pszCommands, arg.Ret.cchEnvCommands*sizeof(wchar_t)); ppReply->SrvStartStopRet.cchEnvCommands = arg.Ret.cchEnvCommands; } else { ppReply->SrvStartStopRet.cchEnvCommands = 0; } SafeFree(arg.Ret.pszCommands); } else if (pIn->SrvStartStop.Started == srv_Stopped) { // Процесс сервера завершается CRealConsole* pRCon = NULL; CVConGuard VCon; for (size_t i = 0;; i++) { if (!CVConGroup::GetVCon(i, &VCon)) break; pRCon = VCon->RCon(); if (pRCon && (pRCon->GetServerPID(true) == pIn->hdr.nSrcPID || pRCon->GetServerPID(false) == pIn->hdr.nSrcPID)) { break; } pRCon = NULL; } gpConEmu->mn_ShellExitCode = pIn->SrvStartStop.nShellExitCode; if (pRCon) pRCon->OnServerClosing(pIn->hdr.nSrcPID, &pIn->SrvStartStop.nShellExitCode); //pIn->dwData[0] = 1; } else { _ASSERTE((pIn->dwData[0] == 1) || (pIn->dwData[0] == 101)); } MCHKHEAP; if (!lbAllocated) { pcbReplySize = sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_SRVSTARTSTOPRET); if (!ExecuteNewCmd(ppReply, pcbMaxReplySize, pIn->hdr.nCmd, pcbReplySize)) goto wrap; } lbRc = TRUE; //// Отправляем //fSuccess = WriteFile( // hPipe, // handle to pipe // pOut, // buffer to write from // pOut->hdr.cbSize, // number of bytes to write // &cbWritten, // number of bytes written // NULL); // not overlapped I/O //ExecuteFreeResult(pOut); break; } // CECMD_SRVSTARTSTOP case CECMD_ASSERT: { DWORD nBtn = MessageBox(NULL, pIn->AssertInfo.szDebugInfo, pIn->AssertInfo.szTitle, pIn->AssertInfo.nBtns); pcbReplySize = sizeof(CESERVER_REQ_HDR)+sizeof(DWORD); if (ExecuteNewCmd(ppReply, pcbMaxReplySize, pIn->hdr.nCmd, pcbReplySize)) { lbRc = TRUE; ppReply->dwData[0] = nBtn; } //ExecutePrepareCmd(&pIn->hdr, CECMD_ASSERT, sizeof(CESERVER_REQ_HDR) + sizeof(DWORD)); //pIn->dwData[0] = nBtn; //// Отправляем //fSuccess = WriteFile( // hPipe, // handle to pipe // pIn, // buffer to write from // pIn->hdr.cbSize, // number of bytes to write // &cbWritten, // number of bytes written // NULL); // not overlapped I/O break; } // CECMD_ASSERT case CECMD_ATTACHGUIAPP: { pcbReplySize = sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_ATTACHGUIAPP); if (!ExecuteNewCmd(ppReply, pcbMaxReplySize, pIn->hdr.nCmd, pcbReplySize)) goto wrap; ppReply->AttachGuiApp = pIn->AttachGuiApp; //CESERVER_REQ Out; //ExecutePrepareCmd(&Out.hdr, CECMD_ATTACHGUIAPP, sizeof(CESERVER_REQ_HDR)+sizeof(Out.AttachGuiApp)); //Out.AttachGuiApp = pIn->AttachGuiApp; #ifdef SHOW_GUIATTACH_START if (pIn->AttachGuiApp.hWindow == NULL) { wchar_t szDbg[1024]; _wsprintf(szDbg, SKIPLEN(countof(szDbg)) L"AttachGuiApp requested from:\n%s\nPID=%u", pIn->AttachGuiApp.sAppFilePathName, pIn->AttachGuiApp.nPID); //MBoxA(szDbg); MessageBox(NULL, szDbg, L"ConEmu", MB_SYSTEMMODAL); } #endif // Уведомить ожидающую вкладку CRealConsole* pRCon = CVConGroup::AttachRequestedGui(pIn->AttachGuiApp.nServerPID, pIn->AttachGuiApp.sAppFilePathName, pIn->AttachGuiApp.nPID); if (pRCon) { CVConGuard VCon(pRCon->VCon()); RECT rcPrev = ppReply->AttachGuiApp.rcWindow; HWND hBack = pRCon->VCon()->GetBack(); //// Размер должен быть независим от возможности наличия прокрутки в VCon //GetWindowRect(hBack, &ppReply->AttachGuiApp.rcWindow); //ppReply->AttachGuiApp.rcWindow.right -= ppReply->AttachGuiApp.rcWindow.left; //ppReply->AttachGuiApp.rcWindow.bottom -= ppReply->AttachGuiApp.rcWindow.top; //ppReply->AttachGuiApp.rcWindow.left = ppReply->AttachGuiApp.rcWindow.top = 0; ////MapWindowPoints(NULL, hBack, (LPPOINT)&ppReply->AttachGuiApp.rcWindow, 2); //pRCon->CorrectGuiChildRect(ppReply->AttachGuiApp.nStyle, ppReply->AttachGuiApp.nStyleEx, ppReply->AttachGuiApp.rcWindow); // Уведомить RCon и ConEmuC, что гуй подцепился // Вызывается два раза. Первый (при запуске exe) ahGuiWnd==NULL, второй - после фактического создания окна pRCon->SetGuiMode(pIn->AttachGuiApp.nFlags, pIn->AttachGuiApp.hAppWindow, pIn->AttachGuiApp.Styles.nStyle, pIn->AttachGuiApp.Styles.nStyleEx, pIn->AttachGuiApp.sAppFilePathName, pIn->AttachGuiApp.nPID, pIn->hdr.nBits, rcPrev); ppReply->AttachGuiApp.nFlags = agaf_Success | (pRCon->isActive(false) ? 0 : agaf_Inactive); ppReply->AttachGuiApp.nServerPID = pRCon->GetServerPID(); ppReply->AttachGuiApp.nPID = ppReply->AttachGuiApp.nServerPID; ppReply->AttachGuiApp.hConEmuDc = pRCon->GetView(); ppReply->AttachGuiApp.hConEmuBack = hBack; ppReply->AttachGuiApp.hConEmuWnd = ghWnd; ppReply->AttachGuiApp.hAppWindow = pIn->AttachGuiApp.hAppWindow; ppReply->AttachGuiApp.hSrvConWnd = pRCon->ConWnd(); ppReply->AttachGuiApp.hkl = (DWORD)(LONG)(LONG_PTR)GetKeyboardLayout(gpConEmu->mn_MainThreadId); ZeroStruct(ppReply->AttachGuiApp.Styles.Shifts); CRealConsole::CorrectGuiChildRect(pIn->AttachGuiApp.Styles.nStyle, pIn->AttachGuiApp.Styles.nStyleEx, ppReply->AttachGuiApp.Styles.Shifts, pIn->AttachGuiApp.sAppFilePathName); } else { ppReply->AttachGuiApp.nFlags = agaf_Fail; _ASSERTE(FALSE && "No one tab is waiting for ChildGui process"); } lbRc = TRUE; //// Отправляем //fSuccess = WriteFile( // hPipe, // handle to pipe // &Out, // buffer to write from // Out.hdr.cbSize, // number of bytes to write // &cbWritten, // number of bytes written // NULL); // not overlapped I/O break; } // CECMD_ATTACHGUIAPP case CECMD_GUICLIENTSHIFT: { pcbReplySize = sizeof(CESERVER_REQ_HDR)+sizeof(GuiStylesAndShifts); if (!ExecuteNewCmd(ppReply, pcbMaxReplySize, pIn->hdr.nCmd, pcbReplySize)) goto wrap; ppReply->GuiAppShifts = pIn->GuiAppShifts; ZeroStruct(ppReply->GuiAppShifts.Shifts); CRealConsole::CorrectGuiChildRect(pIn->GuiAppShifts.nStyle, pIn->GuiAppShifts.nStyleEx, ppReply->GuiAppShifts.Shifts, pIn->GuiAppShifts.szExeName); lbRc = TRUE; break; } // CECMD_GUICLIENTSHIFT case CECMD_GUIMACRO: { // Допустимо, если GuiMacro пытаются выполнить извне CVConGuard VCon; CVConGroup::GetActiveVCon(&VCon); DWORD nFarPluginPID = VCon->RCon()->GetFarPID(true); LPWSTR pszResult = ConEmuMacro::ExecuteMacro(pIn->GuiMacro.sMacro, VCon->RCon(), (nFarPluginPID==pIn->hdr.nSrcPID), &pIn->GuiMacro); int nLen = pszResult ? _tcslen(pszResult) : 0; pcbReplySize = sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_GUIMACRO)+nLen*sizeof(wchar_t); if (!ExecuteNewCmd(ppReply, pcbMaxReplySize, pIn->hdr.nCmd, pcbReplySize)) { SafeFree(pszResult); goto wrap; } if (pszResult) { lstrcpy(ppReply->GuiMacro.sMacro, pszResult); ppReply->GuiMacro.nSucceeded = 1; free(pszResult); } else { ppReply->GuiMacro.sMacro[0] = 0; ppReply->GuiMacro.nSucceeded = 0; } lbRc = TRUE; break; } // CECMD_GUIMACRO case CECMD_CMDSTARTSTOP: { CRealServer* pRSrv = NULL; CVConGuard VCon; DWORD nSrvPID = pIn->hdr.nSrcPID; DWORD nMonitorTID = (pIn->DataSize() >= sizeof(pIn->StartStop)) ? pIn->StartStop.dwAID : 0; if (CVConGroup::GetVConBySrvPID(nSrvPID, nMonitorTID, &VCon)) pRSrv = &VCon->RCon()->m_RConServer; if (pRSrv) { CESERVER_REQ* pOut = pRSrv->cmdStartStop(pInst, pIn, pIn->DataSize()); if (pOut) { DWORD nDataSize = pOut->DataSize(); pcbReplySize = pOut->hdr.cbSize; if (ExecuteNewCmd(ppReply, pcbMaxReplySize, pOut->hdr.nCmd, pcbReplySize)) { if (nDataSize > 0) { memmove(ppReply->Data, pOut->Data, nDataSize); } lbRc = TRUE; } ExecuteFreeResult(pOut); } } break; } // CECMD_CMDSTARTSTOP //case CECMD_DEFTERMSTARTED: //{ // if (gpConEmu->mp_DefTrm) // gpConEmu->mp_DefTrm->OnDefTermStarted(pIn); // pcbReplySize = sizeof(CESERVER_REQ_HDR); // if (!ExecuteNewCmd(ppReply, pcbMaxReplySize, pIn->hdr.nCmd, pcbReplySize)) // goto wrap; // lbRc = TRUE; // break; //} // CECMD_DEFTERMSTARTED default: _ASSERTE(FALSE && "Command was not handled in CGuiServer::GuiServerCommand"); } //// Освободить память //if (pIn && (LPVOID)pIn != (LPVOID)&in) //{ // free(pIn); pIn = NULL; //} wrap: return lbRc; }
// Do the attach procedure for the requested process bool CAttachDlg::StartAttach(HWND ahAttachWnd, DWORD anPID, DWORD anBits, AttachProcessType anType, BOOL abAltMode) { bool lbRc = false; wchar_t szPipe[MAX_PATH]; PROCESS_INFORMATION pi = {}; STARTUPINFO si = {sizeof(si)}; SHELLEXECUTEINFO sei = {sizeof(sei)}; CESERVER_REQ *pIn = NULL, *pOut = NULL; HANDLE hPipeTest = NULL; HANDLE hPluginTest = NULL; HANDLE hProcTest = NULL; DWORD nErrCode = 0; bool lbCreate; CESERVER_CONSOLE_MAPPING_HDR srv; DWORD nWrapperWait = -1; DWORD nWrapperResult = -1; if (!ahAttachWnd || !anPID || !anBits || !anType) { MBoxAssert(ahAttachWnd && anPID && anBits && anType); goto wrap; } if (gpSetCls->isAdvLogging) { wchar_t szInfo[128]; _wsprintf(szInfo, SKIPLEN(countof(szInfo)) L"CAttachDlg::StartAttach HWND=x%08X, PID=%u, Bits%u, Type=%u, AltMode=%u", (DWORD)(DWORD_PTR)ahAttachWnd, anPID, anBits, (UINT)anType, abAltMode); gpConEmu->LogString(szInfo); } if (LoadSrvMapping(ahAttachWnd, srv)) { pIn = ExecuteNewCmd(CECMD_ATTACH2GUI, sizeof(CESERVER_REQ_HDR)); pOut = ExecuteSrvCmd(srv.nServerPID, pIn, ghWnd); if (pOut && (pOut->hdr.cbSize >= (sizeof(CESERVER_REQ_HDR)+sizeof(DWORD))) && (pOut->dwData[0] != 0)) { // Our console server had been already started // and we successfully have completed the attach lbRc = true; goto wrap; } ExecuteFreeResult(pIn); ExecuteFreeResult(pOut); } // Is it a Far Manager with our ConEmu.dll plugin loaded? _wsprintf(szPipe, SKIPLEN(countof(szPipe)) CEPLUGINPIPENAME, L".", anPID); hPluginTest = CreateFile(szPipe, GENERIC_READ|GENERIC_WRITE, 0, LocalSecurity(), OPEN_EXISTING, 0, NULL); if (hPluginTest && hPluginTest != INVALID_HANDLE_VALUE) { CloseHandle(hPluginTest); goto DoPluginCall; } // May be there is already ConEmuHk[64].dll loaded? Either it is already in the another ConEmu VCon? _wsprintf(szPipe, SKIPLEN(countof(szPipe)) CEHOOKSPIPENAME, L".", anPID); hPipeTest = CreateFile(szPipe, GENERIC_READ|GENERIC_WRITE, 0, LocalSecurity(), OPEN_EXISTING, 0, NULL); if (hPipeTest && hPipeTest != INVALID_HANDLE_VALUE) { CloseHandle(hPipeTest); goto DoExecute; } wchar_t szSrv[MAX_PATH+64], szArgs[128]; wcscpy_c(szSrv, gpConEmu->ms_ConEmuBaseDir); wcscat_c(szSrv, (anBits==64) ? L"\\ConEmuC64.exe" : L"\\ConEmuC.exe"); if (abAltMode && (anType == apt_Console)) { _wsprintf(szArgs, SKIPLEN(countof(szArgs)) L" /ATTACH /CONPID=%u /GID=%u /GHWND=%08X", anPID, GetCurrentProcessId(), LODWORD(ghWnd)); } else { _wsprintf(szArgs, SKIPLEN(countof(szArgs)) L" /INJECT=%u", anPID); abAltMode = FALSE; } si.dwFlags = STARTF_USESHOWWINDOW; si.wShowWindow = SW_HIDE; if (anType == apt_Gui) { gpConEmu->CreateGuiAttachMapping(anPID); } hProcTest = OpenProcess(PROCESS_CREATE_THREAD|PROCESS_QUERY_INFORMATION|PROCESS_VM_OPERATION|PROCESS_VM_WRITE|PROCESS_VM_READ, FALSE, anPID); // If the attaching process is running as admin (elevated) we have to run ConEmuC as admin too if (hProcTest == NULL) { nErrCode = GetLastError(); MBoxAssert(hProcTest!=NULL || nErrCode==ERROR_ACCESS_DENIED); sei.hwnd = ghWnd; sei.fMask = (abAltMode ? 0 : SEE_MASK_NO_CONSOLE)|SEE_MASK_NOCLOSEPROCESS|SEE_MASK_NOASYNC; sei.lpVerb = L"runas"; sei.lpFile = szSrv; sei.lpParameters = szArgs; sei.lpDirectory = gpConEmu->ms_ConEmuBaseDir; sei.nShow = SW_SHOWMINIMIZED; lbCreate = ShellExecuteEx(&sei); if (lbCreate) { MBoxAssert(sei.hProcess!=NULL); pi.hProcess = sei.hProcess; } } else { // Normal start DWORD dwFlags = 0 | (abAltMode ? CREATE_NO_WINDOW : CREATE_NEW_CONSOLE) | CREATE_DEFAULT_ERROR_MODE | NORMAL_PRIORITY_CLASS; lbCreate = CreateProcess(szSrv, szArgs, NULL, NULL, FALSE, dwFlags, NULL, NULL, &si, &pi); } if (!lbCreate) { wchar_t szErrMsg[MAX_PATH+255], szTitle[128]; DWORD dwErr = GetLastError(); _wsprintf(szErrMsg, SKIPLEN(countof(szErrMsg)) L"Can't start %s server\n%s %s", abAltMode ? L"injection" : L"console", szSrv, szArgs); _wsprintf(szTitle, SKIPLEN(countof(szTitle)) L"ConEmu Attach, PID=%u, TID=%u", GetCurrentProcessId(), GetCurrentThreadId()); DisplayLastError(szErrMsg, dwErr, 0, szTitle); goto wrap; } if (abAltMode) { lbRc = true; goto wrap; } nWrapperWait = WaitForSingleObject(pi.hProcess, INFINITE); nWrapperResult = -1; GetExitCodeProcess(pi.hProcess, &nWrapperResult); CloseHandle(pi.hProcess); if (pi.hThread) CloseHandle(pi.hThread); if (((int)nWrapperResult != CERR_HOOKS_WAS_SET) && ((int)nWrapperResult != CERR_HOOKS_WAS_ALREADY_SET)) { goto wrap; } DoExecute: // Not the attaching process has our ConEmuHk[64].dll loaded // and we can request to start console server for that console or ChildGui pIn = ExecuteNewCmd(CECMD_STARTSERVER, sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_START)); pIn->NewServer.nGuiPID = GetCurrentProcessId(); pIn->NewServer.hGuiWnd = ghWnd; if (anType == apt_Gui) { _ASSERTE(ahAttachWnd && IsWindow(ahAttachWnd)); pIn->NewServer.hAppWnd = ahAttachWnd; } goto DoPipeCall; DoPluginCall: // Ask Far Manager plugin to do the attach pIn = ExecuteNewCmd(CECMD_ATTACH2GUI, sizeof(CESERVER_REQ_HDR)); goto DoPipeCall; DoPipeCall: pOut = ExecuteCmd(szPipe, pIn, 500, ghWnd); if (!pOut || (pOut->hdr.cbSize < pIn->hdr.cbSize) || (pOut->dwData[0] == 0)) { _ASSERTE(pOut && pOut->hdr.cbSize == (sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_START))); wchar_t szMsg[255], szTitle[128]; wcscpy_c(szMsg, L"Failed to start console server in the remote process"); if (hPluginTest && hPluginTest != INVALID_HANDLE_VALUE) wcscat_c(szMsg, L"\nFar ConEmu plugin was loaded"); if (hPipeTest && hPipeTest != INVALID_HANDLE_VALUE) wcscat_c(szMsg, L"\nHooks already were set"); _wsprintf(szTitle, SKIPLEN(countof(szTitle)) L"ConEmu Attach, PID=%u, TID=%u", GetCurrentProcessId(), GetCurrentThreadId()); DisplayLastError(szMsg, (pOut && (pOut->hdr.cbSize >= pIn->hdr.cbSize)) ? pOut->dwData[1] : -1, 0, szTitle); goto wrap; } lbRc = true; wrap: SafeCloseHandle(hProcTest); UNREFERENCED_PARAMETER(nErrCode); UNREFERENCED_PARAMETER(nWrapperWait); ExecuteFreeResult(pIn); ExecuteFreeResult(pOut); return lbRc; }
// Due to Microsoft bug we need to lock Server reading thread to avoid crash of kernel // http://conemu.github.io/en/MicrosoftBugs.html#Exception_in_ReadConsoleOutput void LockServerReadingThread(bool bLock, COORD dwSize, CESERVER_REQ*& pIn, CESERVER_REQ*& pOut) { DWORD nServerPID = gnServerPID; if (!nServerPID) return; DWORD nErrSave = GetLastError(); if (bLock) { HANDLE hOurThreadHandle = NULL; // We need to give our thread handle (to server process) to avoid // locking of server reading thread (in case of our thread fails) DWORD dwErr = -1; HANDLE hServer = OpenProcess(PROCESS_DUP_HANDLE, FALSE, nServerPID); if (!hServer) { dwErr = GetLastError(); _ASSERTEX(hServer!=NULL && "Open server handle fails, Can't dup handle!"); } else { if (!DuplicateHandle(GetCurrentProcess(), GetCurrentThread(), hServer, &hOurThreadHandle, SYNCHRONIZE|THREAD_QUERY_INFORMATION, FALSE, 0)) { dwErr = GetLastError(); _ASSERTEX(hServer!=NULL && "DuplicateHandle fails, Can't dup handle!"); hOurThreadHandle = NULL; } CloseHandle(hServer); } pIn = ExecuteNewCmd(CECMD_SETCONSCRBUF, sizeof(CESERVER_REQ_HDR)+sizeof(CESERVER_REQ_SETCONSCRBUF)); if (pIn) { pIn->SetConScrBuf.bLock = TRUE; pIn->SetConScrBuf.dwSize = dwSize; // Informational pIn->SetConScrBuf.hRequestor = hOurThreadHandle; } } else { if (pIn) { pIn->SetConScrBuf.bLock = FALSE; } } if (pIn) { ExecuteFreeResult(pOut); pOut = ExecuteSrvCmd(nServerPID, pIn, ghConWnd); if (pOut && pOut->DataSize() >= sizeof(CESERVER_REQ_SETCONSCRBUF)) { pIn->SetConScrBuf.hTemp = pOut->SetConScrBuf.hTemp; } } if (!bLock) { ExecuteFreeResult(pIn); ExecuteFreeResult(pOut); } // Transparently to calling function... SetLastError(nErrSave); }