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

Posted on 2000-05-02
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.
Question by:stesherlock
  • 2
LVL 28

Expert Comment

ID: 2771589
LVL 15

Accepted Solution

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
lConnection = IsNetConnected()
If Not lConnected And lConnection Then
    WriteToLog "Connection opened"
ElseIf lConnected And Not lConnection Then
   WriteToLog "Connection closed"
    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
        GetUser = Left$(UserName, size)
    End If
End Function

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

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

End Sub


Author Comment

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!

Author Comment

ID: 2950120
Comment accepted as answer

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

777 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