Entwickler-Ecke

Windows API - Ausführen, Starten?


Grindfucker - Mi 23.11.05 14:26
Titel: Ausführen, Starten?
Wie kann ich programme (zb. command.exe) öffnen lassen und automatisch etwas eintragen.

zb. (öffne)cmd, (schreibe)ping 127.0.0.1

mfg, Grind



edit: habe es mit SHELLEXECUTE probier aber das kennt er anscheinend nicht, habe auch in der Hilfe und mit dem Suchen button probiert. Ich weiß einfach nicht mehr weiter. Ich benutze Delphi 6.


Moderiert von user profile iconGausi: Topic aus Sonstiges verschoben am Mi 23.11.2005 um 15:38


matze.de - Mi 23.11.05 14:38

Guck dir mal die Befehle Suche in: Delphi-Forum SHELLEXECUTE (USES ShellAPI) und Suche in: Delphi-Forum SENDMESSAGE an.

mfg matze


Grindfucker - Mi 23.11.05 15:17

Kannst du mir ein bespiel für SENDMESSAGE geben (vielleicht im Quelltext) wäre sehr nett.


Martin1966 - Mi 23.11.05 15:56
Titel: Re: Ausführen, Starten?
user profile iconGrindfucker hat folgendes geschrieben:
habe es mit SHELLEXECUTE probier aber das kennt er anscheinend nicht

Einfach die Unit ShellAPI einbinden. ;-)


Pepe - Mi 23.11.05 21:19

WinExec('cmd /C ping 123.123.123.123', SW_SHOW);

sollte auch funktionieren...


Hux - So 27.11.05 15:16

is vom Easy Helper:

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:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
function GetConsoleOutput(const Command: Stringvar Output, Errors: TStringList): Boolean;
var
  StartupInfo: TStartupInfo;
  ProcessInfo: TProcessInformation;
  SecurityAttr: TSecurityAttributes;
  PipeOutputRead: THandle;
  PipeOutputWrite: THandle;
  PipeErrorsRead: THandle;
  PipeErrorsWrite: THandle;
  Succeed: Boolean;
  Buffer: array [0..255of Char;
  NumberOfBytesRead: DWORD;
  Stream: TMemoryStream;
begin
  //Initialisierung ProcessInfo
  FillChar(ProcessInfo, SizeOf(TProcessInformation), 0);

  //Initialisierung SecurityAttr
  FillChar(SecurityAttr, SizeOf(TSecurityAttributes), 0);
  SecurityAttr.nLength := SizeOf(SecurityAttr);
  SecurityAttr.bInheritHandle := true;
  SecurityAttr.lpSecurityDescriptor := nil;

  //Pipes erzeugen
  CreatePipe(PipeOutputRead, PipeOutputWrite, @SecurityAttr, 0);
  CreatePipe(PipeErrorsRead, PipeErrorsWrite, @SecurityAttr, 0);

  //Initialisierung StartupInfo
  FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
  StartupInfo.cb:=SizeOf(StartupInfo);
  StartupInfo.hStdInput := 0;
  StartupInfo.hStdOutput := PipeOutputWrite;
  StartupInfo.hStdError := PipeErrorsWrite;
  StartupInfo.wShowWindow := sw_Hide;
  StartupInfo.dwFlags := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;

  if  CreateProcess(nil, PChar(command), nilnil, true,
  CREATE_DEFAULT_ERROR_MODE or CREATE_NEW_CONSOLE or NORMAL_PRIORITY_CLASS, nilnil,
  StartupInfo, ProcessInfo) then begin
    result:=true;
    //Write-Pipes schließen
    CloseHandle(PipeOutputWrite);
    CloseHandle(PipeErrorsWrite);

    //Ausgabe Read-Pipe auslesen
    Stream := TMemoryStream.Create;
    try
      while true do begin
        succeed := ReadFile(PipeOutputRead, Buffer, 255, NumberOfBytesRead, nil);
        if not succeed then break;
        Stream.Write(Buffer, NumberOfBytesRead);
      end;
      Stream.Position := 0;
      Output.LoadFromStream(Stream);
    finally
      Stream.Free;
    end;
    CloseHandle(PipeOutputRead);

    //Fehler Read-Pipe auslesen
    Stream := TMemoryStream.Create;
    try
      while true do begin
        succeed := ReadFile(PipeErrorsRead, Buffer, 255, NumberOfBytesRead, nil);
        if not succeed then break;
        Stream.Write(Buffer, NumberOfBytesRead);
      end;
      Stream.Position := 0;
      Errors.LoadFromStream(Stream);
    finally
      Stream.Free;
    end;
    CloseHandle(PipeErrorsRead);

    WaitForSingleObject(ProcessInfo.hProcess, INFINITE);
    CloseHandle(ProcessInfo.hProcess);
  end
  else begin
    result:=false;
    CloseHandle(PipeOutputRead);
    CloseHandle(PipeOutputWrite);
    CloseHandle(PipeErrorsRead);
    CloseHandle(PipeErrorsWrite);
  end;
end;

So ruft man des danna uf:

Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
procedure TForm1.Button1Click(Sender: TObject);
var output, errors: TStringList;
begin
  output:=TStringList.Create;
  try
    errors:=TStringList.Create;
    if GetConsoleOutput('cmd /c dir c:\', output, errors) then
      Memo1.Lines.AddStrings(output);
  finally
    output.free;
    errors.free;
  end;
end;


Grindfucker - Mi 30.11.05 08:56

Vielen Dank, ich habe es geschafft

Mit WinExec('cmd /C ping 123.123.123.123', SW_SHOW); hat es geklappt


Delete - Mi 30.11.05 13:15

user profile iconGrindfucker hat folgendes geschrieben:
Vielen Dank, ich habe es geschafft

Mit WinExec('cmd /C ping 123.123.123.123', SW_SHOW); hat es geklappt


WinExec ist von Microsoft als obsolt gekennzeichnet und nur noch aus kompatibilitäts Gründen vorhanden:
Zitat:

Note This function is provided only for compatibility with 16-bit Windows. Applications should use the CreateProcess function.

Benutz besser Shellexecute oder CreateProcess. Unter Windows Vista könnte sie schon nicht mehr vorhanden sein, da dort keine 16-Bit Programme mehr unterstützt werden.