Solved

A few questions in one! All about modems and the sys tray.

Posted on 2000-05-02
4
139 Views
Last Modified: 2012-05-04
OK. Firstly, I need to know how to detect when my modem both connects and disconnects from my ISP. I need to be able to time the interval between.

Secondly, I'd like to know how to minimize my program to the system tray (the one on the bottom right by the clock, if I've got the name wrong!).

Thanks in advance.
0
Comment
Question by:stesherlock
  • 2
4 Comments
 
LVL 28

Expert Comment

by:AzraSound
ID: 2771589
0
 
LVL 15

Accepted Solution

by:
cquinn earned 60 total points
ID: 2772518
The following is the code of an application I have to log all modem connections.  I have it in startup and it logs all connections into a separate log file for each day, named in the format YYYYMMDD.LOG, in the same directory as the application.  I use it to calculate connection times for myself, my wife and my daughter (it writes the logon name to the log) so I have ammunition when the phone bill comes in!  It runs in background and does not appear in the taskbar or task manager list, so they don't know how I know!

Put a timer on a form with a reasonable interval set eg every second or minute.

Add the following code to the form:

Option Explicit

Private Sub Form_Load()
Dim OwnerhWnd As Long
Dim ret As Long

' Make sure the form is invisible:
Me.Visible = False

' Set interval for timer for 5 seconds, and make sure it is enabled:
tmrTimer1.Interval = 10000
tmrTimer1.Enabled = True

' Grab the background or owner window:
OwnerhWnd = GetWindow(Me.hwnd, GW_OWNER)
' Hide from task list:
ret = ShowWindow(OwnerhWnd, SW_HIDE)

ModemLogFile = sFixDir(App.Path) & Format$(Date, "yyyymmdd") & ".log"
sUsername = GetUser()
WriteToLog "Application opened"
End Sub

Private Sub Form_QueryUnload(Cancel As Integer, UnloadMode As Integer)
   WriteToLog "Application closed"
End Sub

Private Sub tmrTimer1_Timer()
Static lConnected As Long, lConnection As Long
DoEvents
lConnection = IsNetConnected()
If Not lConnected And lConnection Then
    WriteToLog "Connection opened"
ElseIf lConnected And Not lConnection Then
   WriteToLog "Connection closed"
Else
    Exit Sub
End If
lConnected = lConnection

End Sub


*********************************
create a module and add the following code:
**********************************

Option Explicit
'Retrieve a network login ID
Public Declare Function WNetGetUser Lib "mpr" Alias "WNetGetUserA" (ByVal lpName As String, ByVal lpusername As String, lpnLength As Long) As Long
Public Declare Function ShowWindow Lib "user32" (ByVal hwnd As Long, ByVal nCmdShow As Long) As Long
Public Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long

Public Declare Function InternetGetConnectedState _
   Lib "wininet.dll" (ByRef lpdwFlags As Long, _
   ByVal dwReserved As Long) As Long

Public ModemLogFile As String
Public sUsername As String

Public Const SW_HIDE = 0
Public Const GW_OWNER = 4

Public Function IsNetConnected() As Boolean

  'if there is a connection
   IsNetConnected = InternetGetConnectedState(0&, 0&)
     
End Function

Public Sub WriteToLog(sAction As String)
Dim iFileNum As Integer
iFileNum = FreeFile()
Open ModemLogFile For Append As iFileNum
Write #iFileNum, sAction; Format$(Now, "hh:nn:ss dd mmm yyyy"); sUsername
Close iFileNum
End Sub

Function GetUser() As String
Dim ReturnCode As Integer
Dim UserName As String
Dim lp As String
    Dim size As Long
    GetUser = "Nonetwrk"
    UserName = String$(255, 0)
    size = Len(UserName)
    ReturnCode = WNetGetUser&(lp, UserName, size)
    If ReturnCode = 0 Then
        While Asc(Mid$(UserName, size, 1)) = 0
            size = size - 1
        Wend
        GetUser = Left$(UserName, size)
    End If
End Function

Public Function sFixDir(sIncoming As String) As String
If Right$(sIncoming, 1) = "\" Then
    sFixDir = sIncoming
Else
    sFixDir = sIncoming & "\"
End If
   
End Function

Public Sub Main()
Call ShowWindow(frmModemLog.hwnd, SW_HIDE)

End Sub



0
 

Author Comment

by:stesherlock
ID: 2775765
I like your prog cquinn, but do you know if I can add the ISP that is connected to the log file?

I ask this because I am signed up with about 5 ISPs, and one or two of them are freephone numbers, so I'd like to be able to know whether or not it adds to the phone bill!
0
 

Author Comment

by:stesherlock
ID: 2950120
Comment accepted as answer
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction While answering a recent question about filtering a custom class collection, I realized that this could be accomplished with very little code by using the ScriptControl (SC) library.  This article will introduce you to the SC library a…
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…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

760 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

18 Experts available now in Live!

Get 1:1 Help Now