Solved

System Uptime

Posted on 2004-03-22
4
1,186 Views
Last Modified: 2012-05-04
I need to get the system uptime and then store it within a char* (char* -only-) in the format of "<hour> hour(s), <minute> minute(s), and <second> second(s)." While I can get the system uptime on my own, my method doesn't seem very stable nor does it result in being properly passed off to a char*. And yes, I'm aware the easiest way to do this resets every forty someodd days. That's quite alright by me.
0
Comment
Question by:State
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 23

Expert Comment

by:chensu
ID: 10654077
What OS?
0
 
LVL 23

Expert Comment

by:chensu
ID: 10654085
For Windows, you can use the GetTickCount() API function which returns the number of milliseconds that have elapsed since the system was started. The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days.
0
 
LVL 4

Accepted Solution

by:
bdunz19 earned 250 total points
ID: 10654285
Try this... I'm not sure if this is it exactly, but maybe you can modify my code to have it work correctly (I just typed this out).

#include <windows.h>
#include <stdio.h>

VOID main()
{
      INT dTime = 0;
      CHAR time[256];
      CHAR hours[256];
      CHAR minutes[256];
      CHAR seconds[256];
      while(1)
      {
            dTime = GetTickCount();
            sprintf(hours, "%d", dTime / 3600000);
            sprintf(minutes, "%d", (dTime % 3600000) / 60000);
            sprintf(seconds, "%d", ((dTime % 3600000) % 60000) / 1000);
            printf("%s hour(s) %s minute(s) %s second(s)\n", hours, minutes, seconds);

            system("cls");
      }
}


Good Luck!
Brandon
0
 
LVL 4

Expert Comment

by:bdunz19
ID: 10654396
lol... This question kind of inspired me, so I decided to go ahead and write a Win32 app that sits on your desktop and updates the system time. Here's the code, this might help you out alittle bit more:

#include <windows.h>
#include <stdio.h>

const CHAR SysUpName[] = "System Uptime";

LRESULT WINAPI SysUpProc(HWND hWnd, MSG msg, WPARAM wParam, LPARAM lParam);
INT CreateSysUp(HINSTANCE hInstance);
INT RegisterSysUp(HINSTANCE hInstance);
INT SysUpMsgLoop();

INT DrawUI(HDC hdc);
INT DrawInfo(HDC hdc);

HWND gM_hWnd = NULL;
CHAR SysUptime[265];

INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR CmdLine, INT)
{
      INT result;
      RegisterSysUp(hInstance);
      CreateSysUp(hInstance);
      UpdateWindow(gM_hWnd);
      ShowWindow(gM_hWnd, SW_SHOWDEFAULT);
      result = SysUpMsgLoop();
      return result;
}

LRESULT WINAPI SysUpProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
      HDC hdc;
      PAINTSTRUCT ps;
      CHAR tmpOut[256];
      INT dTime = 0;
      CHAR hours[256];
      CHAR minutes[256];
      CHAR seconds[256];
      RECT tr;
      tr.left = 0;
      tr.top = 20;
      tr.bottom = 34;
      tr.right = 265;
      static INT CmdX = 0;

      switch(msg)
      {
      case WM_TIMER:
            dTime = GetTickCount();
            sprintf(hours, "%d", dTime / 3600000);
            sprintf(minutes, "%d", (dTime % 3600000) / 60000);
            sprintf(seconds, "%d", ((dTime % 3600000) % 60000) / 1000);
            sprintf(SysUptime, "%s hour(s), %s minute(s), and %s second(s)", hours, minutes, seconds);
            InvalidateRect(gM_hWnd, &tr, TRUE);
      case WM_CREATE:
            SetTimer(hWnd, 0, 1000, NULL);
            return 0;
      case WM_PAINT:
            hdc = BeginPaint(gM_hWnd, &ps);
            DrawInfo(hdc); DrawUI(hdc);
            EndPaint(gM_hWnd, &ps);
            return 0;
      case WM_KEYDOWN:
            switch(wParam)
            {
            case VK_ESCAPE:
                  PostQuitMessage(0);
            }
            break;
      case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
      }
      return DefWindowProc(hWnd, msg, wParam, lParam);
}

INT DrawInfo(HDC hdc)
{
      LOGFONT lf;
      HFONT hFont;
      INT yPosition = 20;
      CHAR tmpOut[256];

      lstrcpy(lf.lfFaceName, "Lucida Console");
      lf.lfHeight         = 10;
    lf.lfWidth          = 0;
    lf.lfEscapement     = 0;
    lf.lfOrientation    = 0;
    lf.lfWeight         = 0;
    lf.lfItalic         = 0;
    lf.lfUnderline      = 0;
    lf.lfStrikeOut      = 0;
    lf.lfCharSet        = DEFAULT_CHARSET ;
    lf.lfOutPrecision   = 0;
    lf.lfClipPrecision  = 0;
    lf.lfQuality        = 0;
    lf.lfPitchAndFamily = 0;
      hFont = CreateFontIndirect (&lf);
      GetObject(hFont, sizeof(LOGFONT), &lf);
      SetBkMode(hdc, TRANSPARENT);
      SelectObject(hdc, hFont);
      SetTextColor(hdc, RGB(255, 255, 255));

      TextOut(hdc, 5, yPosition, SysUptime, strlen(SysUptime));
      yPosition += 14;

      return 0;
}

INT DrawUI(HDC hdc)
{
      LOGFONT lf;
      HFONT hFont;

      lstrcpy(lf.lfFaceName, "Arial");
      lf.lfHeight         = 14;
    lf.lfWidth          = 0;
    lf.lfEscapement     = 0;
    lf.lfOrientation    = 0;
    lf.lfWeight         = 0;
    lf.lfItalic         = 0;
    lf.lfUnderline      = 0;
    lf.lfStrikeOut      = 0;
    lf.lfCharSet        = DEFAULT_CHARSET ;
    lf.lfOutPrecision   = 0;
    lf.lfClipPrecision  = 0;
    lf.lfQuality        = 0;
    lf.lfPitchAndFamily = 0;
      hFont = CreateFontIndirect (&lf);
      GetObject(hFont, sizeof(LOGFONT), &lf);
      SetBkMode(hdc, TRANSPARENT);
      SelectObject(hdc, hFont);
      SelectObject(hdc, CreateSolidBrush(RGB(50, 50, 50)));
      Rectangle(hdc, 0, 0, 265, 16);
      SetTextColor(hdc, RGB(0, 0, 0));
      TextOut(hdc, 9, 2, SysUpName, strlen(SysUpName));
      SetTextColor(hdc, RGB(150, 150, 150));
      TextOut(hdc, 8, 1, SysUpName, strlen(SysUpName));

      return 0;
}

INT SysUpMsgLoop()
{
      MSG msg;
      while(GetMessage(&msg, NULL, 0, 0) > 0)
      {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
      }
      return msg.wParam;
}

INT CreateSysUp(HINSTANCE hInstance)
{
      RECT rCurRes;
      GetWindowRect(GetDesktopWindow(), &rCurRes);

      gM_hWnd = CreateWindowEx(NULL, "SysUp", SysUpName, WS_POPUP | WS_SYSMENU | WS_MINIMIZEBOX, rCurRes.right - 265, 0, 265, 35,
            GetDesktopWindow(), NULL, hInstance, NULL);

      return 0;
}

INT RegisterSysUp(HINSTANCE hInstance)
{
      WNDCLASSEX wc;
      wc.cbClsExtra = NULL;
      wc.cbSize = sizeof(WNDCLASSEX);
      wc.cbWndExtra = NULL;
      wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
      wc.hCursor = LoadCursor(NULL, IDC_ARROW);
      wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
      wc.hIconSm = NULL;
      wc.hInstance = hInstance;
      wc.lpfnWndProc = SysUpProc;
      wc.lpszClassName = "SysUp";
      wc.lpszMenuName = NULL;
      wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;

      RegisterClassEx(&wc);

      return 0;
}
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question