Solved

System Uptime

Posted on 2004-03-22
4
1,184 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
  • 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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

776 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