Last updated:
31. October 2001

User Interface Programming

(Start main menu) Home ­ Articles ­ Book ­ Resources (End main menu)

activateOldInstance.cpp: Listing 28


(Start sub-menu)

Table of Contents

Cover

Sample Chapter

Changelog


 

Feedback…

My blog »

(End sub-menu)

Programming Industrial Strength Windows (cover)

/*
 * $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 win­dow 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)

TopHomeArticlesBookResources
Win­dows De­vel­oper Maga­zineR&D BooksCMP Books
Amazon.comAmazon.co.ukContact Petter Hesselberg

(End bottom menu)

Chapter 7 Listings

createNewFile.cpp

getLongPathName.cpp

resolveName.cpp

activateOldInstance.cpp

ArgumentList.h

ArgumentList.cpp

init.cpp