Mega Code Archive

 
Categories / Delphi / LAN Web TCP
 

Checking and Disconnecting DUN

Title: Checking and Disconnecting DUN Question: How to check and disconnect from DUN connection? Answer: This answer is a direct translation from a snipplet using VB I found from the Internet. The constant RAS_RASCONNSIZE is still a mistery for me, I can't use sizeof(TRasConn) for setting dwSize of TRasConn record. const RAS_MAXENTRYNAME = 256; RAS_MAXDEVICETYPE = 16; RAS_MAXDEVICENAME = 128; RAS_RASCONNSIZE = 412; type TRasEntryName= record dwSize: longint; szEntryName: array[1..RAS_MAXENTRYNAME] of char; end; TRasConn= record dwSize: longint; hRasConn: longint; szEntryName: array [1..RAS_MAXENTRYNAME] of char; szDeviceType,szDeviceName: array [1..RAS_MAXDEVICETYPE] of char; end; TRasEntryNamePtr=^TRasEntryName; TRasConnPtr=^TRasConn; longintPtr=^longint; function RasEnumConnectionsA(lpRasConn:TRasConnPtr;lpcb:longintPtr; lpcConnections:longintPtr):longint;stdcall;external 'RASAPI32.DLL'; function RasHangUpA(hRasConn:longint):longint;stdcall;external 'RASAPI32.DLL'; function ConnectedService: string; var aReg : TRegistry; begin aReg := TRegistry.Create; aReg.RootKey := HKEY_CURRENT_USER; result:=''; if aReg.OpenKey('RemoteAccess', False) then result:= aReg.ReadString('Default'); aReg.Free; end; procedure RasHangup; var i,lpcb,lpcConnections,hRasConn,returnCode:longint; lpRasConn: array [0..255] of TRasConn; serviceName: string; begin lpRasConn[0].dwSize:= RAS_RASCONNSIZE; lpcb:=RAS_MAXENTRYNAME * lpRasConn[0].dwSize; lpcConnections:= 0; serviceName:=connectedService; returnCode:= RasEnumConnectionsA(@lpRasConn[0],@lpcb,@lpcConnections); if returnCode = ERROR_SUCCESS then begin for i:= 0 to lpcConnections - 1 do begin if serviceName=PChar(@lpRasConn[i].szEntryName) then begin hRasConn:=lpRasConn[i].hRasConn; RasHangUpA(hRasConn); end; end; end; end; function IsConnected: boolean; var aReg : TRegistry; aLong: longint; begin aLong:=0; aReg := TRegistry.Create; aReg.RootKey := HKEY_LOCAL_MACHINE; if aReg.OpenKey('System\CurrentControlSet\Services\RemoteAccess', False) then aReg.ReadBinaryData('Remote Connection',aLong,sizeof(aLong)); result:=aLong0; aReg.Free; end;