A cada dia que passa é mais freqüente o uso de dados online oriundos de servidores na Web. Estes são automaticamente transformados em informações ao atualizarem os nossos Dashboards e Scorecards instantâneamente.
Para garantirmos que tais dados receberão as atualizações corretas no tempo apropriado é importante checarmos a conexão das nossas máquinas constantemente. A API InternetGetConnectedState declarada no wininet.dll retorna o estado da conexão do sistema local. A API é simples de usar, e retorna TRUE se houver uma conexão com a Internet.
As funções abaixo são muito úteis para garantir a validade de processos com este perfil:
IsNetConnectViaLANIsNetConnectViaModemIsNetConnectViaProxyIsNetConnectOnlineIsNetRASInstalledGetNetConnectString
Divirta-se com o código abaixo, colando-o inteiramente num novo módulo da sua aplicação.
Option Explicit''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Copyright ©1996-2011 VBnet/ Randy Birch, All Rights Reserved.' Some pages may also contain other copyrights by the author.''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' Distribution: You can freely use this code in your own' applications, but you may not reproduce' or publish this code on any web site,' online service, or distribute as source' on any media without express permission.''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''Private Declare Function InternetGetConnectedState Lib "wininet" _(ByRef dwflags As Long, _ByVal dwReserved As Long) As Long'Local system uses a modem to connect to the Internet.Private Const INTERNET_CONNECTION_MODEM As Long = &H1'Local system uses a LAN to connect to the Internet.Private Const INTERNET_CONNECTION_LAN As Long = &H2'Local system uses a proxy server to connect to the Internet.Private Const INTERNET_CONNECTION_PROXY As Long = &H4'No longer used.Private Const INTERNET_CONNECTION_MODEM_BUSY As Long = &H8Private Const INTERNET_RAS_INSTALLED As Long = &H10Private Const INTERNET_CONNECTION_OFFLINE As Long = &H20Private Const INTERNET_CONNECTION_CONFIGURED As Long = &H40Private Sub Command1_Click()Let Text1.Text = IsNetConnectViaLAN()Let Text2.Text = IsNetConnectViaModem()Let Text3.Text = IsNetConnectViaProxy()Let Text4.Text = IsNetConnectOnline()Let Text5.Text = IsNetRASInstalled()Let Text6.Text = GetNetConnectString()End SubFunction IsNetConnectViaLAN() As BooleanDim dwflags As Long'pass an empty variable into which the API will'return the flags associated with the connectionCall InternetGetConnectedState(dwflags, 0&)'return True if the flags indicate a LAN connectionIsNetConnectViaLAN = dwflags And INTERNET_CONNECTION_LANEnd FunctionFunction IsNetConnectViaModem() As BooleanDim dwflags As Long'pass an empty variable into which the API will'return the flags associated with the connectionCall InternetGetConnectedState(dwflags, 0&)'return True if the flags indicate a modem connectionLet IsNetConnectViaModem = dwflags And INTERNET_CONNECTION_MODEMEnd FunctionFunction IsNetConnectViaProxy() As BooleanDim dwflags As Long'pass an empty variable into which the API will'return the flags associated with the connectionCall InternetGetConnectedState(dwflags, 0&)'return True if the flags indicate a proxy connectionLet IsNetConnectViaProxy = dwflags And INTERNET_CONNECTION_PROXYEnd FunctionFunction IsNetConnectOnline() As Boolean'no flags needed here - the API returns True'if there is a connection of any typeLet IsNetConnectOnline = InternetGetConnectedState(0&, 0&)End FunctionFunction IsNetRASInstalled() As BooleanDim dwflags As Long'pass an empty variable into which the API will'return the flags associated with the connectionCall InternetGetConnectedState(dwflags, 0&)'return True if the flags include RAS installedLet IsNetRASInstalled = dwflags And INTERNET_RAS_INSTALLEDEnd FunctionFunction GetNetConnectString() As StringDim dwflags As LongDim msg As String'build a string for displayIf InternetGetConnectedState(dwflags, 0&) ThenIf dwflags And INTERNET_CONNECTION_CONFIGURED ThenLet msg = msg & "Você tem uma conexão de rede configurada." & vbCrLfEnd IfIf dwflags And INTERNET_CONNECTION_LAN ThenLet msg = msg & "O sistemas local conecta-se a Internet via LAN"End IfIf dwflags And INTERNET_CONNECTION_PROXY ThenLet msg = msg & ", e usuários no Servidor de Proxy. "ElseLet msg = msg & "."End IfIf dwflags And INTERNET_CONNECTION_MODEM ThenLet msg = msg & "O sistema local usa um modem para conectar-se a Internet. "End IfIf dwflags And INTERNET_CONNECTION_OFFLINE ThenLet msg = msg & "A conexão está offline. "End IfIf dwflags And INTERNET_CONNECTION_MODEM_BUSY ThenLet msg = msg & "O sistema de Modem local está ocupado com uma conexão non-Internet. "End IfIf dwflags And INTERNET_RAS_INSTALLED ThenLet msg = msg & "Serviço de Acesso Remoto instalado neste sistema."End IfElseLet msg = "Não conectado a Internet agora."End IfLet GetNetConnectString = msgEnd Function
Tags: VBA, Tips, Network, Connection, Type, code, network, DLL, wininet.dll