home *** CD-ROM | disk | FTP | other *** search
- /*
- SpeedswitchXP V1.4
- - Windows XP CPU Frequency Control for Notebooks -
-
- Copyright(c) 2002-2004 Christian Diefer
-
- This program is free software; you can redistribute it and/or modify
- it under the terms of the GNU General Public License version 2 as
- published by the Free Software Foundation.
-
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
-
- You should have received a copy of the GNU General Public License
- along with this program; if not, write to the Free Software
- Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
- */
-
- #include "stdafx.h"
- #include "SpeedswitchXP.h"
- #include "TOptions.h"
- #include "SpeedswitchXPDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #endif
-
- char logPath[264];
-
- // CSpeedswitchXPApp
-
- BEGIN_MESSAGE_MAP(CSpeedswitchXPApp, CWinApp)
- ON_COMMAND(ID_HELP, CWinApp::OnHelp)
- END_MESSAGE_MAP()
-
-
- // CSpeedswitchXPApp-Erstellung
-
- CSpeedswitchXPApp::CSpeedswitchXPApp()
- {
- activeMutex = FALSE;
- }
-
-
- // Das einzige CSSpeedswitchXPApp-Objekt
-
- CSpeedswitchXPApp theApp;
- CSpeedswitchXPDlg* dlg;
-
- // CSpeedswitchXPApp Initialisierung
-
- BOOL CSpeedswitchXPApp::InitInstance()
- {
- // InitCommonControls() ist fⁿr Windows XP erforderlich, wenn ein Anwendungsmanifest
- // die Verwendung von ComCtl32.dll Version 6 oder h÷her zum Aktivieren
- // von visuellen Stilen angibt. Ansonsten treten beim Erstellen von Fenstern Fehler auf.
- InitCommonControls();
-
- CWinApp::InitInstance();
-
-
- // make sure that only one instance is running
- BOOL ret = FALSE;
- char* mutexName = "SpeedswitchXP_mutex";
-
- hMutex = OpenMutex( MUTEX_ALL_ACCESS, FALSE, mutexName );
- if( hMutex == NULL )
- {
- hMutex = CreateMutex( 0, TRUE, mutexName );
- DWORD err = GetLastError();
- ret = !(hMutex==NULL || err==ERROR_ALREADY_EXISTS);
- if( hMutex != NULL )
- activeMutex = TRUE;
- }
- else
- CloseHandle( hMutex );
-
- if( ret == FALSE )
- {
- MessageBox( NULL, "Program already running", "Terminating...", MB_ICONSTOP|MB_OK );
- return FALSE;
- }
-
- // create log filename
- if( GetModuleFileName(NULL,logPath,255) == 0 )
- {
- CloseHandle( hMutex );
- MessageBox( NULL, "Can't get module name", "Error", MB_OK|MB_ICONSTOP );
- return FALSE;
- }
-
- int i = (int)strlen( logPath ) - 1;
- while( i>=0 && logPath[i]!='.' )
- i--;
-
- if( i >= 0 )
- strcpy( &logPath[i], ".log" );
- else
- {
- CloseHandle( hMutex );
- MessageBox( NULL, "Can't create log filename", "Error", MB_ICONSTOP|MB_OK );
- return FALSE;
- }
-
- // create and display the main window
- dlg = new CSpeedswitchXPDlg();
- m_pMainWnd = dlg;
-
- if( m_pMainWnd )
- {
- log( "Mutex: 0x%08x", hMutex );
- log( "Init complete" );
- log( "--------------------------------" );
- dlg->setCmdLine( m_lpCmdLine );
- return TRUE;
- }
- else
- return FALSE;
- }
-
- /////////////////////////////////////////////////////////////////////////////
- int CSpeedswitchXPApp::ExitInstance()
- {
- if( activeMutex )
- {
- log( "/bReleasing mutex (0x%08x) ...", hMutex );
- if( CloseHandle(hMutex) )
- log( "/aOK" );
- else
- log( "/aError" );
- }
-
- return CWinApp::ExitInstance();
- }
-
- /////////////////////////////////////////////////////////////////////////////
- // print out a debug message
- void log( char* msg, ... )
- {
- BOOL noDate=FALSE, noCR=FALSE;
-
- if( !options.debugMode )
- return;
-
- static char debugBuf[32768];
- va_list ap;
- va_start( ap, msg );
-
- time_t zeit = time( NULL );
- struct tm* ts = localtime( &zeit );
-
- *debugBuf = '\0';
-
- if( *msg == '/' )
- {
- switch( msg[1] )
- {
- case 'a': noDate=TRUE;
- msg+=2;
- break;
-
- case 'b': noCR=TRUE;
- msg+=2;
- break;
-
- case 'c': noDate=TRUE;
- noCR=TRUE;
- msg+=2;
- break;
- }
- }
-
- if( !noDate )
- wsprintf( debugBuf,
- "%02d:%02d:%02d ",
- ts->tm_hour,
- ts->tm_min,
- ts->tm_sec );
-
- vsprintf( &debugBuf[strlen(debugBuf)], msg, ap );
- va_end( ap );
-
- if( !noCR )
- strcat( debugBuf, "\n" );
-
- FILE* f = fopen( logPath, "at" );
- if( f == NULL )
- {
- options.debugMode = FALSE;
- MessageBox( NULL, "Can't write debug log. Debugging disabled.", "Error", MB_ICONSTOP|MB_OK );
- return;
- }
-
- fprintf( f, debugBuf );
- fclose( f );
- }
-
-