Sireesha
asked on
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.NullReferenceExcep tion' 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
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)
End Function
Error is
An unhandled exception of type 'System.NullReferenceExcep
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
ASKER
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.
ASKER
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
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
Why not just use the built in System.TimeZone class and avoid the whole problem?
ASKER
I need to use this to scheduled the customer call backs with the Melita dialers.Thanks.
Private Declare Function GetTimeZoneInformation Lib "kernel32" (ByVal ByReflpTimeZoneInformation As Form1.TIME_ZONE_INFORMATIO N) 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
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
ASKER
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_informatio n 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.
ASKER
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.InteropServ ices.Marsh alAs(Syste m.Runtime. InteropSer vices.Unma nagedType. ByValTStr, SizeConst:=64)> Public StandardName As String
Dim StandardDate As SYSTEMTIME
Dim StandardBias As Integer
<VBFixedString(64), System.Runtime.InteropServ ices.Marsh alAs(Syste m.Runtime. InteropSer vices.Unma nagedType. 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
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.InteropServ
Dim StandardDate As SYSTEMTIME
Dim StandardBias As Integer
<VBFixedString(64), System.Runtime.InteropServ
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
ASKER
How do I close this one now since I found the resolution on a different site?Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
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