Thread: computer timer

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    computer timer

    I want to make a program which times the ammount of time my computer is switched on for. I want it to log this in a text file like this

    30/12/02
    12:50->15:00

    I know how to make a program which gets the time and logs it in a text file. My only problem is I don't know how to make it turn on automaticly when my computer is turned on and close when my computer is turned off
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  2. #2
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Create a new string value under the registry key HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Windows/CurrentVersion/Run and fill it with the full path to your executable. As a result, your program will automatically be executed at Windows startup. If you plan to do this with code, look into a combination of RegCreateKeyEx() and RegSetValueEx().

    Your application's window should automatically receive a WM_CLOSE on shutdown, and you can do any necessary cleanup and final logging then.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  3. #3
    Registered User
    Join Date
    Aug 2002
    Location
    Hermosa Beach, CA
    Posts
    446
    //Win32 specific
    // GetTickCount returns the number of milliseconds
    // since windows has started.
    DWORD GetTickCount();
    The crows maintain that a single crow could destroy the heavens. Doubtless this is so. But it proves nothing against the heavens, for the heavens signify simply: the impossibility of crows.

  4. #4
    Registered User
    Join Date
    Aug 2002
    Posts
    109
    Right I have got everything working apart from one thing, thats when the computer is turned off it doesn't run get_second_time();!! I am extreamly new to programming in windows and I have just copied and pasted my Procedure from a program I made with the help of a tutorial (I have changed it slightly to try and make it work) and I think its that procedure which isn't working currectly!!

    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    
    
    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.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.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_CLOSE:
    			get_second_time();
    			DestroyWindow(hwnd);
            break;
            case WM_DESTROY:
                PostQuitMessage(0);
            break;
            default:
                return DefWindowProc(hwnd, msg, wParam, lParam);
        }
        return 0;
    }
    
    
    int WINAPI WinMain( HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int WinMode )
    {
    
    	MSG Msg;
    	get_first_time();
    
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    
    	return 0;
    
    }
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  5. #5
    Registered User
    Join Date
    Dec 2002
    Posts
    16

    Cool Files on Shutdown

    First off, change your case statement in your procedure function to:

    case WM_DESTROY:
    case WM_CLOSE:
    get_second_time();
    PostQuitMessage(0);
    break;

    This way your get_second_time() function will get called regardless of how your function is closing. Second, you try to open a file in get_second_time(). This can be a problem in NT OS's (NT 4, win2000, XP and all new OS's), because once the system start's it's shutdown, the OS won't allow new files to be opened.

    There are 2 solutions. At least one of these will work (in my experience, I've used both). First, always keep the file you are logging to open. This way, all your get_second_time() routine needs to do is output the string and close the file, which should be allowed on shutdown.

    Second, and more complicated is to respond to the WM_QUERYENDSESSION message. This message is sent to all programs when windows is told to shutdown. If you return FALSE then the windows shutdown is halted. Now you can accomplish any tasks you need to finish, and then reinitiate the shutdown using ExitWindowsEx() when your done.

    Hope this info helps!

    - Kino

  6. #6
    Registered User
    Join Date
    Aug 2002
    Posts
    109
    Code:
    #include <windows.h>
    #include <stdio.h>
    #include <time.h>
    #include <string.h>
    
    
    
    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.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.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:
    			return FALSE;
    			get_second_time();
    			ExitWindowsEx(EWX_SHUTDOWN | EWX_FORCE, 0); /*EWX_FORCE*/
    		break;
    		case WM_DESTROY:
    		case WM_CLOSE:
    			PostQuitMessage(0); 
    		break;
    
        }
        return 0;
    }
    
    
    int WINAPI WinMain( HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int WinMode )
    {
    
    	MSG Msg;
    	get_first_time();
    
    	while(GetMessage(&Msg, NULL, 0, 0) > 0)
        {
            TranslateMessage(&Msg);
            DispatchMessage(&Msg);
        }
        return Msg.wParam;
    
    	return 0;
    
    }
    Heres what I have done but it doesn't work!! I am very new to windows programming!! I need it to work in both WindowsXP and 98
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  7. #7
    Registered User
    Join Date
    Dec 2002
    Posts
    16

    Cool Danger Will Robinson! Danger!

    Well, you put code after you return FALSE to the OS. This code will never be reached. I think the best move for you is to do this:

    case WM_QUERYENDSESSION:
    case WM_DESTROY:
    case WM_CLOSE:
    get_second_time();
    PostQuitMessage(0);
    return true;

    This should work fine, and you don't even have to stop the computers shutdown.

    Hope this works for you!

    - Kino

  8. #8
    Registered User
    Join Date
    Aug 2002
    Posts
    109
    Code:
    LRESULT CALLBACK Procedure(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
    {
        switch(msg)
        {
    		case WM_QUERYENDSESSION:
    		case WM_DESTROY:
    		case WM_CLOSE:
    			get_second_time();
    			PostQuitMessage(0); 
    		return TRUE;
    
        }
        return 0;
    }
    I tryed that and it didn't work does anyone have any other suggestions which might help?
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  9. #9
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Put

    Code:
    MessageBox(NULL, "About to exit get_second_time()", "", MB_OK);
    at the end of get_second_time() to see if the function is being called, at least. That message setup (WM_QUERYENDSESSION, WM_CLOSE, WM_DESTROY) really should work.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  10. #10
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    GRR

    No the Message box did not appear! I also got my friend to try it on his Windows 98 box and it didn't work on there either.

    Can someone try and build and run it on there PC to see if they can get it working please

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

  11. #11
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    MSDN

    I have been looking at MSDN at WM_QUERYENDSESSION and it says somthing about XP but I don't quite understand what it means, can someone help me becuause this might solve my problem. MSDN (WM_QUERYENDSESSION)
    OS:- XP
    Compiler:- MSVC++ 6 or DJGPP or Dev-c++ (Mingw)

  12. #12
    trythis
    Guest
    perhaps this would work (at the top first declare a bool variable called shutdown):
    Code:
    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:
    			get_second_time();
    			PostQuitMessage(0); 
    		return TRUE;
    
        }
        return 0;
    }
    and then in your main loop:
    [CODE]
    int WINAPI WinMain( HINSTANCE hThisInst, HINSTANCE hPrevInst, LPSTR lpszArgs, int WinMode )
    {

    MSG Msg;
    get_first_time();

    while(GetMessage(&Msg, NULL, 0, 0) > 0)
    {
    if(shutdown == true)
    {
    get_second_time();
    PostQuitMessage(0);
    //code to shut windows down (ExitWindowsEx())
    }
    TranslateMessage(&Msg);
    DispatchMessage(&Msg);
    }
    return Msg.wParam;

    return 0;

    }

  13. #13
    trythis
    Guest
    whoops i forgot the last [/CODE] tag, sorry

  14. #14
    trythis
    Guest
    and make sure when you declare shutdown you set it to false

  15. #15
    trythis
    Guest
    (sorry for posting so many) but also you could try sticking get_second_time() in my example right under WM_QUERYENDSESSION (still keep everything else there that i had)

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