In C# .Net, I can the do the following:
long ticks = DateTime.Now.Ticks;
How can I achieve the same result using C++? (I'm not so familiar with C++ libraries).
Thanks.
C++ Equivalent of DateTime.Now.Ticks
In C# .Net, I can the do the following:
long ticks = DateTime.Now.Ticks;
How can I achieve the same result using C++? (I'm not so familiar with C++ libraries).
Thanks.
What you are looking for is clock() which returns the number of ticks since the program started. To translate "Ticks" into something meaningful, divide by the macro CLOCKS_PER_SEC. All are included in <ctime>
[quote name='ThreeMarks' timestamp='1313594948' post='4850361']
In C# .Net, I can the do the following:
long ticks = DateTime.Now.Ticks;
How can I achieve the same result using C++? (I'm not so familiar with C++ libraries).
Thanks.
What you are looking for is clock() which returns the number of ticks since the program started. To translate "Ticks" into something meaningful, divide by the macro CLOCKS_PER_SEC. All are included in <ctime>
[/quote]
Thans Serapth. I found this earlier, but I don't see how this can give me the total ticks at, say today's date. Which I belive in C# is from January /01/0001 if I'm correct.
[quote name='Serapth' timestamp='1313595343' post='4850362']
[quote name='ThreeMarks' timestamp='1313594948' post='4850361']
In C# .Net, I can the do the following:
long ticks = DateTime.Now.Ticks;
How can I achieve the same result using C++? (I'm not so familiar with C++ libraries).
Thanks.
What you are looking for is clock() which returns the number of ticks since the program started. To translate "Ticks" into something meaningful, divide by the macro CLOCKS_PER_SEC. All are included in <ctime>
[/quote]
Thans Serapth. I found this earlier, but I don't see how this can give me the total ticks at, say today's date. Which I belive in C# is from January /01/0001 if I'm correct.
[/quote]
Well you can use std::time() to get a time_t type, which is a running total of number of seconds in unix time ( based on 1970 ) but frankly it's a horrible system, doesn't account for leap seconds and on 32bit systems will roll over in 2038. Otherwise, no.
If you are looking for microsecond precision, I don't believe it is possible, but you may be able to accomplish what you are looking for ( what are you look for anyways? ) using boost::timer
The one to one conversion is approximately:
[source lang="cpp"]
ULONGLONG DateTimeNowTicks()
{
SYSTEMTIME localTime;
GetLocalTime(&localTime);
FILETIME tocks;
SystemTimeToFileTime(&localTime, &tocks);
ULONGLONG ticks = (ULONGLONG(tocks.dwHighDateTime) << 32) | tocks.dwLowDateTime;
// number of days from beginning to 1601 multiplied by ticks per day
return ticks + 0x701ce1722770000ULL;
}
[/source]
Depending on what you want it for, the suggestions posted by others may be more appropriate
[source lang="cpp"]
ULONGLONG DateTimeNowTicks()
{
SYSTEMTIME localTime;
GetLocalTime(&localTime);
FILETIME tocks;
SystemTimeToFileTime(&localTime, &tocks);
ULONGLONG ticks = (ULONGLONG(tocks.dwHighDateTime) << 32) | tocks.dwLowDateTime;
// number of days from beginning to 1601 multiplied by ticks per day
return ticks + 0x701ce1722770000ULL;
}
[/source]
Depending on what you want it for, the suggestions posted by others may be more appropriate
Thanks for the reponses folks. So, what I am trying to achieve is the following:
I have an Excel Addin (in C#) which makes calls to C++ dll. I'd like to prevent the dll from being loaded by the Addin after a certain date - say 3 months into the future. So what I've done is taken the future timestamp and broken it into some numbers using mulitply/add/divide etc.
In the dll I then reconstruct the timestamp (a long integer, the ticks value I plugged into a C# console app) and want to compare this value to the current date time to check for expiration. The reason I deconstruct and reconstruct the the expiration date is to at least make it difficult to figure out how it's done and prevent someone disassembling.
ThreeMarks.
I have an Excel Addin (in C#) which makes calls to C++ dll. I'd like to prevent the dll from being loaded by the Addin after a certain date - say 3 months into the future. So what I've done is taken the future timestamp and broken it into some numbers using mulitply/add/divide etc.
In the dll I then reconstruct the timestamp (a long integer, the ticks value I plugged into a C# console app) and want to compare this value to the current date time to check for expiration. The reason I deconstruct and reconstruct the the expiration date is to at least make it difficult to figure out how it's done and prevent someone disassembling.
ThreeMarks.
If gigantic companies who waste a whole lot of money on protections (instead of making actual content to their software, sadly) yet keep losing to crackers before their software even gets released, what makes you think you will win?
Bottom line - crackers always win. Instead of wasting time and money, I think people should make their software more worthwile or/and cheaper, and they will profit in the long run.
Bottom line - crackers always win. Instead of wasting time and money, I think people should make their software more worthwile or/and cheaper, and they will profit in the long run.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement