Solved

System Uptime

Posted on 2004-03-22
4
1,181 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
Comment Utility
What OS?
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
Comment Utility
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
Comment Utility
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
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.

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now