Solved

Winsock in ActiveX DLL

Posted on 2007-11-19
8
480 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 26

Expert Comment

by:EDDYKT
ID: 20312710
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
ID: 20313728
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
ID: 20314400
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
Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

 

Author Comment

by:Tpaul_10
ID: 20315591
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
 
LVL 26

Expert Comment

by:EDDYKT
ID: 20318979
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
ID: 20321397
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
ID: 20321788
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
ID: 20440262
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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

730 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