Last updated:
31. October 2001
(Start sub-menu)
(End sub-menu)
/* * $Header: /Book/activateOldInstance.cpp 12 11.07.01 14:26 Oslph312 $ * * activateOldInstance has one weakness -- it will only catch * "real" instances, which have an editing window open. If a * previous instance was started with the /p switch, we won't * catch it here. */ #include "precomp.h" #include "activateOldInstance.h" #include "main_class.h" #include "winUtils.h" #include "addAtom.h" #include "resource.h" /** * This struct is used to pass information to the enumProc below. * We use an ATOM rather than a string to identify the document, * as strings can't be sent across Win32 process boundaries. * Discussion: WM_COPYDATA? */ struct EnumStruct { HWND hwnd; ATOM aDocName; }; /** * Callback function for EnumWindows. */ PRIVATE BOOL CALLBACK enumProc( HWND hwnd, LPARAM lParam ) { if ( isClass( hwnd, MAIN_CLASS ) ) { EnumStruct *pEnumStruct = reinterpret_cast< EnumStruct * >( lParam ); DWORD dwResult = 0; const LRESULT lResult = SendMessageTimeout( hwnd, WM_APP, 0, pEnumStruct->aDocName, SMTO_ABORTIFHUNG, 3000, &dwResult ); if ( 0 != lResult && 0 != dwResult ) { pEnumStruct->hwnd = hwnd; return FALSE; // Stop enumerating windows. } } return TRUE; // Continue enumerating windows. } /** * Loop over all existing TextEdit instances * and ask if they have this document: */ bool activateOldInstance( LPCTSTR pszPath, bool bPrinting ) { assert( 0 != pszPath ); PATHNAME szPath = { 0 }; SetLastError( 0 ); const DWORD length = GetShortPathName( pszPath, szPath, dim( szPath ) ); const DWORD dwErr = GetLastError(); if ( 0 != dwErr ) { DebugBreak(); } // TODO: New function getShortPathName with retry for ERROR_NOT_READY. if ( 0 == length || dim( szPath ) < length ) { return false; } assert( length == _tcsclen( szPath ) ); ATOM aDocName = globalAddAtom( szPath ); if ( 0 != aDocName ) { EnumStruct enumStruct = { 0, aDocName }; EnumWindows( enumProc, reinterpret_cast< LPARAM >( &enumStruct ) ); verify( 0 == GlobalDeleteAtom( aDocName ) ); if ( IsWindow( enumStruct.hwnd ) ) { HWND hwndToActivate = GetLastActivePopup( enumStruct.hwnd ); verify( SetForegroundWindow( hwndToActivate ) ); if ( bPrinting ) { FORWARD_WM_COMMAND( enumStruct.hwnd, ID_FILE_PRINT, 0, 0, PostMessage ); } return true; } } return false; } // end of file
(Start bottom menu)
Top •
Home
• Articles
• Book
• Resources
Windows Developer Magazine
• R&D Books
• CMP Books
Amazon.com
• Amazon.co.uk
• Contact Petter Hesselberg
(End bottom menu)
activateOldInstance.cpp