home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2005 March / PCpro_2005_03.ISO / files / systools / speedswitchxp / sswitchxp14.exe / Typical / SpeedswitchXP.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-01-14  |  4.6 KB  |  200 lines

  1. /*
  2.    SpeedswitchXP V1.4
  3.    - Windows XP CPU Frequency Control for Notebooks -
  4.  
  5.    Copyright(c) 2002-2004 Christian Diefer
  6.  
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License version 2 as 
  9.    published by the Free Software Foundation.
  10.    
  11.    This program is distributed in the hope that it will be useful,
  12.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.    GNU General Public License for more details.
  15.    
  16.    You should have received a copy of the GNU General Public License
  17.    along with this program; if not, write to the Free Software
  18.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. */
  20.  
  21. #include "stdafx.h"
  22. #include "SpeedswitchXP.h"
  23. #include "TOptions.h"
  24. #include "SpeedswitchXPDlg.h"
  25.  
  26. #ifdef _DEBUG
  27. #define new DEBUG_NEW
  28. #endif
  29.  
  30. char logPath[264];
  31.  
  32. // CSpeedswitchXPApp
  33.  
  34. BEGIN_MESSAGE_MAP(CSpeedswitchXPApp, CWinApp)
  35.     ON_COMMAND(ID_HELP, CWinApp::OnHelp)
  36. END_MESSAGE_MAP()
  37.  
  38.  
  39. // CSpeedswitchXPApp-Erstellung
  40.  
  41. CSpeedswitchXPApp::CSpeedswitchXPApp()
  42. {
  43.   activeMutex = FALSE;
  44. }
  45.  
  46.  
  47. // Das einzige CSSpeedswitchXPApp-Objekt
  48.  
  49. CSpeedswitchXPApp theApp;
  50. CSpeedswitchXPDlg* dlg;
  51.  
  52. // CSpeedswitchXPApp Initialisierung
  53.  
  54. BOOL CSpeedswitchXPApp::InitInstance()
  55. {
  56.     // InitCommonControls() ist fⁿr Windows XP erforderlich, wenn ein Anwendungsmanifest
  57.     // die Verwendung von ComCtl32.dll Version 6 oder h÷her zum Aktivieren
  58.     // von visuellen Stilen angibt. Ansonsten treten beim Erstellen von Fenstern Fehler auf.
  59.     InitCommonControls();
  60.  
  61.     CWinApp::InitInstance();
  62.  
  63.  
  64.     // make sure that only one instance is running
  65.   BOOL    ret = FALSE;
  66.     char* mutexName = "SpeedswitchXP_mutex";
  67.  
  68.   hMutex = OpenMutex( MUTEX_ALL_ACCESS, FALSE, mutexName );
  69.     if( hMutex == NULL )
  70.     {
  71.         hMutex = CreateMutex( 0, TRUE, mutexName );
  72.         DWORD err = GetLastError();
  73.         ret = !(hMutex==NULL || err==ERROR_ALREADY_EXISTS);
  74.     if( hMutex != NULL )
  75.       activeMutex = TRUE;
  76.     }
  77.     else
  78.         CloseHandle( hMutex );
  79.  
  80.     if( ret == FALSE )
  81.   {
  82.     MessageBox( NULL, "Program already running", "Terminating...", MB_ICONSTOP|MB_OK );
  83.     return FALSE;
  84.   }
  85.  
  86.   // create log filename
  87.   if( GetModuleFileName(NULL,logPath,255) == 0 )
  88.   {
  89.     CloseHandle( hMutex );
  90.     MessageBox( NULL, "Can't get module name", "Error", MB_OK|MB_ICONSTOP );
  91.     return FALSE;
  92.   }
  93.  
  94.   int i = (int)strlen( logPath ) - 1;
  95.   while( i>=0 && logPath[i]!='.' )
  96.     i--;
  97.  
  98.   if( i >= 0 )
  99.     strcpy( &logPath[i], ".log" );
  100.   else
  101.   {
  102.     CloseHandle( hMutex );
  103.     MessageBox( NULL, "Can't create log filename", "Error", MB_ICONSTOP|MB_OK );
  104.     return FALSE;
  105.   }
  106.  
  107.   // create and display the main window
  108.   dlg = new CSpeedswitchXPDlg();
  109.   m_pMainWnd = dlg;
  110.  
  111.   if( m_pMainWnd )
  112.   {
  113.     log( "Mutex: 0x%08x", hMutex );
  114.     log( "Init complete" );
  115.     log( "--------------------------------" );
  116.     dlg->setCmdLine( m_lpCmdLine );
  117.     return TRUE;
  118.   }
  119.   else
  120.     return FALSE;
  121. }
  122.  
  123. /////////////////////////////////////////////////////////////////////////////
  124. int CSpeedswitchXPApp::ExitInstance() 
  125. {
  126.   if( activeMutex )  
  127.   {
  128.     log( "/bReleasing mutex (0x%08x) ...", hMutex );
  129.     if( CloseHandle(hMutex) )
  130.       log( "/aOK" );
  131.     else
  132.       log( "/aError" );
  133.   }
  134.  
  135.     return CWinApp::ExitInstance();
  136. }
  137.  
  138. /////////////////////////////////////////////////////////////////////////////
  139. // print out a debug message
  140. void log( char* msg, ... )
  141. {
  142.   BOOL noDate=FALSE, noCR=FALSE;
  143.  
  144.   if( !options.debugMode )
  145.     return;
  146.  
  147.   static char debugBuf[32768];
  148.   va_list ap;
  149.   va_start( ap, msg );
  150.  
  151.   time_t zeit = time( NULL );
  152.   struct tm* ts = localtime( &zeit );
  153.  
  154.   *debugBuf = '\0';
  155.  
  156.   if( *msg == '/' )
  157.   {
  158.     switch( msg[1] )
  159.     {
  160.       case 'a': noDate=TRUE;
  161.                 msg+=2;  
  162.                 break;
  163.  
  164.       case 'b': noCR=TRUE;
  165.                 msg+=2;  
  166.                 break;
  167.  
  168.       case 'c': noDate=TRUE;
  169.                 noCR=TRUE;
  170.                 msg+=2;  
  171.                 break;
  172.     }
  173.   }
  174.  
  175.   if( !noDate )
  176.     wsprintf( debugBuf,
  177.               "%02d:%02d:%02d ",
  178.               ts->tm_hour,
  179.               ts->tm_min,
  180.               ts->tm_sec );
  181.  
  182.   vsprintf( &debugBuf[strlen(debugBuf)], msg, ap );
  183.   va_end( ap );
  184.  
  185.   if( !noCR )
  186.     strcat( debugBuf, "\n" );
  187.  
  188.   FILE* f = fopen( logPath, "at" );  
  189.   if( f == NULL )
  190.   {
  191.     options.debugMode = FALSE;
  192.     MessageBox( NULL, "Can't write debug log. Debugging disabled.", "Error", MB_ICONSTOP|MB_OK );
  193.     return;
  194.   }
  195.  
  196.   fprintf( f, debugBuf );
  197.   fclose( f );
  198. }
  199.  
  200.