SystemTimeToFileTime

Windows specific questions.
deltarho[1859]
Posts: 2585
Joined: Jan 02, 2017 0:34
Location: UK

SystemTimeToFileTime

Postby deltarho[1859] » Mar 06, 2020 15:23

Code: Select all

#include once "windows.bi"
Dim As SystemTime TimeNow
Dim As LPFILETIME ptrFT
 
GetSystemTime( @TimeNow )
Print SystemTimeToFileTime( ByVal @TimeNow, ByVal ptrFT )
Print "Here"
Sleep

I don't get to see "Here" - the console opens and then closes. SystemTimeToFileTime returns a value but I don't even get to see that.

This is what we have from winbase.bi:

Code: Select all

SystemTimeToFileTime(byval lpSystemTime as const SYSTEMTIME ptr, byval lpFileTime as LPFILETIME) as WINBOOL

Any ideas, folks.
Xusinboy Bekchanov
Posts: 246
Joined: Jul 26, 2018 18:28

Re: SystemTimeToFileTime

Postby Xusinboy Bekchanov » Mar 06, 2020 15:38

You need to do this:

Code: Select all

#include once "windows.bi"
Dim As SystemTime TimeNow
Dim As FILETIME FT
 
GetSystemTime( @TimeNow )
Print SystemTimeToFileTime(@TimeNow, @FT )
Print "Here"
Sleep
deltarho[1859]
Posts: 2585
Joined: Jan 02, 2017 0:34
Location: UK

Re: SystemTimeToFileTime

Postby deltarho[1859] » Mar 06, 2020 16:02

@Xusinboy Bekchanov

Thank you.

So, both MSDN and winbase.bi are misleading - in that we are led up the garden path by mentioning LPFILENAME.

What we should have then is:

Code: Select all

SystemTimeToFileTime(byval lpSystemTime as const SYSTEMTIME ptr, byval lpFileTime as FILETIME ptr) as WINBOOL

Of course, it isn't FBs fault - MSDN is the villain.

The question is why wasn't the error trapped instead of just closing the console without a word.

Thanks again. Image
Xusinboy Bekchanov
Posts: 246
Joined: Jul 26, 2018 18:28

Re: SystemTimeToFileTime

Postby Xusinboy Bekchanov » Mar 06, 2020 16:21

deltarho[1859] wrote:So, both MSDN and winbase.bi are misleading - in that we are led up the garden path by mentioning LPFILENAME.

What we should have then is:

Code: Select all

SystemTimeToFileTime(byval lpSystemTime as const SYSTEMTIME ptr, byval lpFileTime as FILETIME ptr) as WINBOOL

Of course, it isn't FBs fault - MSDN is the villain.

FILETIME ptr and LPFILETIME are the same.
In your version, ptrFT is 0
In my version, FILETIME is created.

deltarho[1859] wrote:@Xusinboy Bekchanov
The question is why wasn't the error trapped instead of just closing the console without a word.

When checking with the GDB gave the following error:
Program received signal SIGSEGV, Segmentation fault.
0x778ced50 in SystemTimeToFileTime () from C:\Windows\SysWOW64\KernelBase.dll
Last edited by Xusinboy Bekchanov on Mar 06, 2020 16:27, edited 2 times in total.
PaulSquires
Posts: 880
Joined: Jul 14, 2005 23:41

Re: SystemTimeToFileTime

Postby PaulSquires » Mar 06, 2020 16:23

You can make it even easier on yourself by using Jose's Afx library, specifically, the AfxTime.inc and/or CTime64.inc class and/or CFileTime.inc class.
deltarho[1859]
Posts: 2585
Joined: Jan 02, 2017 0:34
Location: UK

Re: SystemTimeToFileTime

Postby deltarho[1859] » Mar 06, 2020 16:27

@Xusinboy Bekchanov

I don't use a debugger but if I did the error returned would have straight over my head.
deltarho[1859]
Posts: 2585
Joined: Jan 02, 2017 0:34
Location: UK

Re: SystemTimeToFileTime

Postby deltarho[1859] » Mar 06, 2020 16:36

Paul wrote:You can make it even easier on yourself by using Jose's Afx library

That never crossed my mind.

Code: Select all

GatAsFileTime

It cannot get much easier than that. Image
deltarho[1859]
Posts: 2585
Joined: Jan 02, 2017 0:34
Location: UK

Re: SystemTimeToFileTime

Postby deltarho[1859] » Mar 06, 2020 17:29

I thought it was too good to be true.

I am getting a variable not declared for GetAsFileTime, and I am using #Include Once "Afx\CTime64.inc".

Anyway, I see that Unix time is being used and I want to use an epoch of 1 January 1900, so I'll have to stay with the Windows APIs.
deltarho[1859]
Posts: 2585
Joined: Jan 02, 2017 0:34
Location: UK

Re: SystemTimeToFileTime

Postby deltarho[1859] » Mar 06, 2020 17:45

@Xusinboy Bekchanov
In your version, ptrFT is 0
In my version, FILETIME is created.

Of course.

I should have written:

Code: Select all

Dim As SystemTime TimeNow
Dim As FILETIME FT
Dim As LPFILETIME ptrFT

ptrFT = @FT
 
GetSystemTime( @TimeNow )
Print SystemTimeToFileTime( @TimeNow, ptrFT )

But your method of bypassing LPFILENAME is better.

Added: I have been a PowerBASIC member for 17 years and a FreeBASIC member for 3 years. Dealing with the Windows APIs in PowerBASIC is much more relaxed.
PaulSquires
Posts: 880
Joined: Jul 14, 2005 23:41

Re: SystemTimeToFileTime

Postby PaulSquires » Mar 06, 2020 18:45

Code: Select all

#include once "afx\AfxTime.inc"

Dim As FILETIME FT = AfxSystemFileTime()

deltarho[1859]
Posts: 2585
Joined: Jan 02, 2017 0:34
Location: UK

Re: SystemTimeToFileTime

Postby deltarho[1859] » Mar 06, 2020 19:05

Thanks Paul.

However, I cannot see how I can have 'Dim As SystemTime st', populate st and then get the filetime for st using AfxTime.inc.

Anyway, like I said I'll be using an epoch of 1 January 1900 and Afx has Unix time hardwired.
PaulSquires
Posts: 880
Joined: Jul 14, 2005 23:41

Re: SystemTimeToFileTime

Postby PaulSquires » Mar 06, 2020 19:59

Hi David, no problem, just to complete my thoughts on the subject here is the actual code from AfxTime.inc that is called:

Code: Select all

' ========================================================================================
' Returns the current system time as a FILETIME structure.
' ========================================================================================
PRIVATE FUNCTION AfxSystemFileTime () AS FILETIME
   DIM ST AS SYSTEMTIME, FT AS FILETIME
   GetSystemTime @ST
   SystemTimeToFileTime @ST, @FT
   RETURN FT
END FUNCTION


I thought that it did exactly what you were posting in one of the earlier posts.

The code I posted creates your FT variable and assigns the FT result from the call to AfxSystemFileTime (all in the same one line of code). In FreeBasic you can return TYPE results from a function call. If I remember correctly, that wasn't something that we could do in PowerBasic.
deltarho[1859]
Posts: 2585
Joined: Jan 02, 2017 0:34
Location: UK

Re: SystemTimeToFileTime

Postby deltarho[1859] » Mar 06, 2020 20:59

Paul wrote:If I remember correctly, that wasn't something that we could do in PowerBasic.

It isn't and many other things. FreeBASIC is, in many ways, a frustrating language but it has a wealth of features that PowerBASIC does not have.
Pierre Bellisle
Posts: 42
Joined: Dec 11, 2016 17:22

Re: SystemTimeToFileTime

Postby Pierre Bellisle » Mar 07, 2020 1:23

Minor observation. How about using GetSystemTimeAsFileTime()...

Code: Select all

' ========================================================================================
' Returns the current system time as a FILETIME structure.
' ========================================================================================
PRIVATE FUNCTION AfxSystemFileTime () AS FILETIME
   DIM FT AS FILETIME
   GetSystemTimeAsFileTime @FT
   RETURN FT
END FUNCTION
deltarho[1859]
Posts: 2585
Joined: Jan 02, 2017 0:34
Location: UK

Re: SystemTimeToFileTime

Postby deltarho[1859] » Mar 07, 2020 2:08

Thanks, Pierre.

I have actually finished this project now, according to my initial brief, and it has passed a bunch of values at Wikipedia but I'd still like to see if I can break it in the next few days and if all is well publish it on the forum midweek.

I am using GetSystemTimeEx which is my version of GetSystemTime and uses GetSystemTimePreciseAsFileTime. That is a bit 'over the top' because I only want millisecond resolution but it saves messy about with timeBeginPeriod.

The variable epoch seems to be working OK - for the last half hour I have been using 1 January 1601. I shouldn't think that an old epoch will slow things down much but it is things like that I need to test. I may also add a few procedures like PowerBASIC's Powertime object.

Return to “Windows”

Who is online

Users browsing this forum: No registered users and 3 guests