Solved

how enable a disable network connection

Posted on 2004-09-27
5
376 Views
Last Modified: 2010-04-04
hello experts,

I need to know which is the registry key that manage enable and disable network connection.

thanks.
0
Comment
Question by:MauricioGaviria
5 Comments
 
LVL 2

Expert Comment

by:insomniac92
ID: 12166170
not sure about a registry key, but this is taken straight from torry....untested...

function ConnectDrive(_drvLetter: string; _netPath: string; _showError: Boolean;
  _reconnect: Boolean): DWORD;
var
  nRes: TNetResource;
  errCode: DWORD;
  dwFlags: DWORD;
begin
  { Fill NetRessource with #0 to provide uninitialized values }
  { NetRessource mit #0 füllen => Keine unitialisierte Werte }
  FillChar(NRes, SizeOf(NRes), #0);
  nRes.dwType := RESOURCETYPE_DISK;
  { Set Driveletter and Networkpath }
  { Laufwerkbuchstabe und Netzwerkpfad setzen }
  nRes.lpLocalName  := PChar(_drvLetter);
  nRes.lpRemoteName := PChar(_netPath); { Example: \\Test\C }
  { Check if it should be saved for use after restart and set flags }
  { Überprüfung, ob gespeichert werden soll }
  if _reconnect then
    dwFlags := CONNECT_UPDATE_PROFILE and CONNECT_INTERACTIVE
  else
    dwFlags := CONNECT_INTERACTIVE;

  errCode := WNetAddConnection3(Form1.Handle, nRes, nil, nil, dwFlags);
  { Show Errormessage, if flag is set }
  { Fehlernachricht aneigen }
  if (errCode <> NO_ERROR) and (_showError) then
  begin
    Application.MessageBox(PChar('An error occured while connecting:' + #13#10 +
      SysErrorMessage(GetLastError)),
      'Error while connecting!',
      MB_OK);
  end;
  Result := errCode; { NO_ERROR }
end;

function DisconnectNetDrive(_locDrive: string; _showError: Boolean; _force: Boolean;
  _save: Boolean): DWORD;
var
  dwFlags: DWORD;
  errCode: DWORD;
begin
  { Set dwFlags, if necessary }
  { Setze dwFlags auf gewünschten Wert }
  if _save then
    dwFlags := CONNECT_UPDATE_PROFILE
  else
    dwFlags := 0;
  { Cancel the connection see also at http://www.swissdelphicenter.ch/en/showcode.php?id=391 }
  { Siehe auch oben genannten Link (Netzlaufwerke anzeigen) }
  errCode := WNetCancelConnection2(PChar(_locDrive), dwFlags, _force);
  { Show Errormessage, if flag is set }
  { Fehlernachricht anzeigen }
  if (errCode <> NO_ERROR) and (_showError) then  
  begin
    Application.MessageBox(PChar('An error occured while disconnecting:' + #13#10 +
      SysErrorMessage(GetLastError)),
      'Error while disconnecting',
      MB_OK);
  end;
  Result := errCode; { NO_ERROR }
end;

//Example:

procedure TForm1.Button1Click(Sender: TObject);
begin
  ConnectDrive('h:', '\\Servername\C', True, True);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  DisconnectNetDrive('h:', True, True, True);
end;

0
 
LVL 9

Expert Comment

by:ginsonic
ID: 12166510
Read this:
http://www.delphi3000.com/articles/article_3176.asp

or use this frreware component:
Network Connection Mangager
http://www.programmersheaven.com/zone2/cat489/2758.htm
0
 
LVL 6

Expert Comment

by:Amir Azhdari
ID: 12170159
also you can download this delphi example with source( you can disable/enable all devices )
http://www.yeahware.com/download/enable.zip
0
 

Author Comment

by:MauricioGaviria
ID: 12174847
Sorry so much, really I want know if exists any api function to enable/disable dhcp on network interface and do all operations to renew the IP/mask that
receive from dhcp server, like ipconfig /renew.
0
 
LVL 4

Accepted Solution

by:
BedouinDN earned 500 total points
ID: 12175188
The IP Helper API is what you are looking for.
You will need to import it from the type library IPHLPAPI.lib

Or you can download the Delphi API translation by Marcel van Brakel which is quite good - located at: http://members.chello.nl/m.vanbrakel2
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now