[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1520
  • Last Modified:

Issues with Using GetTimeZoneInformation Kernel function in VB.Net

Hello,
  I am having trouble using GetTimeZoneInformation function in VB.Net.
Here is what I have

  Private Declare Function GetTimeZoneInformation _
   Lib "kernel32" (ByRef lpTimeZoneInformation As _
  TIME_ZONE_INFORMATION) As Long

   Private Structure TIME_ZONE_INFORMATION
        Dim bias As Long
        Dim StandardName() As Byte
        Dim StandardDate As SYSTEMTIME
        Dim StandardBias As Long
        Dim DaylightName() As Byte
        Dim DaylightDate As SYSTEMTIME
        Dim DaylightBias As Long
 Public Sub Initialize()
            ReDim StandardName(63)
            ReDim DaylightName(63)
 End Sub
End Structure

Here is where I am calling the function from

 Public Function convertToGmt(ByVal st As String) As String
        Dim nRet As Long
        Dim bias As Long
        Dim tz As TIME_ZONE_INFORMATION
        st = Format(st, "General Date")
        tz.Initialize()
        nRet = GetTimeZoneInformation(tz)  'Getting Error At this line
End Function

Error is
An unhandled exception of type 'System.NullReferenceException' occurred in OBTMTrackingTool.exe
Additional information: Object reference not set to an instance of an object.

PLEASE HELP!! Tried No of things and could't figure it out.I really appreciate your help and Thanks in Advance
0
Sireesha
Asked:
Sireesha
1 Solution
 
davidrichardsonCommented:
Just Found this havent tried it

    Structure SYSTEMTIME
        Dim wYear As Short
        Dim wMonth As Short
        Dim wDayOfWeek As Short
        Dim wDay As Short
        Dim wHour As Short
        Dim wMinute As Short
        Dim wSecond As Short
        Dim wMilliseconds As Short
    End Structure

    Private Structure TIME_ZONE_INFORMATION
        Dim bias As Long
        Dim StandardName() As Byte
        Dim StandardDate As SYSTEMTIME
        Dim StandardBias As Long
        Dim DaylightName() As Byte
        Dim DaylightDate As SYSTEMTIME
        Dim DaylightBias As Long
        Public Sub Initialize()
            ReDim StandardName(63)
            ReDim DaylightName(63)
        End Sub
    End Structure

    Private Declare Function GetTimeZoneInformation Lib "kernel32" (ByVal ByReflpTimeZoneInformation As Form1.TIME_ZONE_INFORMATION) As Integer
    Public Function convertToGmt(ByVal st As String) As String
        Dim nRet As Long
        Dim bias As Long
        Dim tz As TIME_ZONE_INFORMATION
        st = Format(st, "General Date")
        tz.Initialize()
        nRet = GetTimeZoneInformation(tz)
    End Function
0
 
SireeshaAuthor Commented:
Thanks.I think there is something wrong with the way I am using arrays b'z when I comment them out I am not getting an error when calling nRet = GetTimeZoneInformation(tz) but the bias values are not right.So I need to get this line working with the existing structure.
 
0
 
SireeshaAuthor Commented:
Also missed to Include SYSTEMTIME structure that I am using

Private Structure SYSTEMTIME
            Dim wYear As Integer
            Dim wMonth As Integer
            Dim wDayOfWeek As Integer
            Dim wDay As Integer
            Dim wHour As Integer
            Dim wMinute As Integer
            Dim wSecond As Integer
            Dim wMilliseconds As Integer
        End Structure
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Joe_GriffithCommented:
Why not just use the built in System.TimeZone class and avoid the whole problem?
0
 
SireeshaAuthor Commented:
I need to use this to scheduled the customer call backs with the Melita dialers.Thanks.
0
 
DotNetLover_BaanCommented:
  Private Declare Function GetTimeZoneInformation Lib "kernel32" (ByVal ByReflpTimeZoneInformation As Form1.TIME_ZONE_INFORMATION) As Integer
    Public Function convertToGmt(ByVal st As String) As String
        Dim nRet As Long
        Dim bias As Long

        Dim tz As New TIME_ZONE_INFORMATION   'Add 'New' to the instantiation

        st = Format(st, "General Date")
        tz.Initialize()
        nRet = GetTimeZoneInformation(tz)
    End Function

The NullException occurs because there is no object of the timezone class, since u haven't used New...

-Baan
0
 
SireeshaAuthor Commented:
Thanks Baan.I am sure about using New keyword to instantiate but I didn't find any difference and thought that I was still getting this error.One difference that I didn't try was using Form1.Time_zone_information in the function declaration.I will try that and as well as New keyword to instantiate the timezone on Monday when I get back to work and let you know how it goes.Thanks for your help.
0
 
SireeshaAuthor Commented:
Somebody at this link made my day today.
Here is the working code if anybody is interested.Look for the changed in Time_zone_Information Structure I really appreciate any help that you have provided.

http://www.dotnet247.com/247reference/msgs/20/100758.aspx

    Private Declare Function GetTimeZoneInformation _
   Lib "kernel32" (ByRef lpTimeZoneInformation As _
  TIME_ZONE_INFORMATION) As Long
    Structure SYSTEMTIME
        Dim wYear As Short
        Dim wMonth As Short
        Dim wDayOfWeek As Short
        Dim wDay As Short
        Dim wHour As Short
        Dim wMinute As Short
        Dim wSecond As Short
        Dim wMilliseconds As Short
    End Structure

    Private Structure TIME_ZONE_INFORMATION
        Dim Bias As Integer
        <VBFixedString(64), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=64)> Public StandardName As String
        Dim StandardDate As SYSTEMTIME
        Dim StandardBias As Integer
        <VBFixedString(64), System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst:=64)> Public DaylightName As String
        Dim DaylightDate As SYSTEMTIME
        Dim DaylightBias As Integer
    End Structure
    Private Const TIME_ZONE_ID_INVALID = &HFFFFFFFF
    Private Const TIME_ZONE_ID_UNKNOWN = 0
    Private Const TIME_ZONE_ID_STANDARD = 1
    Private Const TIME_ZONE_ID_DAYLIGHT = 2
    Public Function convertToGmt(ByVal st As String) As String
        On Error Resume Next
        Dim nRet As Long
        Dim bias As Long
        Dim tz As TIME_ZONE_INFORMATION
        st = Format(st, "General Date")

        nRet = GetTimeZoneInformation(tz)

        If nRet <> TIME_ZONE_ID_INVALID Then
            Select Case nRet
                Case TIME_ZONE_ID_UNKNOWN
                    MsgBox("Unknown Time Zone", , "Error!")
                Case TIME_ZONE_ID_STANDARD
                    bias = tz.bias + tz.StandardBias
                Case TIME_ZONE_ID_DAYLIGHT
                    bias = tz.bias + tz.DaylightBias
            End Select
            convertToGmt = DateAdd("n", bias, st)
        End If
    End Function
0
 
SireeshaAuthor Commented:
How do I close this one now since I found the resolution on a different site?Thanks
0
 
moduloCommented:
PAQed with points refunded (500)

modulo
Community Support Moderator
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now