Page 1 of 1
Posted: Thu Jan 02, 2003 11:21 pm
by BackupUser
Restored from previous forum. Originally posted by TerryHough.
I have been struggling to find a way to force a modem intenet connection to be disconnected without success.
I ran across the following code using the API, but don't really understand enough to convert it to PureBasic.
Can someone help me out?
Code: Select all
Const RAS_MAXENTRYNAME = 256
Const RAS_MAXDEVICETYPE = 16
Const RAS_MAXDEVICENAME = 128
Const RAS_RASCONNSIZE = 412
Private Type RasConn
dwSize As Long
hRasConn As Long
szEntryName(RAS_MAXENTRYNAME) As Byte
szDeviceType(RAS_MAXDEVICETYPE) As Byte
szDeviceName(RAS_MAXDEVICENAME) As Byte
End Type
Private Declare Function RasEnumConnections Lib "rasapi32.dll" Alias "RasEnumConnectionsA" (lpRasConn As Any, lpcb As Long, lpcConnections As Long) As Long
Private Declare Function RasHangUp Lib "rasapi32.dll" Alias "RasHangUpA" (ByVal hRasConn As Long) As Long
Private Sub Form_Load()
'KPD-Team 1998
'URL: [url]http://www.allapi.net/[/url]
'E-Mail: [url]mailto:KPDTeam@Allapi.net[/url]
'This program will close your Internet-connection, so to test this, you will have to open an Internet-connection.
Dim i As Long, lpRasConn(255) As RasConn, lpcb As Long
Dim lpcConnections As Long, hRasConn As Long
'Set the structure's size
lpRasConn(0).dwSize = RAS_RASCONNSIZE
lpcb = RAS_MAXENTRYNAME * lpRasConn(0).dwSize
lpcConnections = 0
'Enumerate all the available connections
returncode = RasEnumConnections(lpRasConn(0), lpcb, lpcConnections)
If returncode = 0 Then
For i = 0 To lpcConnections - 1
hRasConn = lpRasConn(i).hRasConn
'Hang up
returncode = RasHangUp(ByVal hRasConn)
Next i
End If
End Sub
All pointers appreciated. I'm not getting it to work.
TIA
Terry
Posted: Fri Jan 03, 2003 3:50 pm
by BackupUser
Restored from previous forum. Originally posted by MrVainSCL.
Mhhhh..
i only know from good old BBS times, that ATH0 string will disconnect...
PIII450, 256MB Ram, 80GB HD + 6,4 GB, RivaTNT, DirectX8.1, SB AWE64, Win2000 + all Updates...
greetz
MrVainSCL! aka Thorsten
Posted: Fri Jan 03, 2003 6:20 pm
by BackupUser
Restored from previous forum. Originally posted by TerryHough.
Originally posted by MrVainSCL
Mhhhh..
i only know from good old BBS times, that ATH0 string will disconnect...
Yes, if all you want to do is hangup the line, effectively disconnecting the modem.
I want to go further, and disconnect the internet connection. Like right-clicking the connection icon in the tray, and then clicking on disconnect, but doing it automatically.
Here is why:
I wrote an FTP program that creates an internet connection using the default Internet Options. In many cases that uses a dial-up through an ISP. That all works well and good (if the Internet Options are properly configured), but when I am ready to terminate the program the internet connection created by the dial-up default is left running. So, I want to terminate it. Not having any luck so far.
To make a long story short...
If my program causes the internet connection to be made, then I want the connection to be disconnected when my program terminates.
Thanks,
Terry
Posted: Fri Jan 03, 2003 8:19 pm
by BackupUser
Restored from previous forum. Originally posted by El_Choni.
This seems to be a translation of that code (also checked the API help), but it doesn't work (I love posting useless code )
Code: Select all
Procedure Error(fatal.b)
FormatMessage_(#FORMAT_MESSAGE_FROM_SYSTEM, 0, GetLastError_(), 0, AllocateMemory(0, 256, 0), 256, 0)
MessageRequester("Error", PeekS(MemoryID()), 0)
FreeMemory(0)
If fatal
End
EndIf
EndProcedure
#RAS_MaxEntryName = 256
#RAS_MaxDeviceType = 16
#RAS_MaxDeviceName = 128
Structure RasConn
dwSize.l
hRasConn.l
szEntryName.b[#RAS_MaxEntryName+1]
szDeviceType.b[#RAS_MaxDeviceType+1]
szDeviceName.b[#RAS_MaxDeviceName+1]
EndStructure
lpcConnections.l
If OpenLibrary(0, "RASAPI32.DLL")
lpcb = #RAS_MaxEntryName*SizeOf(RasConn)
MemoryID = AllocateMemory(1, lpcb, 0)
returncode = CallFunction(0, "RasEnumConnections", MemoryID, lpcb, @lpcConnections)
If returncode=0
For i = 0 To (lpcConnections-1)
*lpRasConn.RasConn = MemoryID+(SizeOf(RasConn)*i)
returncode = CallFunction(0, "RasHangUp", *lpRasConn\hRasConn)
Next i
Else
Error(0)
EndIf
FreeMemory(1)
CloseLibrary(0)
Else
Error(1)
EndIf
Bye, hope you can fix it.
El_Choni
Posted: Sat Jan 04, 2003 8:34 pm
by BackupUser
Restored from previous forum. Originally posted by TerryHough.
Originally posted by El_Choni
...(SNIP) , but it doesn't work (SNIP)
Neither does mine!

and I am not making much progress.
Maybe someone else can give us a pointer.
Thanks,
Terry
Posted: Wed Jan 08, 2003 1:44 am
by BackupUser
Restored from previous forum. Originally posted by TerryHough.
Not many have replied, but many have viewed my question. Now that
I have found the solution I will share it.
Someone once said "If you search long enough, you will find it."
They obviously were referring to the WinAPI, or sort of.
Actually I didn't find the solution in the WinAPI but in the
WININET.dll (installed with Internet Explorer 4.0 and higher).
I could have searched the WinAPI forever without success.
Simple, once you find where to look.
Code: Select all
If InternetAutodialHangup_(0)
; the connection was autodialed, so hang it up
Else
MessageRequester("Failure warning!","Could not disconnect the internet connection automatically...",0)
Endif
That was all I needed in my application.
other information you may want to know:
Code: Select all
#INTERNET_AUTODIAL_FORCE_ONLINE = 1
#INTERNET_AUTODIAL_FORCE_UNATTENDED = 2
If InternetAutodial_(#INTERNET_AUTODIAL_FORCE_ONLINE, 0)
MessageRequester("Testing","Connected",0)
Endif
If InternetAutodial_(#INTERNET_AUTODIAL_FORCE_UNATTENDED, 0)
MessageRequester("Testing","Connected",0)
Endif
Hope this helps someone else out
Terry
Posted: Wed Jan 08, 2003 1:44 am
by BackupUser
Restored from previous forum. Originally posted by TerryHough.
Not many have replied, but many have viewed my question. Now that
I have found the solution I will share it.
Someone once said "If you search long enough, you will find it."
They obviously were referring to the WinAPI, or sort of.
Actually I didn't find the solution in the WinAPI but in the
WININET.dll (installed with Internet Explorer 4.0 and higher).
I could have searched the WinAPI forever without success.
Simple, once you find where to look.
Code: Select all
If InternetAutodialHangup_(0)
; the connection was autodialed, so hang it up
Else
MessageRequester("Failure warning!","Could not disconnect the internet connection automatically...",0)
Endif
That was all I needed in my application.
other information you may want to know:
Code: Select all
#INTERNET_AUTODIAL_FORCE_ONLINE = 1
#INTERNET_AUTODIAL_FORCE_UNATTENDED = 2
If InternetAutodial_(#INTERNET_AUTODIAL_FORCE_ONLINE, 0)
MessageRequester("Testing","Connected",0)
Endif
If InternetAutodial_(#INTERNET_AUTODIAL_FORCE_UNATTENDED, 0)
MessageRequester("Testing","Connected",0)
Endif
Hope this helps someone else out
Terry
Posted: Wed Jan 08, 2003 4:14 am
by BackupUser
Restored from previous forum. Originally posted by PB.
Hi Terry,
You won't believe this, but I just came up with my own solution and
posted my tip moments before I saw yours! The different API calls
prove that I just didn't "rip off" your code.
I note, however, that your code doesn't work for me (W2K) for some
reason. Strange. Can anybody else confirm who's code works best?
Not for competition reasons, but for simple compatibility reasons.
My tip:
viewtopic.php?t=4708
Posted: Wed Jan 08, 2003 1:51 pm
by BackupUser
Restored from previous forum. Originally posted by TerryHough.
Originally posted by PB
You won't believe this, but I just came up with my own solution and posted my tip moments before I saw yours! The different API calls
prove that I just didn't "rip off" your code.
I note, however, that your code doesn't work for me (W2K) for some
reason. Strange. Can anybody else confirm who's code works best?
Not for competition reasons, but for simple compatibility reasons.
Thanks PB. At least I know someone besides me was working on the problem.
Looks like a couple of ways exist to accomplish the disconnect.
I have only tested my code on Win98SE (the only OS I had to worry about). In my case, the dial-up connect is automatically initiated by my code's attempt to do an InternetConnect (based on the interet options being properly set with a default as you know) if it finds it is not already connected. So, I never issue an InternetDial command.
Thanks for your tip and participation on this board.
Terry
Posted: Wed Jan 08, 2003 6:14 pm
by BackupUser
Restored from previous forum. Originally posted by Kale.
right i've tested both here are the results (machine used: WinXP Home, ADSL connection)
----------------------------------------------------------
Code: Select all
#INTERNET_AUTODIAL_FORCE_ONLINE = 1
If InternetAutodial_(#INTERNET_AUTODIAL_FORCE_ONLINE, 0)
MessageRequester("Testing","Connected",0)
Endif
This code prompted me the connection settings box and i had to manually press connect. (the 'connect automatically' check box was unchecked)
----------------------------------------------------------
Code: Select all
#INTERNET_AUTODIAL_FORCE_UNATTENDED = 2
If InternetAutodial_(#INTERNET_AUTODIAL_FORCE_UNATTENDED, 0)
MessageRequester("Testing","Connected",0)
EndIf
This code connected me straight to the internet without me pressing buttons. (the connection setting dialog box did show itself during the connection duration)
----------------------------------------------------------
Code: Select all
If InternetAutodialHangup_(0)
; the connection was autodialed, so hang it up
Else
MessageRequester("Failure warning!","Could not disconnect the internet connection automatically...",0)
EndIf
This disconnected me flawlessly without any interaction (pressing buttons)
----------------------------------------------------------
PB's code here
viewtopic.php?t=4708 br /
connected and disconnected me flawlessly without any interaction (pressing buttons)
----------------------------------------------------------
Great stuff guys
--Kale
New to PureBasic and falling in Love! 