Changeset 2 for trunk/MainWindow.pas
- Timestamp:
- Nov 27, 2008, 12:15:06 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/MainWindow.pas
r1 r2 7 7 Dialogs, StdCtrls, ComCtrls, ShellApi, DosCommand, Registry, Menus, 8 8 CoolTrayIcon, ExtCtrls, XPMan, magrascon, magrasedt, magrasent, SHFolder, 9 ShlObj ;9 ShlObj, magrasapi, Winsock; 10 10 11 11 const … … 14 14 HomePage = 'http://www.zdechov.net/'; 15 15 Email = 'robie@centrum.cz'; 16 Version = '1. 0';17 ReleaseDate = ' 13.8.2006';16 Version = '1.1'; 17 ReleaseDate = '24.8.2006'; 18 18 Creator = 'Chronosoft'; 19 19 RegistryRootKey = HKEY_LOCAL_MACHINE; … … 28 28 MainSharedFolderPath = 'C:\Net'; 29 29 MainSharedFolderName = 'Net'; 30 LocalNetAddr = '192.168.0.0'; 31 LocalNetMask = '255.255.0.0'; 30 32 31 33 type … … 57 59 Oprogramu1: TMenuItem; 58 60 CheckBox9: TCheckBox; 61 Zznamvyten1: TMenuItem; 62 Timer2: TTimer; 63 Label2: TLabel; 59 64 procedure FormShow(Sender: TObject); 60 65 procedure Button2Click(Sender: TObject); … … 68 73 procedure Button3Click(Sender: TObject); 69 74 procedure Oprogramu1Click(Sender: TObject); 75 procedure Zznamvyten1Click(Sender: TObject); 76 procedure MagRasCon1StateChanged(Sender: TObject); 77 procedure Timer2Timer(Sender: TObject); 70 78 private 79 Connecting: Boolean; 71 80 DialHandle: Cardinal; 72 81 function GetLocalNetworkRouting: Boolean; … … 84 93 function GetNetSharedFolder: Boolean; 85 94 procedure SetNetSharedFolder(const Value: Boolean); 95 function GetDefaultGateway: string; 86 96 public 97 DefaultGateway: string; 98 FirstStart: Boolean; 99 LastState: Word; 87 100 HostFilterIndex: Integer; 88 101 PingTimeout: Integer; … … 105 118 property NetSharedFolder: Boolean read GetNetSharedFolder 106 119 write SetNetSharedFolder; 120 procedure Init; 107 121 procedure Dial; 108 122 procedure AddConnection; … … 113 127 MainForm: TMainForm; 114 128 129 function GetIPFromHost(var HostName, IPaddr, WSAErr: string): Boolean; 115 130 function GetComputerNetName: string; 116 131 function CommonAppDataPath: string; … … 119 134 implementation 120 135 121 uses UNetworkTest, UAboutWindow, ULogExceptions, DateUtils, Math; 136 uses UNetworkTest, UAboutWindow, ULogExceptions, DateUtils, Math, 137 UDialingLog, StrUtils; 122 138 123 139 {$R *.dfm} … … 203 219 Command := TDosCommand.Create(nil); 204 220 with Command do try 205 CommandLine := 'route print 192.168.0.0 mask 255.255.0.0 192.168.0.1 metric 30'; 221 CommandLine := 'route print ' + LocalNetAddr + ' mask ' + LocalNetMask + 222 ' ' + DefaultGateway + ' metric 30'; 206 223 OutputLines := Lines2; 207 224 Execute2; 208 225 for I := 0 to Lines2.Count - 1 do 209 if Pos( '255.255.0.0', Lines2[I]) > 0 then Result := True;226 if Pos(LocalNetMask, Lines2[I]) > 0 then Result := True; 210 227 finally 211 228 Free; … … 220 237 Command := TDosCommand.Create(nil); 221 238 with Command do try 222 if Value then CommandLine := 'route -p add 192.168.0.0 mask 255.255.0.0 192.168.0.1metric 30'223 else CommandLine := 'route delete 192.168.0.0 mask 255.255.0.0 192.168.0.1metric 30';239 if Value then CommandLine := 'route -p add '+LocalNetAddr+' mask '+LocalNetMask+' '+DefaultGateway+' metric 30' 240 else CommandLine := 'route delete '+LocalNetAddr+' mask '+LocalNetMask+' '+DefaultGateway+' metric 30'; 224 241 Execute2; 225 242 finally … … 272 289 ComboBox1.Items.Assign(MagRasCon1.PhoneBookEntries); 273 290 ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(ConnectionName); 291 292 Label2.Caption := 'Brána: ' + DefaultGateway; 274 293 end; 275 294 … … 295 314 end; 296 315 316 function TMainForm.GetDefaultGateway: string; 317 var 318 Command: TDosCommand; 319 I: Integer; 320 Lines2: TStringList; 321 begin 322 Lines2 := TStringList.Create; 323 Result := ''; 324 Command := TDosCommand.Create(nil); 325 with Command do try 326 CommandLine := 'route print'; 327 OutputLines := Lines2; 328 Execute2; 329 for I := 0 to Lines2.Count - 1 do 330 if Pos('choz', Lines2[I]) > 0 then 331 Result := Trim(Copy(Lines2[I], 15, 256)); 332 finally 333 Free; 334 Lines2.Free; 335 end; 336 end; 337 297 338 function TMainForm.GetWorkgroupZdechov: Boolean; 298 339 var … … 366 407 procedure TMainForm.FormCreate(Sender: TObject); 367 408 begin 368 Application.ShowMainForm:= False;409 Connecting := False; 369 410 with CoolTrayIcon1 do begin 370 411 HideTaskbarIcon; … … 376 417 RootKey := RegistryRootKey; 377 418 OpenKey(RegistryPath, True); 419 if ValueExists('FirstStart') then FirstStart := ReadBool('FirstStart') 420 else FirstStart := False; 378 421 if ValueExists('AutoDial') then AutoDial := ReadBool('AutoDial'); 379 422 if ValueExists('AutoRedial') then AutoRedial := ReadBool('AutoRedial'); 380 423 if ValueExists('ConnectionName') then ConnectionName := ReadString('ConnectionName'); 381 424 if ValueExists('NetworkTestLastUpdate') then NetworkTestLastUpdate := StrToDate(ReadString('NetworkTestLastUpdate')); 382 if ValueExists('PingTimeout') then PingTimeout := ReadInteger('PingTimeout'); 425 if ValueExists('PingTimeout') then PingTimeout := ReadInteger('PingTimeout') 426 else PingTimeout := 200; 383 427 finally 384 428 Free; 385 429 end; 386 387 if AutoDial then Dial; 430 Application.ShowMainForm := not FirstStart; 431 388 432 end; 389 433 … … 399 443 RootKey := HKEY_LOCAL_MACHINE; 400 444 OpenKey('\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\GloballyOpenPorts\List', True); 401 Result := (ReadString('137:UDP') = '137:UDP: 192.168.0.0/255.255.0.0:Enabled:@xpsp2res.dll,-22001')402 and (ReadString('138:UDP') = '138:UDP: 192.168.0.0/255.255.0.0:Enabled:@xpsp2res.dll,-22002')403 and (ReadString('139:TCP') = '139:TCP: 192.168.0.0/255.255.0.0:Enabled:@xpsp2res.dll,-22004')404 and (ReadString('445:TCP') = '445:TCP: 192.168.0.0/255.255.0.0:Enabled:@xpsp2res.dll,-22005');445 Result := (ReadString('137:UDP') = '137:UDP:'+LocalNetAddr+'/'+LocalNetMask+':Enabled:@xpsp2res.dll,-22001') 446 and (ReadString('138:UDP') = '138:UDP:'+LocalNetAddr+'/'+LocalNetMask+':Enabled:@xpsp2res.dll,-22002') 447 and (ReadString('139:TCP') = '139:TCP:'+LocalNetAddr+'/'+LocalNetMask+':Enabled:@xpsp2res.dll,-22004') 448 and (ReadString('445:TCP') = '445:TCP:'+LocalNetAddr+'/'+LocalNetMask+':Enabled:@xpsp2res.dll,-22005'); 405 449 finally 406 450 Free; … … 415 459 OpenKey('\SYSTEM\CurrentControlSet\Services\SharedAccess\Parameters\FirewallPolicy\StandardProfile\GloballyOpenPorts\List', True); 416 460 if Value then begin 417 WriteString('137:UDP', '137:UDP: 192.168.0.0/255.255.0.0:Enabled:@xpsp2res.dll,-22001');418 WriteString('138:UDP', '138:UDP: 192.168.0.0/255.255.0.0:Enabled:@xpsp2res.dll,-22002');419 WriteString('139:TCP', '139:TCP: 192.168.0.0/255.255.0.0:Enabled:@xpsp2res.dll,-22004');420 WriteString('445:TCP', '445:TCP: 192.168.0.0/255.255.0.0:Enabled:@xpsp2res.dll,-22005');461 WriteString('137:UDP', '137:UDP:'+LocalNetAddr+'/'+LocalNetMask+':Enabled:@xpsp2res.dll,-22001'); 462 WriteString('138:UDP', '138:UDP:'+LocalNetAddr+'/'+LocalNetMask+':Enabled:@xpsp2res.dll,-22002'); 463 WriteString('139:TCP', '139:TCP:'+LocalNetAddr+'/'+LocalNetMask+':Enabled:@xpsp2res.dll,-22004'); 464 WriteString('445:TCP', '445:TCP:'+LocalNetAddr+'/'+LocalNetMask+':Enabled:@xpsp2res.dll,-22005'); 421 465 end else begin 422 466 WriteString('137:UDP', '137:UDP:LocalSubNet:Enabled:@xpsp2res.dll,-22001'); … … 434 478 I: Integer; 435 479 begin 436 with MagRasCon1 do begin 437 GetConnections; 438 I := 0; 439 while (I < Connections.Count) and (Connections.EntryName(I) <> ConnectionName) do I := I + 1; 440 if not (I < Connections.Count) then begin 441 EntryName := ConnectionName; 442 PhoneNumber := ''; 443 DialHandle := 0; 444 AutoConnectEx(DialHandle); 480 if MagRasCon1.ConnectState = RASCS_Connected then 481 MagRasCon1.DisconnectEx(DialHandle, 0, 3000, False); 482 483 DialingLog.Memo1.Lines.Add(''); 484 DialingLog.Memo1.Lines.Add(DateTimeToStr(Now) + ': Vytáèím pøípojení'); 485 with MagRasCon1 do begin 486 GetConnections; 487 I := 0; 488 while (I < Connections.Count) and (Connections.EntryName(I) <> ConnectionName) do I := I + 1; 489 if not (I < Connections.Count) then begin 490 EntryName := ConnectionName; 491 PhoneNumber := ''; 492 DialHandle := 0; 493 AutoConnectEx(DialHandle); 494 end; 445 495 end; 446 end;447 496 end; 448 497 449 498 procedure TMainForm.Timer1Timer(Sender: TObject); 450 499 begin 451 Dial; 500 if MagRasCon1.ConnectState <> RASCS_Connected then begin 501 Timer1.Enabled := False; 502 Dial; 503 end; 452 504 end; 453 505 … … 458 510 RootKey := RegistryRootKey; 459 511 OpenKey(RegistryPath, True); 512 WriteBool('FirstStart', True); 460 513 WriteBool('AutoDial', AutoDial); 461 514 WriteBool('AutoRedial', AutoRedial); … … 535 588 CheckBox7.Checked := True; 536 589 CheckBox8.Checked := True; 590 CheckBox9.Checked := True; 537 591 ComboBox1.ItemIndex := ComboBox1.Items.IndexOf(DefaultConnectionName); 538 592 end; … … 577 631 end; 578 632 633 procedure TMainForm.Zznamvyten1Click(Sender: TObject); 634 begin 635 if not DialingLog.Visible then DialingLog.ShowModal; 636 end; 637 638 procedure TMainForm.MagRasCon1StateChanged(Sender: TObject); 639 var 640 Info: string; 641 begin 642 if LastState = MagRasCon1.ConnectState then Exit; 643 644 // check type of event 645 Info := ''; 646 case MagRasCon1.StateEventSource of 647 SourceDial: Info := ' Dial: '; 648 SourceStatus: begin 649 Info := ' Status: '; 650 Timer1.Enabled := True; 651 end; 652 SourceHangup: Info := ' Hangup: '; 653 end; 654 655 // see if new event, else display it 656 LastState := MagRasCon1.ConnectState ; 657 DialingLog.Memo1.Lines.Add (Info + MagRasCon1.StatusStr 658 + ' (' + IntToStr(LastState) + ')'); 659 660 // if dialling need to see what's happened 661 if DialHandle <> 0 then begin 662 // online OK, restart timer 663 if (MagRasCon1.ConnectState = RASCS_Connected) then begin 664 // ConnHandle := DialHandle 665 // DialHandle := 0; 666 // TimerStatus.Enabled := True; 667 end; 668 669 // dialling failed, either an error or disconnected 670 if ((MagRasCon1.ConnectState > RASBase) and 671 (MagRasCon1.ConnectState < RASCS_Paused)) or 672 (MagRasCon1.ConnectState = RASCS_Disconnected) then begin 673 // disconnect, returns when done or after three seconds, no StateChanged 674 //ConnHandle := DialHandle; 675 //DialHandle := 0; 676 MagRasCon1.DisconnectEx(DialHandle, 0, 3000, False); 677 Timer1.Enabled := True; 678 // reset is done in timer event 679 end; 680 end; 681 end; 682 683 procedure TMainForm.Init; 684 var 685 WSAErr: string; 686 Hostname: string; 687 begin 688 HostName := 'default'; 689 GetIPFromHost(Hostname, DefaultGateway, WSAErr); 690 DefaultGateway := ReverseString(DefaultGateway); 691 DefaultGateway := '1'+Copy(DefaultGateway, Pos('.', DefaultGateway), 255); 692 DefaultGateway := ReverseString(DefaultGateway); 693 694 //DefaultGateway := GetDefaultGateway; 695 if AutoDial then Dial; 696 end; 697 698 procedure TMainForm.Timer2Timer(Sender: TObject); 699 begin 700 MagRasCon1.CurrentStatusEx(DialHandle, 0); 701 end; 702 703 function GetIPFromHost(var HostName, IPaddr, WSAErr: string): Boolean; 704 type 705 Name = array[0..100] of Char; 706 PName = ^Name; 707 var 708 HEnt: pHostEnt; 709 HName: PName; 710 WSAData: TWSAData; 711 i: Integer; 712 begin 713 Result := False; 714 if WSAStartup($0101, WSAData) <> 0 then begin 715 WSAErr := 'Winsock is not responding."'; 716 Exit; 717 end; 718 IPaddr := ''; 719 New(HName); 720 if GetHostName(HName^, SizeOf(Name)) = 0 then 721 begin 722 HostName := StrPas(HName^); 723 HEnt := GetHostByName(HName^); 724 for i := 0 to HEnt^.h_length - 1 do 725 IPaddr := 726 Concat(IPaddr, 727 IntToStr(Ord(HEnt^.h_addr_list^[i])) + '.'); 728 SetLength(IPaddr, Length(IPaddr) - 1); 729 Result := True; 730 end 731 else begin 732 case WSAGetLastError of 733 WSANOTINITIALISED:WSAErr:='WSANotInitialised'; 734 WSAENETDOWN :WSAErr:='WSAENetDown'; 735 WSAEINPROGRESS :WSAErr:='WSAEInProgress'; 736 end; 737 end; 738 Dispose(HName); 739 WSACleanup; 740 end; 741 579 742 end.
Note:
See TracChangeset
for help on using the changeset viewer.