Thread: Updating without pressing a button

  1. #1
    george7378
    Guest

    Updating without pressing a button

    Hi everyone,

    I have a program which takes the current system time, and displays it as year, month, day, hour, minute and second. At the moment, it updates the time when you press an 'update' button. However, I want to make it automatically update the time without the user pressing the 'update' button. Here's the program I have now:

    Code:
    #include <windows.h>
    #include <math.h>
    #include <cmath>
    #include <cstdio>
    #include "resource.h" 
    
    BOOL CALLBACK ToolDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
    {
    	switch(Message)
    	{
    		case WM_COMMAND:
    			switch(LOWORD(wParam))
    			{
    
    			case IDC_UPDATE:
    
    				SYSTEMTIME* p_st = new SYSTEMTIME;
                    GetSystemTime(p_st);
                    double year = p_st->wYear;
                    double month = p_st->wMonth;
                    double day = p_st->wDay;
                    double hour = p_st->wHour;
    				double minute = p_st->wMinute;
    				double second = p_st->wSecond;
    
                    char strYear[100];
    				sprintf_s(strYear,"%0.0f", year);
    				SetDlgItemText(hwnd, IDC_YEAR, strYear);
    
    				char strMonth[100];
    				sprintf_s(strMonth,"%0.0f", month);
    				SetDlgItemText(hwnd, IDC_MONTH, strMonth);
    
    				char strDay[100];
    				sprintf_s(strDay,"%0.0f", day);
    				SetDlgItemText(hwnd, IDC_DAY, strDay);
    
    				char strHour[100];
    				sprintf_s(strHour,"%0.0f", hour);
    				SetDlgItemText(hwnd, IDC_HOUR, strHour);
    
    				char strMinute[100];
    				sprintf_s(strMinute,"%0.0f", minute);
    				SetDlgItemText(hwnd, IDC_MINUTE, strMinute);
    
    				char strSecond[100];
    				sprintf_s(strSecond,"%0.0f", second);
    				SetDlgItemText(hwnd, IDC_SECOND, strSecond);
    
    				break;	
    
    			}
    		break;
    
    		case WM_CLOSE:
    			EndDialog(hwnd, 0);
    		    break;
    
    		default:
    			return FALSE;
    	}
    	return TRUE;
    }
    
    int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
    	LPSTR lpCmdLine, int nCmdShow)
    {
    	return DialogBox(hInstance, MAKEINTRESOURCE(IDD_CLOCK), NULL, ToolDlgProc);
    }
    As you can see, when the user presses IDC_UPDATE, the date and time will update. How could I implement a loop which automatically updates? I could use a while loop in a console program, but that doesn't work here.

    Thanks a lot.

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    I am thinking you do not know how to call sprintf_s properly. Are you getting any warnings? You should be.
    As for the question, timers might just make your day.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    george7378
    Guest
    I'm not getting any errors for sprintf_s - I had no idea I was doing it wrong!

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Warnings, not errors, since I assume you're compiling in C.
    Take a good look: sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l
    It wants at least 4 parameters. You are giving it 3.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What, not even "incompatible type" for the 2nd parameter?
    sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l (CRT)


    SYSTEMTIME* p_st = new SYSTEMTIME;
    GetSystemTime(p_st);


    Consider instead, something like
    SYSTEMTIME st;
    GetSystemTime(&st);

    And save yourself some memory leak worries.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    george7378
    Guest
    I'm compiling using visual C++ (if this makes a difference) and I don't get any warnings either.

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, so first, do you intend to use C or C++?
    Secondly, this cannot possibly be your real code if it compiles fine. For C++ it won't compile at all. For C, it will give warnings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Elysia View Post
    Warnings, not errors, since I assume you're compiling in C.
    Take a good look: sprintf_s, _sprintf_s_l, swprintf_s, _swprintf_s_l
    It wants at least 4 parameters. You are giving it 3.
    Quote Originally Posted by MSDN
    In C++, using these functions is simplified by template overloads; the overloads can infer buffer length automatically (eliminating the need to specify a size argument) and they can automatically replace older, non-secure functions with their newer, secure counterparts. For more information, see Secure Template Overloads.
    The preprocessor macro is
    Code:
    #define __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1_ARGLIST(_ReturnType, _FuncName, _VFuncName, _DstType, _Dst, _TType1, _TArg1) \
        extern "C++" \
        { \
    	__pragma(warning(push)); \
    	__pragma(warning(disable: 4793)); \
        template <size_t _Size> \
        inline \
        _ReturnType __CRTDECL _FuncName(_DstType (&_Dst)[_Size], _TType1 _TArg1, ...) _CRT_SECURE_CPP_NOTHROW \
        { \
            va_list _ArgList; \
            _crt_va_start(_ArgList, _TArg1); \
            return _VFuncName(_Dst, _Size, _TArg1, _ArgList); \
        } \
    	__pragma(warning(pop)); \
        }
    
    __DEFINE_CPP_OVERLOAD_SECURE_FUNC_0_1_ARGLIST(int, sprintf_s, vsprintf_s, _Deref_post_z_ char, _Dest, _In_z_ _Printf_format_string_ const char *, _Format)

  9. #9
    george7378
    Guest
    This is my real code - I promise! Of course I have the resource file and the header file too, but that's it. I'm compiling as a multi-threaded debug, but other than that, the settings are as default (using VC++ 2008 express).

  10. #10
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    _Mike: First I heard of that. Mystery unraveled then!
    george, you still haven't mentioned if you are looking to use C or C++?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Elysia View Post
    _Mike: First I heard of that. Mystery unraveled then!
    Yeah I found it by accident when rewriting some of my code a while back.
    I changed a char[] to char* and all of a sudden it wouldn't compile anymore because I had forgotten the size parameter for swprintf_s earlier

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    LOL - another reason to avoid the "MS 'safe' API' to begin with, yet more vendor lock-in.

    Mmm, does that C++ template magic actually work if you're compiling this as 'C' code?

    Or does it require programmer laziness to start with a .cpp file, and then lazily not read the manual to not pass all the correct parameters to what should be an "off limits" function in a proper C++ program.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Salem View Post
    LOL - another reason to avoid the "MS 'safe' API' to begin with, yet more vendor lock-in.
    I don't really see the problem. Using sprint_s already locks you to Microsoft. What harm would another overload of the function do?

    Mmm, does that C++ template magic actually work if you're compiling this as 'C' code?
    I would assume not. But at least it can help when having legacy C++ code.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  14. #14
    Registered User
    Join Date
    Jan 2010
    Posts
    412
    Quote Originally Posted by Salem View Post
    Mmm, does that C++ template magic actually work if you're compiling this as 'C' code?
    No, it's guarded with
    #if defined(__cplusplus) && _CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES
    so C code will just see the regular *_s functions.

    But _CRT_SECURE_CPP_OVERLOAD_SECURE_NAMES is defined to 1 by default so it's opt-out rather than opt-in which it should have been in my opinion.
    Last edited by _Mike; 06-19-2011 at 07:19 AM. Reason: Reworded the post to make it a bit clearer

  15. #15
    george7378
    Guest
    I'm looking to use C++ please.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to make pressing a button do something?
    By SterlingM in forum C++ Programming
    Replies: 2
    Last Post: 05-22-2011, 06:12 PM
  2. Virtually pressing a button
    By Queatrix in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2005, 10:19 AM
  3. Pressing a button works sometimes
    By johny145 in forum Windows Programming
    Replies: 14
    Last Post: 05-18-2005, 11:53 AM
  4. Pressing A Button on Dos..
    By yakabod in forum A Brief History of Cprogramming.com
    Replies: 8
    Last Post: 01-30-2003, 07:46 AM
  5. Pressing a button in another app
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 03-11-2002, 12:53 PM