Jump to content
  • Advertisement
Sign in to follow this  

WIN32 MessageBox Overflow?

This topic is 5456 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello, I recently learned of the GetTickCount function in WIN32 that returns the number of miliseconds windows has been running at. I thought it'd be neat to write a little program that pops up a message box saying how long my computer's been on. With that, I tried to run something like this:
DWORD time = GetTickCount();
std::string theMessage = "" + time;
MessageBox(NULL, theMessage.c_str(), "Time Windows has been on", MB_OK);

When I tried to run it, the program crashed; maybe the number is too big or something. So, I tried again, this time dividing time by a rather large number
DWORD time = GetTickCount();
std::string theMessage = "" + time/10000000;
MessageBox(NULL, theMessage.c_str(), "Time Windows has been on", MB_OK);

Now, it simply displays a blank MessageBox. I would have thought dividing by 10000000 would have solved my problem but it's blank now! Does anyone know why?

Share this post

Link to post
Share on other sites
Adding a DWORD to a std::string or a literal doesn't do what you expect it to. I don't even know what it does myself, probably something evil involving pointers.
You need to format the DWORD (=unsigned integer) as a string using a stringstream or something.

Share this post

Link to post
Share on other sites
Uh oh.

When I hover my mouse cursor over DWORD in VS .NET 2003, it tells me that DWORD is simply a typedef for an unsigned long. EDIT: Do you know why you can't add DWORDs to strings? I never knew until now :[

EDIT2: The messagebox is screwing up whenever I try to append any number using the + operator, this is weird. EDIT3: I think I'm getting confused with Java, time to re-read about std::string

Share this post

Link to post
Share on other sites

char buf[100];
sprintf( buf, "The computer has been on %ld milliseconds.",
GetTickCount() );
MessageBox( 0, buf, "IMPORTANT INFAMATON", MB_OK );

Note that GetTickCount() is only accurate to about 15 milliseconds or so; it's not a very good timer. Check here for more information about PC timers.

Share this post

Link to post
Share on other sites
Okay, sorry for my stupidness. I forgot that

1: In C++ you can't append numbers onto strings with the + operator. *shakes fist at Java* I stole the code from the C++ FAQ Lite to convert the DWORD into a string (which uses string streams as Paradigm Shifter suggested).

2: How to split up the miliseconds into hours, minutes and seconds! I still can't remember how to do it with %, so I used another less efficient method.

Anyway, so I finally got it done (which took far more time than it should have) and the following code spits out how long you've been running windows.

#include <windows.h>
#include <string>
#include <sstream>

//eww global variables
HWND wndHandle;

//window size and stuff
const int windowWidth = 1024;
const int windowHeight = 768;

//function forward declarations
bool initWindow(HINSTANCE hInstance);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine,
int nCmdShow)
//initialize window
return false;

//main message loop
MSG msg;
ZeroMemory(&msg, sizeof(msg));
while(msg.message != WM_QUIT)
//check message queue
if(PeekMessage(&msg, NULL, 0U, 0U, PM_REMOVE ))

//get number of miliseconds windows has been running for
DWORD time = GetTickCount();
int hours = time / 3600000;
time -= hours * 3600000;
int minutes = time / 60000;
time -= minutes * 60000;
int seconds = time / 1000;
std::ostringstream o1, o2, o3;
o1 << hours;
o2 << minutes;
o3 << seconds;
std::string theMessage = "Windows has been running for: " + o1.str() + " hours, " + o2.str() + " minutes, " + o3.str() + " seconds.";
MessageBox(NULL, theMessage.c_str(), "Time Windows has been on", MB_OK);
msg.message = WM_QUIT;

return (int)msg.wParam;

// InitWindow()
bool initWindow(HINSTANCE hInstance)

//create window properties structure
wcex.cbSize = sizeof(WNDCLASSEX); //size of window
wcex.style = CS_HREDRAW | CS_VREDRAW; //window graphic style
wcex.lpfnWndProc = (WNDPROC)WndProc; //"window procedure callback"
wcex.cbClsExtra = 0; //extra unused bytes
wcex.cbWndExtra = 0; //extra unused bytes
wcex.hInstance = hInstance; //handle to application instance
wcex.hIcon = 0; //application icon
wcex.hCursor = LoadCursor(NULL, IDC_ARROW); //application cursor
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1); //background color
wcex.lpszMenuName = NULL; //window menu name
wcex.lpszClassName = "Windows Time Running"; //window class name
wcex.hIconSm = 0; //"small handle" icon
//registers window property class with Windows

//create window
wndHandle = CreateWindow(
"Windows Time Running", //name of window class using
"Windows Time Running Title", //name of title bar
WS_OVERLAPPEDWINDOW, //window graphic style
/* WS_OVERLAPPEDWINDOW is a standard window */
CW_USEDEFAULT, //window x coordinate
CW_USEDEFAULT, //window y coordinate
windowWidth, //window width
windowHeight, //window height
NULL, //"parent window" (NULL means desktop)
NULL, //menu name
hInstance, //application instance
NULL); //value passed to window (unused)

//verify window has been created
return false;

//display window on screen
//ShowWindow(wndHandle, SW_SHOW);

return true;

//check queue for messages
//destroy window

//return message to default window for further processing
return DefWindowProc(hWnd, message, wParam, lParam);

hplus0603: I'm not using the timer for a game or anything so the 10 milisecond inaccuracy doesn't really matter :]

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!