Hey Guys,
I've run into a problem that I don't know how to fix.
I have a program that grabs the private IP address for the computer using the Winsock control.
I have it check to see if it has changed and compare to what it was, etc..
Well, if the ip address changes via DHCP renewal, the program sees it just fine everytime it changes. But if I statically assign an IP address, the program only recognizes the change one time. Even though its the same code.
Here is the simplified version of what I'm doing
Code:
Private Sub Refresh_All()
If WS1.State <> sckClosed Then
WS1.Close
End If
strNewLocalIP = WS1.LocalIP
If strNewLocalIP <> strOldLocalIP Then
' do a few updates to database and send email (this works perfectly)
End If
WS1.Close
End Sub
Yes, thats the very simplified version and more or less pseudocode for what I'm doing.
This and the rest of the code are all stored in the Sub. I call the sub when the program loads, and then on a specified timer when it gets to 0. The user can set whether they want it to check on startup. But regardless whether they do or wait for the automatic refresh, it calls the same Sub.
But, if I statically assign the IP address, it only notices the change the first time it runs the sub. Every subsequent attempt doesn't do anything. It's like it doesn't recognize that it has changed.
So, if they have it set to check for changes on startup, when the program loads, it sees the change right away. But anytime it checks after that while its still running, it doesn't notice.
But, if they have it wait until the first refresh to check, it notices during the refresh, but not any other time after that.
I hope that makes enough sense to get the help I need
Thanks for your help, but it is doing the same thing with your code that it does with mine.
If I have your program running, then I go in and manually assign a static IP address, then click the command button again, it doesn't recognize that it has changed. But if I re-run the program, it sees it again.
To me, it seems that Winsock only recognizes the change 1 time. After that, it requires a restart of the whole app to see it again. Which, doesn't make any sense since it is reinitialized every time it is refreshed.
Here is the Code, add Winsock1, and your good to go.
Set Winsock1 Index to 0, then run this
Code:
Option Explicit
Private Sub Command1_Click()
Me.Caption = IPchanged
End Sub
Public Function IPchanged() As Boolean
Static OldIP
Load Winsock1(1)
IPchanged = OldIP <> Winsock1(1).LocalIP
OldIP = Winsock1(1).LocalIP
Unload Winsock1(1)
End Function
Private Sub Form_Load()
Dim InitializeIPcheck
'InitializeIPcheck = IPchanged
End Sub
I got that much, but what does changing the index to 0 at design time and then using a 1 as in index at run time do?
Also, something to keep in mind, this program is designed to run for long periods of time, so it will most likely see multiple occurrences when the IP address changes.
Do note:
By changing the index from (no index) to a index, i.e 0
VB will add An Index if there is no code.
i.e
Code:
Private Sub Winsock1_DataArrival( ByVal bytesTotal As Long)
End Sub
Private Sub Winsock1_Error(ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
End Sub
Code:
Private Sub Winsock1_DataArrival(Index As Integer, ByVal bytesTotal As Long)
End Sub
Private Sub Winsock1_Error(Index As Integer, ByVal Number As Integer, Description As String, ByVal Scode As Long, ByVal Source As String, ByVal HelpFile As String, ByVal HelpContext As Long, CancelDisplay As Boolean)
End Sub