how do I Convert Filetime to datetim
-
Thanks for the response. I am taking the filetime from the System.Runtime.InteropServices. I am not defining my own structure. However when I use CType(ft.dwLowDateTime, UInt32) I get the error message that I cannot convert from integer to UInt32 for some reason VB assumes the ft.dwLowDateTime is an integer, as in C# this doesn't seem to be the problem. How can I change that? Cheers David Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...
I finally found the code you're trying to convert by hand. I got this to work:
Private Declare Auto Function SystemTimeToFileTime Lib "kernel32" ( \_ <\[In\]()> ByRef st As SYSTEMTIME, \_ ByRef lpFileTime AS FILETIME) \_ As Boolean
<StructLayout(LayoutKind.Sequential)> _
Private Structure SYSTEMTIME
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMillisecond As UInt16
End Structure
.
.
.
Private Sub Button1_Click(blah, blah) Handles Button1.Click
Dim st As SYSTEMTIME
With st
.wDay = 14
.wMonth = 4
.wYear = 3606 ' 2006 + 1600
.wHour = 14
.wMinute = 15
.wSecond = 31
End With
Dim ft As New FILETIME
SystemTimeToFileTime(st, ft)
Dim dt As New DateTime(((CType(ft.dwHighDateTime, Long)) << 32) Or _
CType(ft.dwLowDateTime, UInt32))
Debug.WriteLine(dt)
End SubDave Kreskowiak Microsoft MVP - Visual Basic
-
I finally found the code you're trying to convert by hand. I got this to work:
Private Declare Auto Function SystemTimeToFileTime Lib "kernel32" ( \_ <\[In\]()> ByRef st As SYSTEMTIME, \_ ByRef lpFileTime AS FILETIME) \_ As Boolean
<StructLayout(LayoutKind.Sequential)> _
Private Structure SYSTEMTIME
Public wYear As UInt16
Public wMonth As UInt16
Public wDayOfWeek As UInt16
Public wDay As UInt16
Public wHour As UInt16
Public wMinute As UInt16
Public wSecond As UInt16
Public wMillisecond As UInt16
End Structure
.
.
.
Private Sub Button1_Click(blah, blah) Handles Button1.Click
Dim st As SYSTEMTIME
With st
.wDay = 14
.wMonth = 4
.wYear = 3606 ' 2006 + 1600
.wHour = 14
.wMinute = 15
.wSecond = 31
End With
Dim ft As New FILETIME
SystemTimeToFileTime(st, ft)
Dim dt As New DateTime(((CType(ft.dwHighDateTime, Long)) << 32) Or _
CType(ft.dwLowDateTime, UInt32))
Debug.WriteLine(dt)
End SubDave Kreskowiak Microsoft MVP - Visual Basic
I am getting imediate troubles with .wDay = 14 saying that that cannot be converted to Uint16. Could it be that I need to change one of these option setting. Forgot the name, but as you used to have option strict etc. Be the way thanks for the help, much appreciated. Just one other thing what does << mean can't find that in the help file as it is an illegal entry there. Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...
-
I am getting imediate troubles with .wDay = 14 saying that that cannot be converted to Uint16. Could it be that I need to change one of these option setting. Forgot the name, but as you used to have option strict etc. Be the way thanks for the help, much appreciated. Just one other thing what does << mean can't find that in the help file as it is an illegal entry there. Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...
I'm using VB 2005, and you're not. That's why the problems... "<<" is a bit-wise shift operator that finally appeared (many years too late!) in 2005. To get the same effect using math, you need to multiply the value by 2^(number of bits to shift left). Sooooo, the same operation can be done like this:
Dim dt As New DateTime(((CType(ft.dwHighDateTime, Long)) * (2 ^ 32) Or CType(ft.dwLowDateTime, UInt32))
Because of the math, it's quite a bit slower, but it does the same thing. As for the other problem, IIRC, since the values involved are small, you can get away with changing the data types in the SYSTEMTIME structure to
Int16
and it'll still work.<StructLayout(LayoutKind.Sequential)> _
Private Structure SYSTEMTIME
Public wYear As Int16
Public wMonth As Int16
Public wDayOfWeek As Int16
Public wDay As Int16
Public wHour As Int16
Public wMinute As Int16
Public wSecond As Int16
Public wMillisecond As Int16
End StructureDave Kreskowiak Microsoft MVP - Visual Basic
-
I'm using VB 2005, and you're not. That's why the problems... "<<" is a bit-wise shift operator that finally appeared (many years too late!) in 2005. To get the same effect using math, you need to multiply the value by 2^(number of bits to shift left). Sooooo, the same operation can be done like this:
Dim dt As New DateTime(((CType(ft.dwHighDateTime, Long)) * (2 ^ 32) Or CType(ft.dwLowDateTime, UInt32))
Because of the math, it's quite a bit slower, but it does the same thing. As for the other problem, IIRC, since the values involved are small, you can get away with changing the data types in the SYSTEMTIME structure to
Int16
and it'll still work.<StructLayout(LayoutKind.Sequential)> _
Private Structure SYSTEMTIME
Public wYear As Int16
Public wMonth As Int16
Public wDayOfWeek As Int16
Public wDay As Int16
Public wHour As Int16
Public wMinute As Int16
Public wSecond As Int16
Public wMillisecond As Int16
End StructureDave Kreskowiak Microsoft MVP - Visual Basic
Then I still have the problem that I cannot convert the dwLowDateTime to Uint32. As in the original problem. The thing I don't get is that this convertion works fine for C# and vb.net Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...
-
Then I still have the problem that I cannot convert the dwLowDateTime to Uint32. As in the original problem. The thing I don't get is that this convertion works fine for C# and vb.net Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...
OK. You can change that
UInt32
toLong
an it'll work. Dave Kreskowiak Microsoft MVP - Visual Basic -
OK. You can change that
UInt32
toLong
an it'll work. Dave Kreskowiak Microsoft MVP - Visual BasicHi, Tried this, assuming that is what you meant ... Dim dt As DateTime = New DateTime(((CType(ft.dwHighDateTime, Long)) << 32) Or CType(ft.dwLowDateTime, Long)) But then I get the exception message: "Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks" Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...
-
Hi, Tried this, assuming that is what you meant ... Dim dt As DateTime = New DateTime(((CType(ft.dwHighDateTime, Long)) << 32) Or CType(ft.dwLowDateTime, Long)) But then I get the exception message: "Ticks must be between DateTime.MinValue.Ticks and DateTime.MaxValue.Ticks. Parameter name: ticks" Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...
Hmmmm...I never got that message in testing. What's the value of the High and Low in ft when it fails? Even better, also, what's the values in st before the call to SystemTimeToFileTime? Dave Kreskowiak Microsoft MVP - Visual Basic -- modified at 18:50 Thursday 13th April, 2006
-
Hmmmm...I never got that message in testing. What's the value of the High and Low in ft when it fails? Even better, also, what's the values in st before the call to SystemTimeToFileTime? Dave Kreskowiak Microsoft MVP - Visual Basic -- modified at 18:50 Thursday 13th April, 2006
Here are the value for st : wDay: 31 wDayOfWeek: 5 wHour: 23 wMilliseconds: 0 wMinute: 0 wMonth: 12 wSecond: 0 wYear: 3604 whith wich values are you testing? Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...
-
Here are the value for st : wDay: 31 wDayOfWeek: 5 wHour: 23 wMilliseconds: 0 wMinute: 0 wMonth: 12 wSecond: 0 wYear: 3604 whith wich values are you testing? Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...
OK. This should do it. The problem is that the bitwise math isn't working as it should because one of the value types in the FILETIME structure isn't what it appeared to be.
Dim dt As New DateTime((ft.dwHighDateTime \* (2 ^ 32)) + ft.dwLowDateTime)
I'd beat the snot out of this code before putting it into production. Dave Kreskowiak Microsoft MVP - Visual Basic -- modified at 19:42 Thursday 13th April, 2006
-
OK. This should do it. The problem is that the bitwise math isn't working as it should because one of the value types in the FILETIME structure isn't what it appeared to be.
Dim dt As New DateTime((ft.dwHighDateTime \* (2 ^ 32)) + ft.dwLowDateTime)
I'd beat the snot out of this code before putting it into production. Dave Kreskowiak Microsoft MVP - Visual Basic -- modified at 19:42 Thursday 13th April, 2006
Hi Dave, Fixed it in a more convential way ... : Private Shared Function SystemTimeToDateTime(ByRef st As SYSTEMTIME) As DateTime Dim dt As DateTime Dim dtFormat As String Dim en As New CultureInfo("en-GB") dtFormat = "dd/MM/yyyy HH:mm" With st dt = DateTime.ParseExact( _ Format$(.wDay, "00") & "/" & _ Format$(.wMonth, "00") & "/" & _ Format$(.wYear - 1600) & " " & _ Format$(.wHour, "00") & ":" & _ Format$(.wMinute, "00"), dtFormat, en.DateTimeFormat) ' & ":" & _ ' Format$(.wSecond, "00"), dtFormat, en.DateTimeFormat) End With Return dt End Function Gues it is a bit less efficient, not sure how much though. Your remark about the code and the snot was that just about this problem or also about the rest of the code. If so could you give me some pointers, guess it is never too late to learn. :)) Cheers David Think, try, think, think, try, think, think, think, try, ASK, think, try, advance on step and start over...