Autor Beitrag
Blamaster
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164



BeitragVerfasst: Mo 03.09.07 15:36 
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:

ausblenden volle Höhe 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:
ausblenden 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:

ausblenden 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 125

Win7
D7 Ent.
BeitragVerfasst: 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.

ausblenden volle Höhe 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:

ausblenden volle Höhe 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
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1338
Erhaltene Danke: 120

Win 10
RIO, CE, Lazarus
BeitragVerfasst: 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:
ausblenden Delphi-Quelltext
1:
function ExecConsole(var Output, Errors: Stringconst pwid: String): Boolean;					

_________________
Wir zerstören die Natur und Wälder der Erde. Wir töten wilde Tiere für Trophäen. Wir produzieren Lebewesen als Massenware um sie nach wenigen Monaten zu töten. Warum sollte unser aller Mutter, die Natur, nicht die gleichen Rechte haben?
GTA-Place
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
EE-Regisseur
Beiträge: 5248
Erhaltene Danke: 2

WIN XP, IE 7, FF 2.0
Delphi 7, Lazarus
BeitragVerfasst: Mo 03.09.07 21:27 
Das kann aber alles nicht funktionieren, da er PWId erst nach dem Aufruf der Funktion einen Wert zuweist ;-)

_________________
"Wer Ego-Shooter Killerspiele nennt, muss konsequenterweise jeden Horrorstreifen als Killerfilm bezeichnen." (Zeit.de)
Blamaster Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 164



BeitragVerfasst: 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.