Disable/enable local network connection in Delphi

Source: Internet
Author: User

Disable/enable local network connections. Most of the Methods searched on the Internet are implemented by using setupapi to disable/enable related devices. One of them is implemented through shellapi, and these methods are not good, the Code is as follows:

// Unit to <br/> get the local network connection list <br/> // and enable/disable correspond network connection <br/> // author: jhzhang@iflytek.com <br/> // history: 2009/1/16 create <br/> // <br/> unit unetworkconnection; <br/> interface <br/> uses Windows, messages, sysutils, variants, classes, graphics, ActiveX, <br/> comobj, contnrs, tntwidestrutils; <br/> const clsid_connectionmanager: tguid = <br/> '{BA126AD1-2166-11D1-B1D0-00805FC1270E}'; iid_inetconnectionmanager: <br/> tguid = '{C08956A2-1CD3-11D1-B1C5-00805FC1270E}'; <br/> type accept = (ncme_default); netcon_status = (<br/> ncs_disconnected, ncs_connecting, ncs_connected, ncs_disconnecting, <br/> response, response, <br/> ncs_hardware_malfunction, ncs_media_disconnected, ncs_authenticating, <br/> response, response, <br/> ncs_invalid_address, response ); netcon_mediatype = (<br/> ncm_none, ncm_direct, ncm_isdn, ncm_lan, ncm_phone, ncm_tunnel, <br/> ncm_pppoe, ncm_bridge, callback, <br/> callback ); pnetcon_properties = ^ netcon_properties; <br/> netcon_properties = record guidid: tguid; pszwname: lpwstr; // array <br/> [0 .. 255] of wchar; pszwdevicename: lpwstr; Status: netcon_status; <br/> mediatype: netcon_mediatype; dwcharacter: DWORD; identifier: <br/> tguid; clsiduiobject: tguid; end; inetconnectionmanager = interface; <br/> inetconnection = interface; ienumnetconnection = interface; <br/> inetconnectionmanager = interface (iunknown) <br/> ['{C08956A2-1CD3-11D1-B1C5-00805FC1270E}'] function <br/> enumconnections (FLAG: netconmgr_enum_flags; out bp:< br/> ienumnetconnection): hresult; stdcall; end; ienumnetconnection = <br/> interface (iunknown) ['{C08956A0-1CD3-11D1-B1C5-00805FC1270E}'] function <br/> next (CELT: DWORD; out rgelt: inetconnection; pceltfetched: Pulong ): <br/> hresult; stdcall; function SKIP (CELT: ulong): hresult; stdcall; <br/> function reset (): hresult; stdcall; function clone (Out ppenum: <br/> ienumnetconnection): hresult; stdcall; end; inetconnection = <br/> interface (iunknown) ['{C08956A1-1CD3-11D1-B1C5-00805FC1270E}'] function <br/> connect (): hresult; stdcall; function disconnect (): hresult; stdcall; <br/> function Delete (): hresult; stdcall; function <br/> duplicate (pszwduplicatename: lpcwstr; out ppcon: inetconnection): <br/> hresult; stdcall; function getproperties (Out ppprops: <br/> pnetcon_properties): hresult; stdcall; function getuiobjectclassid (out <br/> pclsid: tguid): hresult; stdcall; function Rename (pszwnewname: <br/> lpcwstr): hresult; stdcall; end; // global functions procedure <br/> getconnectionlist (plist: tstrings ); function enableconnection (guid: <br/> string; benable: Boolean): integer; <br/> Implementation <br/> procedure getconnectionlist (plist: tstrings ); <br/> var pmanager: inetconnectionmanager; penum: ienumnetconnection; <br/> pconnection: inetconnection; celtfetched: DWORD; pproperties: <br/> pnetcon_properties; Status: string; <br/> begin cocreateinstance (clsid_connectionmanager, nil, clsctx_server, <br/> iid_inetconnectionmanager, pmanager); If <br/> succeeded (pmanager. enumconnections (ncme_default, penum) then begin <br/> while (penum. next (1, pconnection, @ celtfetched) = s_ OK) Do begin <br/> pconnection. getproperties (pproperties); Case pproperties. status of <br/> ncs_connected: Status: = 'connected'; ncs_disconnected: Status: = <br/> 'disconnected'; ncs_connecting: Status: = 'connected '; <br/> ncs_hardware_disabled: Status: = 'Disabled '; else status: = 'others'; <br/> end; plist. add (format ('% S | % s', [guidtostring (pproperties. guidid), <br/> pproperties. pszwname, pproperties. pszwdevicename, status]); end; <br/> end; <br/> // This method will enable/disable the network connection similar to <br/> the GUI code. <br/> // enableconnection (L "Local Area Connection", true); <br/> // <br/> function enableconnection (guid: string; benable: Boolean ): integer; <br/> var metadata: Procedure (pprops: pnetcon_properties) <br/> stdcall; // metadata: Procedure (myob: ienumnetconnectionj, javasflags, ienumnetconnection ** ppenum ); <br/> hres: hresult; <br/> pman: inetconnectionmanager; <br/> penum: ienumnetconnection; <br/> pcon: inetconnection; <br/> count: DWORD; <br/> done: integer; <br/> hmod: hmodule; <br/> pprops: pnetcon_properties; <br/> begin <br/> result: = 0; <br/> hmod: = loadlibrary ('netshell. dll '); <br/> If (hmod = 0) Then <br/> exit; <br/> @ lpncfreenetconproperties: = getprocaddress (hmod, 'ncfreenetconproperties '); <br/> If (@ lpncfreenetconproperties = nil) Then <br/> begin <br/> freelibrary (hmod); <br/> exit; <br/> end; <br/> pman: = nil; <br/> hres: = cocreateinstance (clsid_connectionmanager, nil, clsctx_all, iid_inetconnectionmanager, pman); <br/> If (succeeded (hres )) then <br/> begin <br/> hres: = pman. enumconnections (ncme_default, penum); <br/> If (succeeded (hres) Then <br/> begin <br/> done: = 0; <br/> guid: = lowercase (guid); <br/> while (penum. next (1, pcon, @ count) = s_ OK) and (done = 0) DO <br/> begin <br/> pcon. getproperties (pprops); <br/> If (succeeded (hres) Then <br/> begin <br/> If (lowercase (guidtostring (pprops. guidid) = guid) Then <br/> begin <br/> If (benable) then begin <br/> result: = pcon. connect (); <br/> end <br/> else <br/> begin <br/> result: = pcon. disconnect (); <br/> end; <br/> done: = 1; <br/> end; // end if (wcsstr (pprops->... <br/> lpncfreenetconproperties (pprops); <br/> // pcon. _ release (); <br/> end; // end while <br/> // penum. _ release (); <br/> end; <br/> // pman. _ release (); <br/> end; <br/> freelibrary (hmod); <br/> end.

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.