delphi 如何断开网络和连接网络

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了delphi 如何断开网络和连接网络相关的知识,希望对你有一定的参考价值。

delphi 如何断开网络和连接网络

就想防火墙一样 点断开 就断开了 再点开启有开启了

求代码

unit uNetworkConnection;
interface
uses Windows, Messages, SysUtils, Variants, Classes, Graphics, ActiveX,
ComObj, Contnrs, TntWideStrUtils;
const CLSID_ConnectionManager: TGUID =
'BA126AD1-2166-11D1-B1D0-00805FC1270E'; IID_INetConnectionManager:
TGUID = 'C08956A2-1CD3-11D1-B1C5-00805FC1270E';
type NETCONMGR_ENUM_FLAGS = (NCME_DEFAULT); NETCON_STATUS = (
NCS_DISCONNECTED, NCS_CONNECTING, NCS_CONNECTED, NCS_DISCONNECTING,
NCS_HARDWARE_NOT_PRESENT, NCS_HARDWARE_DISABLED,
NCS_HARDWARE_MALFUNCTION, NCS_MEDIA_DISCONNECTED, NCS_AUTHENTICATING,
NCS_AUTHENTICATION_SUCCEEDED, NCS_AUTHENTICATION_FAILED,
NCS_INVALID_ADDRESS, NCS_CREDENTIALS_REQUIRED ); NETCON_MEDIATYPE = (
NCM_NONE, NCM_DIRECT, NCM_ISDN, NCM_LAN, NCM_PHONE, NCM_TUNNEL,
NCM_PPPOE, NCM_BRIDGE, NCM_SHAREDACCESSHOST_LAN,
NCM_SHAREDACCESSHOST_RAS ); PNETCON_PROPERTIES = ^NETCON_PROPERTIES;
NETCON_PROPERTIES = record guidId: TGuid; pszwName: LPWSTR; //array
[0..255] of WCHAR; pszwDeviceName: LPWSTR; Status: NETCON_STATUS;
MediaType: NETCON_MEDIATYPE; dwCharacter: DWORD; clsidThisObject:
TGuid; clsidUiObject: TGuid; end; INetConnectionManager = interface;
INetConnection = interface; IEnumNetConnection = interface;
INetConnectionManager = interface(IUnknown)
['C08956A2-1CD3-11D1-B1C5-00805FC1270E'] function
EnumConnections(flag: NETCONMGR_ENUM_FLAGS; out ppv:
IEnumNetConnection): HRESULT; stdcall; end; IEnumNetConnection =
interface(IUnknown) ['C08956A0-1CD3-11D1-B1C5-00805FC1270E'] function
Next(celt: DWORD; out rgelt: INetConnection; pceltFetched: PULONG):
HRESULT; stdcall; function Skip(celt: ULONG): HRESULT; stdcall;
function Reset(): HRESULT; stdcall; function Clone(out ppenum:
IEnumNetConnection): HRESULT; stdcall; end; INetConnection =
interface(IUnknown) ['C08956A1-1CD3-11D1-B1C5-00805FC1270E'] function
Connect(): HRESULT; stdcall; function Disconnect(): HRESULT; stdcall;
function Delete(): HRESULT; stdcall; function
Duplicate(pszwDuplicateName: LPCWSTR; out ppCon: INetConnection):
HRESULT; stdcall; function GetProperties(out ppProps:
PNETCON_PROPERTIES): HRESULT; stdcall; function GetUiObjectClassId(out
pclsid: TGUID): HRESULT; stdcall; function Rename(pszwNewName:
LPCWSTR): HRESULT; stdcall; end; //Global functions procedure
GetConnectionList(pList: TStrings); function EnableConnection(Guid:
String; bEnable: Boolean): Integer;
implementation
procedure GetConnectionList(pList: TStrings);
var pManager : INetConnectionManager; pEnum : IEnumNetConnection;
pConnection : INetConnection; celtFetched : DWORD; pProperties :
PNETCON_PROPERTIES; Status : String;
begin CoCreateInstance(CLSID_ConnectionManager, nil, CLSCTX_SERVER,
IID_INetConnectionManager, pManager); if
SUCCEEDED(pManager.EnumConnections(NCME_DEFAULT, pEnum)) then begin
while (pEnum.Next(1, pConnection, @celtFetched) = S_OK) do begin
pConnection.GetProperties(pProperties); case pProperties.Status of
NCS_CONNECTED: Status := 'CONNECTED'; NCS_DISCONNECTED: Status :=
'DISCONNECTED'; NCS_CONNECTING: Status := 'CONNECTING';
NCS_HARDWARE_DISABLED: Status := 'DISABLED'; else Status := 'Others';
end; pList.Add(Format('%s|%s|%s|%s', [GUIDToString(pProperties.guidId),
pProperties.pszwName, pProperties.pszwDeviceName, Status])); end; end;
end;
// This method will enable/disable the network connection similar to
the GUI code.
// EnableConnection(L"Local Area Connection", true);
//
function EnableConnection(Guid: String; bEnable: Boolean): Integer;
var LPNcFreeNetconProperties : procedure(pProps : PNETCON_PROPERTIES)
stdcall; //LPIEnumNetConnection_EnumConnections : procedure(myob : IEnumNetConnectionj, NETCONMGR_ENUM_FLAGS flags, IEnumNetConnection * * ppEnum);
hres : HRESULT;
pMan : INetConnectionManager;
pEnum : IEnumNetConnection;
pCon : INetConnection;
count : DWORD;
done : Integer;
hmod : HMODULE;
pProps : PNETCON_PROPERTIES;
begin
result := 0;
hmod := LoadLibrary('netshell.dll');
if (hmod = 0) then
exit;
@LPNcFreeNetconProperties := GetProcAddress(hmod, 'NcFreeNetconProperties');
if (@LPNcFreeNetconProperties = nil) then
begin
FreeLibrary(hmod);
exit;
end;
pMan := nil;
hres := CoCreateInstance(CLSID_ConnectionManager, nil, CLSCTX_ALL, IID_INetConnectionManager, pMan);
if (SUCCEEDED(hres)) then
begin
hres := pMan.EnumConnections(NCME_DEFAULT, pEnum);
if (SUCCEEDED(hres)) then
begin
done := 0;
Guid := LowerCase(Guid);
while ((pEnum.Next(1, pCon, @count) = S_OK) and (done = 0)) do
begin
pCon.GetProperties(pProps);
if (SUCCEEDED(hres)) then
begin
if (LowerCase(GuidToString(pProps.guidId)) = Guid) then
begin
if (bEnable) then begin
result := pCon.Connect();
end
else
begin
result := pCon.Disconnect();
end;
done := 1;
end; //END if (wcsstr(pProps->...
LPNcFreeNetconProperties(pProps);
//pCon._Release();
end;
end; //END while
//pEnum._Release();
end;
//pMan._Release();
end;
FreeLibrary(hmod);
end;
end.

这段代码转自csnd博客http://blog.csdn.net/jh_zzz/archive/2009/01/17/3809047.aspx
参考技术A 下载一个MS的程序 DevCon 放到程序目录下,然后用delphi执行 cmd -c devcon disable *DEV_8136* 就可以关闭 型号为8136的网卡 参考技术B 可不可以用delphi通过启用与禁用网络连接来实现。 参考技术C 宽带连接吗?局域网连接?不行就禁用网卡再打开

如何关闭svn连接

参考技术A 1.重启电脑就ok了

1.在电脑连接网络地方右键单机,选择打开网络和共享中心。

2.在网络和共享中心页面选择更改适配器设置。

3.在网络连接页面,有显示我们连接过的网络设置。

4.选择要删除的vpn网络,右键单机,选择删除按钮即可。

ok

以上是关于delphi 如何断开网络和连接网络的主要内容,如果未能解决你的问题,请参考以下文章

如何在 python 中连接/断开/配置无线网络?

如何用C语言检测网络是不是连接上、已连接、已断开状态

如何关闭svn连接

如何在 Firebase 作业调度程序中检测网络断开连接?

无线网络连接已断开?

宽带连接已断开连接怎么回事?