Click here to Skip to main content
13,731,697 members
Click here to Skip to main content

Stats

12.3K views
696 downloads
12 bookmarked
Posted 15 Apr 2012
Licenced CPOL

VB.NET wrappers for much of the Windows API

, 15 Apr 2012
Includes most API functions except for graphics.
UtilitiesLib
bin
Debug
sclib.dll
sclib.pdb
Release
sclib.dll
sclib.pdb
My Project
Application.myapp
vssver2.scc
obj
Debug
DesignTimeResolveAssemblyReferencesInput.cache
Salience.Library.Resources.resources
sclib.dll
sclib.pdb
TempPE
My Project.Resources.Designer.vb.dll
UtilitiesLib.vbproj.GenerateResource.Cache
Release
DesignTimeResolveAssemblyReferencesInput.cache
GenerateResource-ResGen.read.1.tlog
GenerateResource-ResGen.write.1.tlog
Salience.Library.Resources.resources
sclib.dll
sclib.pdb
TempPE
My Project.Resources.Designer.vb.dll
UtilitiesLib.suo
Imports System.ComponentModel
Imports System.Runtime.InteropServices
Imports Microsoft.Win32
Imports Microsoft.Win32.SafeHandles
Imports System.Runtime.InteropServices.ComTypes

Public Class TimeApi

    Public Structure SYSTEMTIME
        Dim Year As Integer
        Dim Month As Integer
        Dim DayOfWeek As Integer
        Dim Day As Integer
        Dim Hour As Integer
        Dim Minute As Integer
        Dim Second As Integer
        Dim Milliseconds As Integer
    End Structure

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function FileTimeToLocalFileTime( _
        <[In]()> ByRef lpFileTime As ComTypes.FILETIME, _
        <Out()> ByRef lpLocalFileTime As ComTypes.FILETIME) As Boolean
    End Function

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function FileTimeToSystemTime( _
        <[In]()> ByRef lpFileTime As ComTypes.FILETIME, _
        <Out()> ByRef lpSystemTime As SYSTEMTIME) As Boolean
    End Function

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function SystemTimeToFileTime( _
        <[In]()> ByRef lpSystemTime As SYSTEMTIME, _
        <Out()> ByRef lpFileTime As ComTypes.FILETIME) As Boolean
    End Function

    <DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True)> _
    Public Shared Function LocalFileTimeToFileTime( _
        <[In]()> ByRef lpLocalFileTime As ComTypes.FILETIME, _
        <Out()> ByRef lpFileTime As ComTypes.FILETIME) As Boolean
    End Function

    Public Shared Function DateTimeToFileTime(ByVal d As DateTime) As ComTypes.FILETIME
        Dim ft As New ComTypes.FILETIME()
        Dim tmp As Long = d.ToFileTime()
        'Logger.WriteEntry(d.ToString())
        'Dim A As Integer = CInt(tmp) 'And &HFFFFFFFF
        ft.dwLowDateTime = CInt(tmp >> 32) 'And &HFFFFFFFF
        ft.dwHighDateTime = CInt(tmp >> 32)
        Return ft
    End Function

    'Public Shared Function DateTimeToFileTime(ByVal d As DateTime) As ComTypes.FILETIME
    '    Dim ft As New ComTypes.FILETIME()
    '    Dim tmp As Long = d.ToFileTime()
    '    ft.dwLowDateTime = CInt(tmp And &HFFFFFFFF)
    '    ft.dwHighDateTime = CInt(tmp >> 32)
    '    Return ft
    'End Function

    Public Shared Function DateTimeToFileTimeUtc(ByVal d As DateTime) As ComTypes.FILETIME
        Dim ft As New ComTypes.FILETIME()
        Dim tmp As Long = d.ToFileTimeUtc()
        ft.dwLowDateTime = CInt(tmp And &HFFFFFFFF)
        ft.dwHighDateTime = CInt(tmp >> 32)
        Return ft
    End Function

    Public Shared Function FileTimeToDateTime(ByVal ft As ComTypes.FILETIME) As DateTime
        Dim dt As DateTime = DateTime.MaxValue
        Dim tmp As Long
        tmp = (CLng(ft.dwHighDateTime) << 32) + ft.dwLowDateTime

        Try
            dt = DateTime.FromFileTime(tmp)
        Catch ex As ArgumentOutOfRangeException
            dt = DateTime.MaxValue
        End Try
        Return dt
    End Function

    Public Shared Function FileTimeToDateTimeUtc(ByVal ft As ComTypes.FILETIME) As DateTime
        Dim dt As DateTime = DateTime.MaxValue
        Dim tmp As Long
        tmp = (CLng(ft.dwHighDateTime) << 32) + ft.dwLowDateTime

        Try
            dt = DateTime.FromFileTimeUtc(tmp)
        Catch ex As ArgumentOutOfRangeException
            dt = DateTime.MaxValue
        End Try
        Return dt
    End Function

    'Public Shared Function SystemTimeToFileTimeUtc(ByVal st As SYSTEMTIME) As FILETIME
    '    Dim ft As New FILETIME
    '    If Not TimeApi.SystemTimeToFileTime(st, ft) Then
    '        Throw New Win32Exception(Marshal.GetLastWin32Error())
    '    End If
    '    If LocalFileTimeToFileTime(ft, ft) Then
    '        Throw New Win32Exception(Marshal.GetLastWin32Error())
    '    End If
    '    Return ft
    'End Function

    'Public Shared Function DateTimeToSystemTime(ByVal d As DateTime) As SYSTEMTIME
    '    Dim st As SYSTEMTIME
    '    st.Month = d.Month - 1
    '    st.Day = d.Day - 1
    '    st.DayOfWeek = d.DayOfWeek - 1
    '    st.Year = d.Year - 1
    '    st.Hour = d.Hour - 1
    '    st.Minute = d.Minute - 1
    '    st.Second = d.Second - 1
    '    st.Milliseconds = d.Millisecond - 1
    '    Return st
    'End Function

End Class

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

andrewbb@gmail.com
Architect
United States United States
No Biography provided

You may also be interested in...

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web01-2016 | 2.8.180920.1 | Last Updated 15 Apr 2012
Article Copyright 2012 by andrewbb@gmail.com
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid