Entwickler-Ecke

Dateizugriff - ShellExecute mit wait


JeanvanHees - Di 15.10.02 09:37
Titel: ShellExecute mit wait
Gutenmorgen, :wink2:
mittag, abend (für langschläfer)

Ich hab diese procedure von internet.
Die wait funktionalität war das wichtigste für mich...
Aber manchmal funktioniert sie nicht....
Vielleicht weißt ihr warum?
Hier ist sie...

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:
////////////////////////////////////////////////////////////////
// AppName:  name (including path) of the application
// AppArgs:  command line arguments
// Wait:     0 = don't wait on application
//           >0 = wait until application has finished (maximum in milliseconds)
//           <0 = wait until application has started (maximum in milliseconds)
// Hide:     True = application runs invisible in the background
// ExitCode: exitcode of the application (only avaiable if Wait <> 0)
//
function STO_ShellExecute(const AppName, AppArgs: String; const Wait: Integer;
  const Hide: Boolean; var ExitCode: DWORD): Boolean;
var
  myStartupInfo: TStartupInfo;
  myProcessInfo: TProcessInformation;
  sAppName: String;
  iWaitRes: Integer;
begin
  // initialize the startupinfo
  FillChar(myStartupInfo, SizeOf(TStartupInfo), 0);
  myStartupInfo.cb := Sizeof(TStartupInfo);
  myStartupInfo.dwFlags := STARTF_USESHOWWINDOW;
  if Hide then // hide application
    myStartupInfo.wShowWindow := SW_HIDE
  else // show application
    myStartupInfo.wShowWindow := SW_SHOWNORMAL;

  // prepare applicationname
  sAppName := AppName;
  if (Length(sAppName) > 0) and (sAppName[1] <> '"') then
    sAppName := '"' + sAppName + '"';

  // start process
  ExitCode := 0;
  Result := CreateProcess(nil, PChar(sAppName + ' ' + AppArgs), nil, nil, False,
              NORMAL_PRIORITY_CLASS, nil, PChar(ExtractFilePath(AppName)),
              myStartupInfo, myProcessInfo);

  // could process be started ?
  if Result then
  begin
    // wait on process ?
    if (Wait <> 0) then
    begin
      if (Wait > 0) then // wait until process terminates
        iWaitRes := WaitForSingleObject(myProcessInfo.hProcess, Wait)
      else // wait until process has been started
        iWaitRes := WaitForInputIdle(myProcessInfo.hProcess, Abs(Wait));
      // timeout reached ?
      if iWaitRes = WAIT_TIMEOUT then
      begin
        Result := False;
        TerminateProcess(myProcessInfo.hProcess, 1);
      end;
      // getexitcode
      GetExitCodeProcess(myProcessInfo.hProcess, ExitCode);
    end;
    CloseHandle(myProcessInfo.hProcess);
  end;
end;


Delete - Di 15.10.02 09:49

Es wäre sehr hilfreich...
Wenn du uns auch sagen würdest...
Wann sie nicht funktioniert...
Oder unterwelchen Bedingungen.

:wink:


JeanvanHees - Di 15.10.02 10:00

Naja, das ist ja das fremde....
Ich brauche die procedur um einen file um zu wandeln, und bei einige files funktioniert er, und bei anderen nicht...
Sie sind all vom gleichen typ, ob es jetzt die länge ist?
:nixweiss:
Ich kann vielleicht mit debugging noch mal suchen..aber es ist so eine endlose suche ...die ursache kann ja alles sein!


Delete - Di 15.10.02 10:10

Na ja, wenn du noch nicht mal weißt, wo der Fehler liegen könnte, woher sollen wir es denn wissen? Wir kennen nur die Prozedur aber nicht den Rest des Programmes. Wer weiß was du noch so machst.


JeanvanHees - Di 15.10.02 10:37

Ich kann einfach nicht mehr informationen geben. :roll:
Es liegt nach 99% warscheinlichkeit an die procedur.
Tut mir leid, aber ich kann hier nicht mehr code darüber posten...

Naja, dann muß ich mich mal irgendwo anders umsehen um das problem zu lösen.
:nut:


JeanvanHees - Mi 16.10.02 08:57

Naja, hab schon die lösung, es war etwas so blödes das ich sicher weiß das ich euch am :rofl: kriege.
Ich habe ein fehler gemacht beim kopieren.... :oops:
Also die procedur die hier steht ist gut und wirkt prima...
Wenn mann nicht zu blöd ist um ordentlich zu kopieren...
Tut mir leid leute das ich immer fragen stelle die ich im endeffect selber löse. :dunce: