Link to home
Start Free TrialLog in
Avatar of State
State

asked on

System Uptime

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.
Avatar of chensu
chensu
Flag of Canada image

What OS?
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.
ASKER CERTIFIED SOLUTION
Avatar of bdunz19
bdunz19

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bdunz19
bdunz19

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;
}