I'm a VC programmer, ocasionally I try to get familiar with VB.NET, but frankly each time I quit being disapointed by its limitations. But that's not the point. Somebody asked me for help to write the VB.NET code to list the browsing history.
First I wrote it in VC, it works nice. Now I managed to do the same in VB, except the fact that I'm unable to deal with the conversion from FILETIME to SYSTEMTIME. I'm supposed to call the API function FileTimeToSystemTime but it doesn't work. The problem is that I don't have any experience using COM in VB.NET. There's (at least) one error in my code, but I can't find it.
Here's a sample, simplified as much as possible. I created a Windows Forms application, leaving everything with the default values. The whole code associated with Form1 follows:
First of all I would like to use the latest technology so I don't want to use "Declare".
Note that the two values passed to FileTime variable are a valid FILETIME value (I checked it using Visual C++ code). It refers to March 15, 2009.
To me it appears that a wrong function is called (not the FileTimeToSystemTime function). I have two reasons to believe that:
1. The GetLastWin32Error function returns the error code 1400. That stands for INVALID_WINDOW_HANDLE, but the function is not supposed to deal with any window.
2. The function call
modifies the value of the SysTimeBfr, even though it's not supposed to do that. The function should just fill in the correponding memory area with the requested info.
What am I doing wrong ? Any help would be greatly appreciated.
First I wrote it in VC, it works nice. Now I managed to do the same in VB, except the fact that I'm unable to deal with the conversion from FILETIME to SYSTEMTIME. I'm supposed to call the API function FileTimeToSystemTime but it doesn't work. The problem is that I don't have any experience using COM in VB.NET. There's (at least) one error in my code, but I can't find it.
Here's a sample, simplified as much as possible. I created a Windows Forms application, leaving everything with the default values. The whole code associated with Form1 follows:
VB.NET:
Imports System.Runtime.InteropServices
Public Class Form1
Private Structure SYSTEMTIME
Public wYear As Short
Public wMonth As Short
Public wDayOfWeek As Short
Public wDay As Short
Public wHour As Short
Public wMinute As Short
Public wSecond As Short
Public wMilliseconds As Short
End Structure
<DllImport("kernel32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _
Private Shared Function FileTimeToSystemTime( _
ByRef lpFileTime As IntPtr, _
ByRef lpSystemTime As IntPtr _
) As Integer
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim SysTimeBfr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(SYSTEMTIME)))
Dim FileTimeBfr As IntPtr = Marshal.AllocHGlobal(Marshal.SizeOf(GetType(ComTypes.FILETIME)))
Dim SysTime As SYSTEMTIME
Dim FileTime As ComTypes.FILETIME
FileTime.dwHighDateTime = 29992273
FileTime.dwLowDateTime = -23761104 ' I swear these values are valid
Marshal.StructureToPtr(FileTime, FileTimeBfr, True)
Dim ret As Integer = FileTimeToSystemTime(FileTimeBfr, SysTimeBfr)
Dim err As UInteger = Marshal.GetLastWin32Error()
SysTime = Marshal.PtrToStructure(SysTimeBfr, GetType(SYSTEMTIME))
End Sub
End Class
First of all I would like to use the latest technology so I don't want to use "Declare".
Note that the two values passed to FileTime variable are a valid FILETIME value (I checked it using Visual C++ code). It refers to March 15, 2009.
To me it appears that a wrong function is called (not the FileTimeToSystemTime function). I have two reasons to believe that:
1. The GetLastWin32Error function returns the error code 1400. That stands for INVALID_WINDOW_HANDLE, but the function is not supposed to deal with any window.
2. The function call
VB.NET:
FileTimeToSystemTime(FileTimeBfr, SysTimeBfr)
What am I doing wrong ? Any help would be greatly appreciated.