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
SireeshaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.