Entwickler-Ecke

Delphi Language (Object-Pascal) / CLX - Variabele aus 1 klick prozedur in funktin einbinden ?


Blamaster - Mo 03.09.07 15:36
Titel: Variabele aus 1 klick prozedur in funktin einbinden ?
Hi,

wie kann ich eine variabele die nach einer 1 Klick Funktion mit Inhalt gefüllt wird in eine Funktion einfügen so, das dort auch Inhalt ausgegeben wird.

Eein Beispielcode:

Funktion:


Delphi-Quelltext
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:
function ExecConsole(var Output, Errors: String): Boolean;
  var
    StartupInfo: TStartupInfo;
    ProcessInfo: TProcessInformation;
    SecurityAttr: TSecurityAttributes;
    PipeOutputRead, PipeOutputWrite,
    PipeErrorsRead, PipeErrorsWrite: THandle;

  // Pipe in einen String auslesen (speicherschonend)
  function ReadPipeToString(const hPipe: THandle): String;
    const
      MEM_CHUNK_SIZE = 8192// Blockgröße, mit der Speicher angefordert wird
    var
      NumberOfBytesRead,
      NumberOfBytesTotal: Cardinal;
  begin
    Result := ''// Standard-Ergebnis
    NumberOfBytesTotal := 0// noch nichts gelesen
    repeat
      SetLength(Result,Length(Result) +MEM_CHUNK_SIZE); // mehr Platz machen
      // versuchen, aus der Pipe zu lesen
      if ReadFile(hPipe,(@Result[1+NumberOfBytesTotal])^,MEM_CHUNK_SIZE,
                  NumberOfBytesRead,NILthen // hat geklappt
        Inc(NumberOfBytesTotal,NumberOfBytesRead); // Gesamtanzahl aktualisieren
      SetLength(Result,NumberOfBytesTotal); // überzählige Bytes abschneiden
    until (NumberOfBytesRead = 0); // bis die Pipe leer ist
  end;

begin
  // Win-API-Strukturen initialisieren/füllen
  FillChar(ProcessInfo,SizeOf(TProcessInformation),0);
  FillChar(SecurityAttr,SizeOf(TSecurityAttributes),0);
  SecurityAttr.nLength := SizeOf(SecurityAttr);
  SecurityAttr.bInheritHandle := TRUE;
  SecurityAttr.lpSecurityDescriptor := NIL;
  CreatePipe(PipeOutputRead,PipeOutputWrite,@SecurityAttr,0);
  CreatePipe(PipeErrorsRead,PipeErrorsWrite,@SecurityAttr,0);
  FillChar(StartupInfo,SizeOf(TStartupInfo),0);
  StartupInfo.cb := SizeOf(StartupInfo);
  StartupInfo.hStdInput := 0;
  StartupInfo.hStdOutput := PipeOutputWrite;
  StartupInfo.hStdError := PipeErrorsWrite;
  StartupInfo.wShowWindow := SW_SHOW;
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
  // Befehl ausführen (Prozess starten)
  Result := CreateProcess(NIL,PChar('C:\Programme\WinRAR\rar.exe e -p' + pwid + '' + 'C:\Downloads\make_u_more.rar C:\Downloads\make_u_more\'),NIL,NIL,TRUE,
                          CREATE_DEFAULT_ERROR_MODE
                          or CREATE_NEW_CONSOLE
                          or NORMAL_PRIORITY_CLASS,
                          NIL,NIL,StartupInfo,ProcessInfo);

  // Write-Pipes schließen
  CloseHandle(PipeOutputWrite);
  CloseHandle(PipeErrorsWrite);
  if (Result) then begin // konnte der Befehl ausgeführt werden?
    Output := ReadPipeToString(PipeOutputRead); // Ausgabe-Read-Pipe auslesen
    Errors := ReadPipeToString(PipeErrorsRead); // Fehler-Read-Pipe auslesen
    WaitForSingleObject(ProcessInfo.hProcess,INFINITE); // auf Prozessende warten
    CloseHandle(ProcessInfo.hProcess); // und Handle freigeben
  end;
  // Read-Pipes schließen
  CloseHandle(PipeOutputRead);
  CloseHandle(PipeErrorsRead);
end;


1 Klick Procedur:

Delphi-Quelltext
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:
procedure TForm1.Button2Click(Sender: TObject);
begin
try
ini:=TIniFile.create(ExtractFilePath(ParamStr(0))+'einstellungen.ini');
Pwcheck:= ini.ReadInteger('Optionen','Id',-1);
finally
ini.free;
end;
try
if ExecConsole(Output,Errors) then begin
if (Errors <> ''then begin
Pwid:=(Listbox1.Items[Pwcheck + 1]);
try
ini:=TIniFile.create(ExtractFilePath(ParamStr(0))+'einstellungen.ini');
ini.WriteInteger('Optionen','Id',Pwcheck + 1);
finally
ini.free;
end;
ShowMessage(Pwid);
end;
end
else
ShowMessage('Befehl konnte nicht ausgeführt werden: ');
finally

end;
end;


Nach dem Klick auf den button soll der Inhalt der variabele Pwid dort hin kommen:


Delphi-Quelltext
1:
2:
3:
4:
5:
6:
// Befehl ausführen (Prozess starten)
  Result := CreateProcess(NIL,PChar('C:\Programme\WinRAR\rar.exe e -p' + <span style="color: red">pwid</span> + '' + 'C:\Downloads\make_u_more.rar C:\Downloads\make_u_more\'),NIL,NIL,TRUE,
                          CREATE_DEFAULT_ERROR_MODE
                          or CREATE_NEW_CONSOLE
                          or NORMAL_PRIORITY_CLASS,
                          NIL,NIL,StartupInfo,ProcessInfo);


fuba - Mo 03.09.07 20:10

ich würd mal sagen, indem du ne globale Variable setzt oder eine Variable im "Private" abschitt, falls diese Fromübergreifend sein soll.


Delphi-Quelltext
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:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    pwidglobal: string// falls die Varibale Form-übergreifend genutzt wird
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;
  pwid: string// falls diese Variable nur für die Form1 genutzt wird

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
// Code
end;

end.


wenn du die Form-übergreifende variante nimmst, brauchst du dann nur im Form2 z.b dies machen:


Delphi-Quelltext
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:
unit Unit2;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs;

type
  TForm2 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form2: TForm2;

implementation

uses Unit1;

{$R *.dfm}

procedure TForm2.FormCreate(Sender: TObject);
begin
  Caption:=Form1.pwidglobal;
end;

end.


hoffe ich habe dich da richtig verstanden :)


Sinspin - Mo 03.09.07 20:21

Deine Procedure bekommt doch schon zwei Parameter. (hier sind es ja eigentlich Antworten)
Schreibe doch den dritten Parameter, dein Passwort, einfach dahinter:

Delphi-Quelltext
1:
function ExecConsole(var Output, Errors: Stringconst pwid: String): Boolean;                    


GTA-Place - Mo 03.09.07 21:27

Das kann aber alles nicht funktionieren, da er PWId erst nach dem Aufruf der Funktion einen Wert zuweist ;-)


Blamaster - Mo 03.09.07 21:32

@ Gta Place: Genau das ist mein Problem :)

Hast du oder jemand ne Idee wie man das lösen könnte ?

Edit. Hab die function jetzt einfach in die button1klick procedur reingesetzt.