Testade göljande kod i ett program, för att se ifall jag har en aktiv uppkoppling, den genererar Not Connected även fast jag är ansluten, har telia adsl.
Nedan följer koden:
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, WinInet, StdCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
function IsConnectedToInternet: Boolean;
var
dwConnectionTypes: DWORD;
begin
dwConnectionTypes :=
INTERNET_CONNECTION_MODEM +
INTERNET_CONNECTION_LAN +
INTERNET_CONNECTION_PROXY;
Result := InternetGetConnectedState(@dwConnectionTypes, 0);
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if IsConnectedToInternet then
ShowMessage('Connected.')
else
ShowMessage('Not Connected.')
end;
end.
function IsConnectedToInternet: Boolean;
var
dwConnectionTypes: DWORD;
begin
InternetGetConnectedState(@dwConnectionTypes, 0);
if(dwConnectionTypes AND INTERNET_CONNECTION_MODEM)
Result:=true;
else if(dwConnectionTypes AND INTERNET_CONNECTION_LAN)
Result:=true;
else if(dwConnectionTypes AND INTERNET_CONNECTION_PROXY)
Result:=true;
else
Result:=false;
end;
Jag har varken testat VB koden eller jobbat i Delphi men detta är min tolkning, hoppas det är rätt :)
Om du bara vill veta om du är uppkopplad eller inte behöver du bara kolla returvärdet på InternetConnectedState. Så här:
function IsConnectedToInternet: Boolean;
var
dwConnectionTypes: dword;
begin
Result := InternetGetConnectedState(@dwConnectionTypes, 0)
end;
Vill du däremot veta på vilket sätt du är uppkopplad måste du titta på flaggorna, typ så här:
function TypAvAnslutning: string;
var
dwConnectionTypes: dword;
begin
if InternetGetConnectedState(@dwConnectionTypes, 0) then begin
if (dwConnectionTypes and INTERNET_CONNECTION_MODEM) = INTERNET_CONNECTION_MODEM then
Result := 'Modem'
else if (dwConnectionTypes and INTERNET_CONNECTION_LAN) = INTERNET_CONNECTION_LAN then
Result := 'LAN'
else if (dwConnectionTypes and INTERNET_CONNECTION_PROXY) = INTERNET_CONNECTION_PROXY then
Result := 'Proxy'
else
Result := 'Annan typ av anslutning'
end else
Result := 'Ingen anslutning'
end;
Funkar för mig i alla fall...
138 ms totalt · 3 externa anrop · v20260731065814-full.25f56b17