Autor Beitrag
Postman
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

Windows 7
Delphi 2010
BeitragVerfasst: Di 23.02.10 13:01 
Hallo,

ich habe eine Test GUI die eine Konsolenanwendung per ShellExecute aufruft. Der Aufruf funktioniert soweit wunderbar (Konsole poppt auf, Programm läuft sauber an), allerdings kann ich die Konsole nicht aus der GUI heraus per Strg+C beenden, wie es manuell möglich ist (der Sinn dahinter ist, dass die Konsolenanwendung mit Strg+C sauber herunterfährt, beim Abschießen entstehen diverse Probleme).

Der Ansatz ist, sich aus der GUI heraus mit AttachConsole() an die Konsole der Konsolenanwendung "dranzuhängen" und dann das GenerateConsoleCtrlEvent() abzuschicken. Der Errorcode beim Ausführen des AttachConsole() ist 5 (Access is denied.). Die Frage ist, warum ist es mir nicht möglich, mich an die Konsole dranzuhängen? Beide Anwendungen werden vom gleichen User aus aufgerufen.

Hier der Code:

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:
Function AttachConsoleDummy(dwProcessId: LongWord): LongBool;
  Begin
    Result := False;
End;

procedure TForm1.Button1Click(Sender: TObject);
var
  PID: DWORD;
  AttachConsole: Function(dwProcessId: LongWord): LongBool; StdCall;
  errorcode: integer;
begin
  // TRY TO SHUTDOWN
  GetProcessHandle(PID);

  AttachConsole := GetProcAddress(GetModuleHandle('kernel32.dll'), 'AttachConsole');
  If @AttachConsole = nil Then AttachConsole := @AttachConsoleDummy;
  AttachConsole(PID);
  errorcode := GetLastError;

  GenerateConsoleCtrlEvent(CTRL_C_EVENT,0);
  errorcode := GetLastError;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  filestring;
  myHandle: THandle;
  PID: DWORD;
  return: integer;
begin
  fServicePath := 'D:/';
  file := fServicePath + 'meineexe.exe';
  ZeroMemory(@ffileProcInfo, SizeOf(ffileProcInfo));
  return := ShellExecute(Handle, nil, PChar(file), nilnil, SW_SHOW);
end;


function GetProcessHandle(var PID: DWORD): THandle;
begin
  PID := GetPidFromProcessName('meineexe.exe');
  Result := OpenProcess(PROCESS_ALL_ACCESS, False, PID);
end;
ALF
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 1085
Erhaltene Danke: 53

WinXP, Win7, Win10
Delphi 7 Enterprise, XE
BeitragVerfasst: Di 23.02.10 13:35 
Hi, ich hoffe nix falsch zu sagen, aber wer erhält das Handle nach diesem Aufruf?
ausblenden Delphi-Quelltext
1:
GetProcessHandle(PID);					

müsste es nicht so ähnlich sein?
ausblenden Delphi-Quelltext
1:
hndl:= GetProcessHandle(PID);					

Irgendwie ist mir das nicht ganz logisch, hab aber nicht soviel Ahnung davon, aber
ausblenden Delphi-Quelltext
1:
AttachConsole(PID);					

PID ist doch irgendwie nicht gesetzt oder?

Sollte man das return nicht auswerten?
ausblenden Delphi-Quelltext
1:
return := ShellExecute(Handle, nil, PChar(file), nilnil, SW_SHOW);					

Gruss Alf

_________________
Wenn jeder alles kann oder wüsste und keiner hätt' ne Frage mehr, omg, währe dieses Forum leer!
Jakob_Ullmann
ontopic starontopic starontopic starontopic starontopic starontopic starofftopic starofftopic star
Beiträge: 1747
Erhaltene Danke: 15

Win 7, *Ubuntu GNU/Linux*
*Anjuta* (C, C++, Python), Geany (Vala), Lazarus (Pascal), Eclipse (Java)
BeitragVerfasst: Di 23.02.10 17:11 
ALF, doch PID ist gesetzt, weil die function PID als Argument als Referenz entgegennimmt, das heißt sie setzt diese Variable (erkennbar am var). -> Call by reference / call by value

@Postman: Naja, eigentlich reicht es ja, wenn du die Konsole fokusieren kannst, dann kannst du den Tastendruck simulieren. Zum Beispiel so:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
keybd_event(VK_CONTROL, 000);
keybd_event(Ord('C'),   000);
keybd_event(Ord('C'),   0, KEYEVENTF_KEYUP, 0);
keybd_event(VK_CONTROL, 0, KEYEVENTF_KEYUP, 0);
Postman Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 41

Windows 7
Delphi 2010
BeitragVerfasst: Mi 24.02.10 13:57 
Danke euch beiden, aber hat sich inzwischen erledigt.

Der obige Code funktioniert, es war nur ein Rechte Problem unter Windows 7, die UAC hat dazwischengefunkt und offenbar somit den Zugriff verweigert.