Solved

Winsock in ActiveX DLL

Posted on 2007-11-19
8
474 Views
Last Modified: 2013-11-18
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
 
0
Comment
Question by:Tpaul_10
  • 4
  • 4
8 Comments
 
LVL 26

Expert Comment

by:EDDYKT
Comment Utility
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

0
 

Author Comment

by:Tpaul_10
Comment Utility
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
0
 
LVL 26

Expert Comment

by:EDDYKT
Comment Utility
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

0
 

Author Comment

by:Tpaul_10
Comment Utility
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
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 26

Expert Comment

by:EDDYKT
Comment Utility
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
0
 

Author Comment

by:Tpaul_10
Comment Utility
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.
0
 
LVL 26

Accepted Solution

by:
EDDYKT earned 500 total points
Comment Utility
if it works for you

then

put this in your form

Option Explicit

' hide window from task manager
Const SW_HIDE = 0
Const GW_OWNER = 4

'Window always on top
Const SWP_NOACTIVATE = &H10
Const SWP_NOMOVE = &H2
Const SWP_NOSIZE = &H1

Private Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Private Declare Function SetWindowPos Lib "user32" (ByVal hwnd As Long, ByVal hWndInsertAfter As Long, ByVal X As Long, ByVal Y As Long, ByVal cx As Long, ByVal cy As Long, ByVal wFlags As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Private Sub Form_Load()

    ShowWindow GetWindow(hwnd, GW_OWNER), SW_HIDE
    SetWindowPos hwnd, -1, 0, 0, 0, 0, SWP_NOACTIVATE Or SWP_NOMOVE Or SWP_NOSIZE
End Sub

change to
load form1
form1.top = -10000
form1.left = -10000
fom1.show
0
 

Author Comment

by:Tpaul_10
Comment Utility
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.
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now