4 Replies - 1261 Views - Last Post: 18 January 2010 - 11:12 AM Rate Topic: -----

#1 maybnxtseasn   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 04-April 09

[WIN32] pointer to structure help

Posted 18 January 2010 - 06:55 AM

in the windows library there is a structure for storeing the variables of the date and time

typedef struct _SYSTEMTIME {
  WORD wYear;
  WORD wMonth;
  WORD wDayOfWeek;
  WORD wDay;
  WORD wHour;
  WORD wMinute;
  WORD wSecond;
  WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;



the function used to fill these values with their correct data is

GetSystemTime() and GetLocalTime()



well what i want to do is use the same values used in this but use it in my own structure

struct Time
{
	int hours;
	int minutes;
	int seconds;
};



how do i have my structure point the the values stored in wHour,wMinute,and wSecond??? im assumeing i will use the *PSYSTEMTIME pointer to this structure to do so....im just unsure how to use it..

Is This A Good Question/Topic? 0
  • +

Replies To: [WIN32] pointer to structure help

#2 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: [WIN32] pointer to structure help

Posted 18 January 2010 - 07:26 AM

Well really you probably just want to copy over the data. Here is an example (C++) where added a constructor to your structure that will initialize it with the system time if you have not specified a time:

#include <windows.h>
#include <iostream>

struct Time
{
	int hours;
	int minutes;
	int seconds;
	
	//default constructor: gets system time from windows
	Time() {
		SYSTEMTIME st;
		GetSystemTime(&st);
		hours = st.wHour;
		minutes = st.wMinute;
		seconds = st.wSecond;
	}
	Time(int h, int m, int s) : hours(h), minutes(m), seconds(s) { }
};

std::ostream& operator<<(std::ostream &out, const Time &t) {
	out << t.hours << ":" << t.minutes << ":" << t.seconds;
	return out;
}

int main()
{
	Time t;
	std::cout << "And the time is: " << t << std::endl;
	return 0;
}


I also added an output operator for simplicity.
Was This Post Helpful? 0
  • +
  • -

#3 maybnxtseasn   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 87
  • Joined: 04-April 09

Re: [WIN32] pointer to structure help

Posted 18 January 2010 - 10:08 AM

View PostNickDMax, on 18 Jan, 2010 - 06:26 AM, said:

Well really you probably just want to copy over the data. Here is an example (C++) where added a constructor to your structure that will initialize it with the system time if you have not specified a time:

#include <windows.h>
#include <iostream>

struct Time
{
	int hours;
	int minutes;
	int seconds;
	
	//default constructor: gets system time from windows
	Time() {
		SYSTEMTIME st;
		GetSystemTime(&st);
		hours = st.wHour;
		minutes = st.wMinute;
		seconds = st.wSecond;
	}
	Time(int h, int m, int s) : hours(h), minutes(m), seconds(s) { }
};

std::ostream& operator<<(std::ostream &out, const Time &t) {
	out << t.hours << ":" << t.minutes << ":" << t.seconds;
	return out;
}

int main()
{
	Time t;
	std::cout << "And the time is: " << t << std::endl;
	return 0;
}


I also added an output operator for simplicity.



should i just declare it as a class lol?
Was This Post Helpful? 0
  • +
  • -

#4 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: [WIN32] pointer to structure help

Posted 18 January 2010 - 11:00 AM

Depends. struct is actually just an alias for class. basically when you write
struct myStruct { 
...
};


You are doing exactly the same as:
class myStruct { public:
...
};


So you can use struct/class it does not matter. Usually in such a case where all the members are public and I don't have a lot of member functions I generally go with struct. -- saving class for more advanced uses that better form a model of an object.

but it is up to you.
Was This Post Helpful? 0
  • +
  • -

#5 NickDMax   User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2255
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: [WIN32] pointer to structure help

Posted 18 January 2010 - 11:12 AM

BTW... if you did not want to make your code platform dependent you can actually get the time directly from the C/C++ standard library:
//#include <windows.h>
#include <iostream>
#include <ctime>

struct Time
{
	int hours;
	int minutes;
	int seconds;
	
	//default constructor: gets system time from windows
	Time() {
		//SYSTEMTIME st;
		//GetSystemTime(&st);
		//hours = st.wHour;
		//minutes = st.wMinute;
		//seconds = st.wSecond;
		std::time_t theTime;
		std::tm* pTime;
		std::time(&theTime);
		pTime = std::gmtime(&theTime);
		hours = pTime->tm_hour;
		minutes = pTime->tm_min;
		seconds = pTime->tm_sec;
	}
	Time(int h, int m, int s) : hours(h), minutes(m), seconds(s) { }
};

std::ostream& operator<<(std::ostream &out, const Time &t) {
	out << t.hours << ":" << t.minutes << ":" << t.seconds;
	return out;
}

int main()
{
	Time t;
	std::cout << "And the time is: " << t << std::endl;
	return 0;
}


Although if you plan on using other windows specific features then it does not really make much difference.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1