Thread: computer timer

  1. #16
    Registered User
    Join Date
    Aug 2002
    Posts
    109
    Sorry I'm new to all of this how do you declare shutdown you to false. Do you declare a bool variable like this:

    BOOL shutdown;

    Thanks for the help
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  2. #17
    trythis
    Guest
    and one more comment --> in your main message loop, you exit the loop if GetMessage() returns zero (IE WM_QUIT is sent) so the code under WM_QUIT will never be processed. if it is the WM_QUIT message that was sent. try this in a sum-up of all my previous posts:
    Code:
    bool shutdown = false;
    bool quit           = false;
    //...............(everything else here)
    //...............................................
    
    LRESULT CALLBACK Procedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
    		case WM_QUERYENDSESSION:
                                                    shutdown = true;
                                                    return FALSE;
                                                    break;
    		case WM_DESTROY:
    		case WM_CLOSE:
                                                    quit = true;
    			get_second_time();
    			PostQuitMessage(0); 
    		return TRUE;
    
        }
        return 0;
    }
    //.........everthing else you want here
    //..........................................
    
    int WINAPI WinMain( HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int WinMode )
    {
    
    MSG Msg;
    get_first_time();
    
    while(quit == false)
    {
    GetMessage(&Msg, NULL, 0, 0) ;
    
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
    if(shutdown == true)
    {
    get_second_time();
    PostQuitMessage(0);
    //code to shut windows down (ExitWindowsEx())
    }
    }
    return Msg.wParam;
    
    return 0;
    
    }

  3. #18
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Thanks

    Ok thanks I will try it. I had to rename the shutdown variable because it said I was redeclareing it (I renamed it as shutdownPC)

    I will be back to tell you if it works
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  4. #19
    Registered User
    Join Date
    Aug 2002
    Posts
    109
    It didn't work I did everything you said apart from but the close windows function in because I wanted to see if it would stop the shutdown completey but it did not. Heres my code so you can check it:

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    BOOL shutdownPC = FALSE;
    BOOL quit = FALSE;
    
    
    void remove_char( char *string, const char item )
    {
            char *at = strchr( string, item );
    
            while( *at ) *(at++) = *(at + 1);
    }
    
    
    void get_first_time()
    {
    	FILE *fp;
    	char first_time[25];
    	time_t timer;
        timer=time(NULL);
        strcpy(first_time,asctime(localtime(&timer)));
        timer=time(NULL);
    
    	fp = fopen("c:\\log\\log.txt", "at");
    	remove_char( first_time, '\n' );
    	fprintf(fp,"%s --> ",first_time);
    	fclose(fp);
    }
    
    void get_second_time()
    {
    	FILE *fp;
    	char second_time[25];
    	time_t timer;
        timer=time(NULL);
        strcpy(second_time,asctime(localtime(&timer)));
        timer=time(NULL);
    
    	fp = fopen("c:\\log\\log.txt", "at");
    	fprintf(fp,"%s-____________________-\n\n",second_time);
    	fclose(fp);
    }
    
    
    
    LRESULT CALLBACK Procedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
    		case WM_QUERYENDSESSION:
    			shutdownPC = TRUE;
    			return FALSE;
            break;
    		case WM_DESTROY:
    		case WM_CLOSE:
                quit = TRUE;
    			get_second_time();
    			PostQuitMessage(0); 
    		return TRUE;
    
        }
        return 0;
    }
    
    
    
    
    int WINAPI WinMain( HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int WinMode )
    {
    
    MSG Msg;
    get_first_time();
    
    while(quit == FALSE)
    {
    GetMessage(&Msg, NULL, 0, 0) ;
    
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
    if(shutdownPC == TRUE)
    {
    get_second_time();
    PostQuitMessage(0);
    //code to shut windows down (ExitWindowsEx())
    }
    }
    return Msg.wParam;
    
    return 0;
    }
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  5. #20
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793


    I dont think you are approaching this task properly......you want a program to run all the time the system is up....and then report the length of time the system is running....also you need this to run as a windows service if you want the time the system is running for, as a normal win 32 app will only run while a user is logged on...then you run up against a problem that you cant record properly for the shutdowns - so you want to hold up the entire system halt process so you can let your program do its thing then restart the halting of the system....then there's the problem about not being able to open a handle during shutdown, so you may have to keep the file open allthe time.....

    Phew!!!!!!!.......want an easier method - instead of fighting and cheating the system, use it!

    Win XP & 2K use an events log to take details of interesting things like errors, warnings and events.......now this is run as a service when the system boots (even before the user logs on) and finishes just before the power goes off (doesnt matter if there was a crash - it even logs those!!!)

    Now to read this log, you can use the API - ReadEventLog()......it reads a bunch of records at a time (quite a pain this as you cannot know how larger each message is!), and if you filter for the events you want, you can get all shutdowns and startups...then you can subract 1 from the other for each day and viola - the info you need!

    You need to study this API @ MSDN, but for a starter, here's a prog that shows each shutdown and startup.....its pretty quickly done, so I dont guarantee anything - but it should give you an idea

    Code:
    #include <windows.h>
    #include <exception>
    #include <iostream>
    #include <iomanip>
    #include <ctime>
    
    const int	EVENT_LOGON = 0x80001775,
    			EVENT_LOGOFF = 0x80001776,
    			DATA_SIZE = //read in at least 10 records per read
    				sizeof(EVENTLOGRECORD) * 10;
    
    int main()
    {
    
        HANDLE hEvent = 0;
        DWORD   dwRead,
    			dwDummy; 
    	PEVENTLOGRECORD lpElr = 0;
    	UCHAR* lpData[DATA_SIZE] = {0};
    	time_t tTime;
     
        hEvent = OpenEventLog(0,"System"); 
        if (!hEvent) {
    		std::cout << "Unable to open event log";
            return 1;
    	}
    
    	lpElr = reinterpret_cast<PEVENTLOGRECORD>(lpData);
    
        for( ; ; ){
    		if(!ReadEventLog(hEvent,EVENTLOG_FORWARDS_READ | 
    			EVENTLOG_SEQUENTIAL_READ,0,lpElr,DATA_SIZE,
    			&dwRead,&dwDummy)) break;
    	
    		while(dwRead > 0){		
    			switch(lpElr->EventID){
    
    			case EVENT_LOGON:
    				tTime = lpElr->TimeGenerated; 
    				std::cout << std::hex <<  lpElr->EventID << " ";
    				std::cout << "System started at ";
    				std::cout << ctime(&tTime);
    				std::cout << std::endl;
    				break;
    			case EVENT_LOGOFF:
    				tTime = lpElr->TimeGenerated; 
    				std::cout << std::hex << lpElr->EventID << " ";
    				std::cout << "System shutdown at ";
    				std::cout << ctime(&tTime);
    				std::cout << std::endl;
    				break;
    			}
    
    			dwRead -= lpElr->Length;
    			lpElr = reinterpret_cast<PEVENTLOGRECORD>(
    				reinterpret_cast<BYTE*>(lpElr) + lpElr->Length);
    		}
    
    		lpElr = reinterpret_cast<PEVENTLOGRECORD>(lpData);
    	}
    
        CloseEventLog(hEvent); 
    
    
    
    	return 0;
    }

  6. #21
    Registered User
    Join Date
    Aug 2002
    Posts
    109
    Sorry but that only works for WIN XP and 2K and I need it to work for WIN XP and 98 which I have said in a previous post.

    So can anyone help me?
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tic tac toe crashes :(
    By stien in forum Game Programming
    Replies: 4
    Last Post: 05-13-2007, 06:25 PM
  2. Replies: 34
    Last Post: 02-26-2006, 01:16 PM
  3. Tabbed Windows with MDI?
    By willc0de4food in forum Windows Programming
    Replies: 25
    Last Post: 05-19-2005, 10:58 PM
  4. Computer will not boot.
    By RealityFusion in forum Tech Board
    Replies: 25
    Last Post: 09-10-2004, 04:05 PM