Link to home
Start Free TrialLog in
Avatar of Tpaul_10
Tpaul_10Flag for United States of America

asked on

Winsock in ActiveX DLL

Hi,
I have a Winsock control in ActiveXDLL.I am trying to get time from the internet ,then i will compare that internet time with my time if the condition passes Show Form2  else show my message "Expired" I  have 2 Forms( Form1 and Form2) My winsock1 control is in Form1 but i don't want to show my Form1.
My code is working in standard vb project but its not working in Activex DLL.
I think i am doing some thing wrong in my class module.

Here is my code:
.................................................in Form1
Dim sTime As String
Dim sNTP As String
Dim TimeDelay As Single
Private Type SYSTEMTIME
  wYear As Integer
  wMonth As Integer
  wDayOfWeek As Integer
  wDay As Integer
  wHour As Integer
  wMinute As Integer
  wSecond As Integer
  wMilliseconds As Integer
End Type

Private Declare Function SetSystemTime Lib "kernel32" _
   (lpSystemTime As SYSTEMTIME) As Long



Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal lpszAgent As String, ByVal dwAccessType As Long, ByVal lpszProxyName As String, ByVal lpszProxyBypass As String, ByVal dwFlags As Long) As Long
Private Declare Function InternetOpenUrl Lib "wininet.dll" Alias "InternetOpenUrlA" (ByVal hInet As Long, ByVal lpszUrl As String, ByVal lpszHeaders As String, ByVal dwHeadersLength As Long, ByVal dwFlags As Long, ByVal dwContext As Long) As Long
Private Declare Function InternetCloseHandle Lib "wininet.dll" (ByVal hInet As Long) As Long

Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
Private Const INTERNET_FLAG_RELOAD = &H80000000
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
Dim checkType As Integer
 
Sub Form_Load()
        Winsock1.Close
        Winsock1.LocalPort = 0
        Winsock1.RemoteHost = "time-a.nist.gov"
        Winsock1.RemotePort = 37
        Winsock1.Connect
   
End Sub

Private Sub Form_Unload(Cancel As Integer)
Winsock1.Close
End Sub

Private Sub Winsock1_Close()
Winsock1.Close
End Sub

Private Sub Winsock1_DataArrival(ByVal bytesTotal As Long)
        Dim strData As String
       Winsock1.GetData strData
       Call SyncSystemClock(strData)
End Sub

Sub SyncSystemClock(ByVal sTime As String)
   Dim NTPTime As Double
   Dim UTCDATE As Date
   Dim dwSecondsSince1990 As Long
   Dim ST As SYSTEMTIME
     
   sTime = Trim(sTime)
   
If Len(sTime) = 4 Then
   
     'since the data was returned in a string,
     'format it back into a numeric value
      NTPTime = Asc(Left$(sTime, 1)) * (256 ^ 3) + _
                Asc(Mid$(sTime, 2, 1)) * (256 ^ 2) + _
                Asc(Mid$(sTime, 3, 1)) * (256 ^ 1) + _
                Asc(Right$(sTime, 1))
     
      dwSecondsSince1990 = NTPTime - 2840166000#
      UTCDATE = DateAdd("s", CDbl(dwSecondsSince1990), #1/1/1990#)
         If DateValue(UTCDATE) > #8/1/2020# Then
          Unload Form1
         Form1.Hide
       MsgBox "Trail version has expired "
   
     Else
             Unload Form1
            Form1.Hide
             Form2.show
           
 End If
End If
End Sub

..........................in my Class module
Public Sub ShowTime()
Dim frmT As Form1
Set frmT = New Form1
Load frmT
frmT.Show
Set frmT = Nothing
End Sub

I know i have frmT.Show in my class module, but i want to check the time without showing my form1,
Please guide me where i am doing wrong ..........
Here is my problem
1)My code is not working at all for the first run
2)With out showing form1 i want to check time
3)Sometimes it gives me runtime error "40006"

Thanks
 
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Public Sub ShowTime()
Dim frmT As Form1
Set frmT = New Form1
Load frmT
frmT.Show   ' remove this line if you don't want to show the form
Set frmT = Nothing  ' this will terminate your form, may be too fast to terminate
End Sub

You can do something like this


Option Explicit
 
Private frm As Form1
Private WithEvents wins As Winsock
 
Private Sub Class_Initialize()
    If (frm Is Nothing) Then
        Set frm = New Form1
        Set wins = frm.Winsock1
        wins.Close
        wins.LocalPort = 0
        wins.RemoteHost = "time-a.nist.gov"
        wins.RemotePort = 37
        wins.Connect
    End If
End Sub
 
Private Sub Class_Terminate()
    wins.Close
    Set wins = Nothing
    Set frm = Nothing
End Sub
 
Private Sub wins_Close()
    wins.Close
End Sub
 
Private Sub Wins_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String
    wins.GetData strData
    Call SyncSystemClock(strData)
End Sub
 
Private Sub SyncSystemClock(ByVal sTime As String)
   Dim NTPTime As Double
   Dim UTCDATE As Date
   Dim dwSecondsSince1990 As Long
   Dim ST As SYSTEMTIME
     
   sTime = Trim(sTime)
   
    If Len(sTime) = 4 Then
   
        'since the data was returned in a string,
        'format it back into a numeric value
        NTPTime = Asc(Left$(sTime, 1)) * (256 ^ 3) + _
                Asc(Mid$(sTime, 2, 1)) * (256 ^ 2) + _
                Asc(Mid$(sTime, 3, 1)) * (256 ^ 1) + _
                Asc(Right$(sTime, 1))
      
        dwSecondsSince1990 = NTPTime - 2840166000#
        UTCDATE = DateAdd("s", CDbl(dwSecondsSince1990), #1/1/1990#)
        If DateValue(UTCDATE) > #8/1/2020# Then
            MsgBox "Trail version has expired "
        Else
            Form2.Show
            
        End If
    End If
End Sub

Open in new window

Avatar of Tpaul_10

ASKER

Hi EDDYKT Thanks for your response.
I tried your code.But its not working.No response at all.
if i try put msgboxes every where then its working on the second run.

Thanks
you need to wait for connection estlished first

ie

Private Sub Wins_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String
    If wins.State = sckConnected Then
        wins.GetData strData
        Call SyncSystemClock(strData)
    end if 
End Sub

Open in new window

No Luck.
If i put message boxes in all my subs its working other wise no response at all
Do i need to take care of Events here.
Thanks
add a line to see wehter you get the event back

Private Sub Wins_DataArrival(ByVal bytesTotal As Long)
    Dim strData As String
msgbox len(strdata) & "   " & wins.state   ' add this line
If wins.State = sckConnected Then
        wins.GetData strData
        Call SyncSystemClock(strData)
    end if
End Sub
Hi EDDYKT Thank you so much for your response.
If i say load form1
 fom1.show its working fine.
But i don't want my form to show so if i say just load form1 its not giving me any reponse at all (i think its not calling SyncSystemClock(strData))
Thanks.
ASKER CERTIFIED SOLUTION
Avatar of EDDYKT
EDDYKT
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi EDDYKT
For some reason it didn't work, so what i did was i manually change the form1 properties from properties window it did work.
Thanks for all your help.