1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
| procedure TFormMain.SwitchOnOff;
var
sConnectionName : string;
sEnableVerb : string;
sDisableVerb : string;
shellApp, oControlPanel, oNetConnections, oLanConnection: OleVariant;
i: integer;
bEnabled: boolean;
oEnableVerb, oDisableVerb: Variant;
s: string;
begin
sConnectionName := 'Connexion au réseau local';
sEnableVerb := 'Activer';
sDisableVerb := 'Désactiver';
shellApp := CreateOleObject('shell.application');
oControlPanel := shellApp.Namespace(ssfCONTROLS);
oNetConnections := null;
for i := 0 to oControlPanel.Items.Count - 1 do
begin
// C'est a ce niveau que ca plante ***
if oControlPanel.Items[i].Name = 'Connexions réseau' then
begin
oNetConnections := oControlPanel.Items[i].getfolder;
break;
end;
end;
if oNetConnections = null then
begin
MessageDlg('''Connexions réseau'' non trouvée', mtWarning, [mbOK], 0);
Exit;
end;
oLanConnection := null;
for i := 0 to oNetConnections.items.count - 1do
begin
if LowerCase(oNetConnections.items[i].Name) = LowerCase(sConnectionName) then
begin
oLanConnection := oNetConnections.items[i];
break;
end;
end;
if oLanConnection = null then
begin
MessageDlg(sConnectionName + ' non trouvée', mtWarning, [mbOK], 0);
Exit;
end;
bEnabled := true;
oEnableVerb := null;
oDisableVerb := null;
s := 'Verbs: ' + #13#10;
for i := 0 to oLanConnection.verbs.Count - 1 do
begin
s := s + #13#10 + oLanConnection.verbs[i].name;
if oLanConnection.verbs[i].name = sEnableVerb then
begin
oEnableVerb := oLanConnection.verbs[i];
bEnabled := false;
end;
if oLanConnection.verbs[i].name = sDisableVerb then
oDisableVerb := oLanConnection.verbs[i];
end;
if bEnabled then
begin
oLanConnection.invokeverb(sDisableVerb);
oDisableVerb.DoIt;
end
else
begin
oLanConnection.invokeverb(sEnableVerb);
oEnableVerb.DoIt;
end;
end; |
Partager