Internetverbindung steuern

Vektor

Erfahrenes Mitglied
Hallo zusammen,

ich habe wieder ein Problem :). Und zwar möchte ich aus meinem Programm heraus ermitteln ob eine Verbindung mit dem Internet besteht, und falls nicht, den Standarddialog dazu aufrufen (Für die Standart-DFÜ-Verbindung). Diese soll dann eventuell auch wieder getrennt werden.

Ich hab mir jetzt schon mal folgende API's rausgesucht und deklariert:
Code:
Private Declare Function InternetDial Lib "wininet.dll" ( _
      ByVal hwndParent As Long, _
      ByVal lpszConiID As String, _
      ByVal dwFlags As Long, _
      ByRef hCon As Long, _
      ByVal dwReserved As Long) As Long

    Private Declare Function InternetHangUp Lib "wininet.dll" _
      (ByVal dwConnection As Long, ByVal dwReserved As Long) _
      As Long

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

    Private Declare Function RasEnumConnections Lib "RasApi32.DLL" Alias _
    "RasEnumConnectionsA" ( _
    ByVal lprasconn As Object, ByVal lpcb As Long, ByVal lpcConnections As Long) As Long

    Private Structure RASCONN
        Dim dwSize As Integer
        Dim hRasConn As Integer
        <VBFixedArray(256)> Dim szEntryName() As Byte
        <VBFixedArray(16)> Dim szDeviceType() As Byte
        <VBFixedArray(128)> Dim szDeviceName() As Byte

        Public Sub Initialize()
            ReDim szEntryName(256)
            ReDim szDeviceType(16)
            ReDim szDeviceName(128)
        End Sub
    End Structure
Das klappt aber nicht so wie ich will! (Oder ich kann einfach nicht damit umgehen :)) Leider finde ich auch keine Anleitungen oder Hilfen zu den API's. Nun hoffe ich, wieder einmal, dass ihr mir helfen könnt und mir sagt wie ich sowas zum laufen bekomme.

Vielen Dank...
 

JensG

Erfahrenes Mitglied
Hallo Vektor,

schau dir mal das an.

Code:
Public Class Form1
    Inherits System.Windows.Forms.Form

    Private Declare Function InternetGetConnectedStateEx Lib "wininet.dll" _
        Alias "InternetGetConnectedStateExA" (ByRef lpdwFlags As Integer, ByVal _
        lpszConnectionName As System.Text.StringBuilder, ByVal dwNameLen As Integer, _
        ByVal dwReserved As Integer) As Integer

    Public Declare Function InternetDial Lib "wininet.dll" (ByVal hwndParent As Integer, ByVal lpszConiID _
        As String, ByVal dwFlags As Integer, ByRef hCon _
        As Integer, ByVal dwReserved As Integer) As Integer




    Private Declare Function InternetAutodialHangup Lib "wininet.dll" _
    (ByVal dwReserved As Integer) As Integer




    Private Declare Function IsNetworkAlive Lib "Sensapi" (ByVal dwFlags As _
    Integer) As Integer

    Private Const NETWORK_ALIVE_LAN = &H1 'connection use net card
    Private Const NETWORK_ALIVE_WAN = &H2 'RAS connection
    Private Const NETWORK_ALIVE_AOL = &H4 'AOL connection (w9x only)



    Private Const INTERNET_CONNECTION_MODEM = &H1&
    Private Const INTERNET_CONNECTION_LAN = &H2&
    Private Const INTERNET_CONNECTION_PROXY = &H4&
    Private Const INTERNET_RAS_INSTALLED = &H10&
    Private Const INTERNET_CONNECTION_OFFLINE = &H20&
    Private Const INTERNET_CONNECTION_CONFIGURED = &H40&

    

    Const DIAL_UNATTENDED As Integer = &H8000&
    Const DIAL_FORCE_ONLINE As Integer = 1
    Const DIAL_FORCE_UNATTENDED As Integer = 2



    Public Structure RASENTRYNAME95
        Public dwSize As Integer
        Public szEntryName() As Byte
    End Structure

    Dim ConID As Integer, ConName As String




#Region " Vom Windows Form Designer generierter Code "

    Public Sub New()
        MyBase.New()

        ' Dieser Aufruf ist für den Windows Form-Designer erforderlich.
        InitializeComponent()

        ' Initialisierungen nach dem Aufruf InitializeComponent() hinzufügen

    End Sub

    ' Die Form überschreibt den Löschvorgang der Basisklasse, um Komponenten zu bereinigen.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    ' Für Windows Form-Designer erforderlich
    Private components As System.ComponentModel.IContainer

    'HINWEIS: Die folgende Prozedur ist für den Windows Form-Designer erforderlich
    'Sie kann mit dem Windows Form-Designer modifiziert werden.
    'Verwenden Sie nicht den Code-Editor zur Bearbeitung.
    Friend WithEvents Button1 As System.Windows.Forms.Button
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.Button1 = New System.Windows.Forms.Button
        Me.SuspendLayout()
        '
        'Button1
        '
        Me.Button1.Location = New System.Drawing.Point(32, 40)
        Me.Button1.Name = "Button1"
        Me.Button1.Size = New System.Drawing.Size(152, 40)
        Me.Button1.TabIndex = 0
        Me.Button1.Text = "Button1"
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(440, 273)
        Me.Controls.Add(Me.Button1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        Me.ResumeLayout(False)

    End Sub

#End Region

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim dwFlags As Integer
        Dim sNameBuf As System.Text.StringBuilder = New System.Text.StringBuilder(513)
        Dim sConnectionName As String
        Dim msg As String
        Dim lPos As Integer
        Dim iret As Integer

        iret = InternetGetConnectedStateEx(dwFlags, sNameBuf, sNameBuf.Capacity, 0&)
        If iret Then
            'Yes, you have active connection
            Dim CheckConnectionIGCS As Boolean
            CheckConnectionIGCS = True
            'Collect ExtraInfo:
            lPos = sNameBuf.Length


            If lPos > 0 Then sConnectionName = sNameBuf.ToString()
            Dim ConnectionInfo As Integer = dwFlags

            If (dwFlags And INTERNET_CONNECTION_LAN) Then
                MsgBox("Your connection use LAN")
            ElseIf (dwFlags And INTERNET_CONNECTION_MODEM) Then
                MsgBox("Your connection use Modem")
            End If
        Else
            If (dwFlags And INTERNET_CONNECTION_OFFLINE) Then
                MsgBox("You use a LAN But You Are Not Connected")
            Else
                MsgBox("Your use A Modem, But you are not connected")

              



                InternetDial(0, "DefaultDialUp", DIAL_FORCE_ONLINE, 0, 0)




            End If
        End If




    End Sub
End Class
 

Vektor

Erfahrenes Mitglied
Hallo,

vielen Dank für deine Antwort. Ich hab das jetzt mithilfe folgender API's gelöst.
Code:
Private Declare Function InternetGetConnectedState Lib "wininet.dll" (ByRef lpdwFlags As Int32, _
    ByVal dwReserved As Int32) As Boolean

    Private Declare Function InternetDial Lib "Wininet.dll" (ByVal hwndParent As IntPtr, _
    ByVal lpszConnectoid As String, ByVal dwFlags As Int32, ByRef lpdwConnection As Int32, _
    ByVal dwReserved As Int32) As Int32

    Private Declare Function InternetHangUp Lib "Wininet.dll" _
    (ByVal lpdwConnection As Int32, ByVal dwReserved As Int32) As Int32

    Private Declare Ansi Function RasEnumConnectionsA Lib _
      "RasApi32.DLL" (<[In](), Out()> ByVal lpRasConn As RASCONN(), ByRef lbcp As Integer, _
    ByRef lbcConnections As Integer) As Integer
Aber dein Code gefällt mir auf den ersten Blick wesentlich besser, da man damit sogar ermitteln kann über welches Gerät man verbunden ist. Ich werde mal diese Lösung noch ausprobieren. Danke nochmal.