Thread: how to get Date/Time under WinXP??

  1. #1
    Registered User
    Join Date
    Feb 2002
    Posts
    93

    Question how to get Date/Time under WinXP??

    I have a question about retrieving the system date/time ... i can't seem to get it to work and i am unsure why...

    here is some code i've tried..

    Code:
    #include <windows.h>
    
    int main()
    {
      LPSYSTEMTIME time = NULL;
      GetSystemTime(time);
    
      return 0;
    }
    
    &&
    
    int main()
    {
      SYSTEMTIME *time = NULL;
      GetSystemTime(time);
    
      return 0;
    }
    however the cpu brings up the crash dialog box to send info to MICROSOFT...(yeah like i'm gonna do that.. ) and if i try and output the structure to the screen nothing is outputed..
    Code:
    EX:
    cout << time.wYear or time->wYear;
    i coulda swore that i tried this same code under windows 98 and it worked fine.. however i can not try because i don't have win 98 at my current location , just winXP...
    what's goin' on? MSDN says this should work under winXP... i think there may be problems with kernel32.lib but i cannot be sure.. any ideas? anyone know any other time functions to use? i need to retrieve the date (month/day/year)

  2. #2
    erstwhile
    Join Date
    Jan 2002
    Posts
    2,227
    You have not initialised your pointer 'time'.

    Either use malloc or new to allocate memory (and don't forget to free or delete it after you're done with it) or, for simplicity:
    Code:
    SYSTEMTIME time;
    GetSystemTime(&time);
    Should just about do it.

  3. #3
    Registered User
    Join Date
    Feb 2002
    Posts
    93
    thanx that piece of code fixed my problem.. now if i write it the way u showed.. do i still need to use new/delete or is memory automatically freed when the SYSTEMTIME structure goes out of scope?

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    When you statically allocate storage, (i.e. by declaring the variable, rather than a pointer to the variable), the space is created on the stack. This memory is freed when the fucntion returns. You do not need to delete/free it.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    Registered User
    Join Date
    Feb 2002
    Posts
    93
    thanx for the info.. i figured thats how it worked.. just wanted to double check... last thing i need is a memory leak..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Programming in Linux and in WinXP
    By zehcnas in forum C Programming
    Replies: 2
    Last Post: 05-06-2008, 03:42 AM
  2. Dialog opens properly in winxp, but Freezes in win98se
    By hanhao in forum C++ Programming
    Replies: 4
    Last Post: 07-10-2005, 04:46 AM
  3. problem: online on winxp and linux red hat
    By gemini_shooter in forum Linux Programming
    Replies: 5
    Last Post: 05-29-2005, 02:14 PM
  4. Major glitch losing source files with MSVC++ and WinXP
    By JasonD in forum C++ Programming
    Replies: 10
    Last Post: 08-14-2003, 12:15 PM
  5. WinXP Upgrade Doesn't Make Any Sense
    By Troll_King in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 10-25-2001, 04:18 PM