[Delphi] CreateProcess, en dan meer...

Pagina: 1
Acties:
  • 104 views sinds 30-01-2008
  • Reageer

  • Guillome
  • Registratie: Januari 2001
  • Niet online
Hoi

Ik maak een process aan met createprocess, en dan maak ik dat process "SW_HIDE". Dus dan zie je `m niet.

Maar hoe maak ik `m weer visible? Met ShowWindow? Dat lukt me niet.

Mijn geknoei:
code:
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
var
  Form1: TForm1;
  pInfo:Process_Information;
  sInfo:StartupInfo;

implementation

{$R *.DFM}

procedure TForm1.Button2Click(Sender: TObject);
var PCharArr:PChar;
begin
  PCharArr := AllocMem(SizeOf(Char)*(Length('notepad.exe')+7));
  try
    {This is a startupinfo for CreateProcess}
    FillChar (sInfo, SizeOf (sInfo), 0);
    sInfo.cb := SizeOf (sInfo);    with sInfo do
    begin
    cb := SizeOf(TStartupInfo);
    dwFlags := startf_UseShowWindow;
    wShowWindow := SW_HIDE;
    end;
    CreateProcess(nil,StrPCopy(PCharArr,('notepad.exe')),nil,nil,FALSE,0,nil,nil,sInfo,pInfo);
  finally
    freemem(PCharArr);
  end;
  ShowMessage(IntToStr(pInfo.dwProcessId));
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  ShowMessage(IntToStr(pInfo.dwProcessId));
  DestroyWindow(pInfo.hProcess); 
  ShowWindow(pInfo.dwProcessId, SW_SHOWNORMAL);
  ShowMessage(IntToStr(pInfo.dwProcessId));
  TerminateProcess(pInfo.hProcess, pInfo.hProcess);
end;

If then else matters! - I5 12600KF, Asus Tuf GT501, Gigabyte Gaming OC 16G 5080 RTX, Asus Tuf Gaming H670 Pro, 48GB, Corsair RM850X PSU, SN850 1TB, Arctic Liquid Freezer 280, ASUS RT-AX1800U router


  • .oisyn
  • Registratie: September 2000
  • Laatst online: 08-09 12:35

.oisyn

Moderator Devschuur®

Demotivational Speaker

voor ShowWindow heb je de window handle (HWND) nodig van de window die je wilt laten zien. Dus die window zul je moeten vinden...

Als je de titel van de window weet dan kun je m vinden met FindWindow (), anders zul je met EnumWindows () alle windows moeten langslopen, en controleren met GetWindowThreadProcessId () of een van de windows idd van het betreffende process is (het processID van het proces dat je net hebt gestart staat in de PROCESS_INFORMATION struct)

.edit: typo :)

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


  • Guillome
  • Registratie: Januari 2001
  • Niet online
`t Moest wel EnumWindows zijn :)
Ik maar denken, waarom doet ie t nu niet
Maar ik ga weer puzzelen :) Tnx

[edit]
Snap ter nog nix van, van dat EnumWindows

If then else matters! - I5 12600KF, Asus Tuf GT501, Gigabyte Gaming OC 16G 5080 RTX, Asus Tuf Gaming H670 Pro, 48GB, Corsair RM850X PSU, SN850 1TB, Arctic Liquid Freezer 280, ASUS RT-AX1800U router


  • .oisyn
  • Registratie: September 2000
  • Laatst online: 08-09 12:35

.oisyn

Moderator Devschuur®

Demotivational Speaker

EnumWindows is een functie die alle windows afloopt, en bij elke window een door jouw opgegeven functie aanroept (een callback dus)

ik heb geen verstand van Delphi, maar in C doe je dat zo:
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
BOOL CALLBACK enumWindowsCallback (HWND hWnd, LPARAM param)
{
    if (isDeGoedeWindow (hWnd)) // is dit de juiste window?
    {
      ShowWindow (hWnd, SW_SHOW); // laat zien
      return FALSE;      // stoppen met de enumeratie
    }

    return TRUE; // window nog niet gevonden, dus doorgaan
}


// ergens in jouw code
EnumWindows (enumWindowsCallback, 0);

die param is gewoon een eigen opgegeven waarde die ook naar de callback functie wordt gestuurd

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


Verwijderd

Is dit wat je wilt?
code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
procedure TForm1.Button1Click(Sender: TObject);
  function EnumWindProc(hWnd: HWND;lParam: LPARAM): BOOL; stdcall;
  begin
    Result:=lParam=1234;
    if Result then
    ShowWindow(hWnd, SW_SHOWNORMAL);
  end;
var
  siNotepad: STARTUPINFO;
  piNotepad: PROCESS_INFORMATION;
begin
  FillChar(siNotepad, SizeOf(STARTUPINFO), 0);
  FillChar(piNotepad, SizeOf(PROCESS_INFORMATION), 0);
  siNotepad.cb := SizeOf(STARTUPINFO);
  siNotepad.dwFlags := STARTF_USESHOWWINDOW;
  siNotepad.lpTitle := nil;
  siNotepad.wShowWindow := SW_HIDE;
  CreateProcess(nil, 'notepad.exe', nil, nil, False, 0, nil, nil, siNotepad, piNotepad);
  EnumThreadWindows(piNotepad.dwThreadId, @EnumWindProc, 1234);
end;

  • .oisyn
  • Registratie: September 2000
  • Laatst online: 08-09 12:35

.oisyn

Moderator Devschuur®

Demotivational Speaker

oh ja stom, EnumThreadWindows (), helemaal vergeten |:(

Give a man a game and he'll have fun for a day. Teach a man to make games and he'll never have fun again.


  • Guillome
  • Registratie: Januari 2001
  • Niet online
Tnx hvdberg
Je bent een kei!!!!!
Hij werkt mjummie goed!!!! :)
Tnx!!!: D

Ik heb nu dit
code:
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
var
  Form1: TForm1;
  pInfo:Process_Information;
  sInfo:StartupInfo;

implementation

{$R *.DFM}

procedure TForm1.Button2Click(Sender: TObject);
var PCharArr:PChar;
begin
  PCharArr := AllocMem(SizeOf(Char)*(Length('notepad.exe')+7));
  try
    {This is a startupinfo for CreateProcess}
    FillChar (sInfo, SizeOf (sInfo), 0);
    sInfo.cb := SizeOf (sInfo);    with sInfo do
    begin
    cb := SizeOf(TStartupInfo);
    dwFlags := startf_UseShowWindow;
    wShowWindow := SW_HIDE;
    end;
    CreateProcess(nil,StrPCopy(PCharArr,('notepad.exe')),nil,nil,FALSE,0,nil,nil,sInfo,pInfo);
  finally
    freemem(PCharArr);
  end;
  ShowMessage(IntToStr(pInfo.dwProcessId));
end;

procedure TForm1.Button1Click(Sender: TObject);
  function EnumWindProc(hWnd: HWND;lParam: LPARAM): BOOL; stdcall;
  begin
    Result:=lParam=1234;
    if Result then
    ShowWindow(hWnd, SW_SHOWNORMAL);
  end;
begin
  EnumThreadWindows(pInfo.dwThreadId, @EnumWindProc, 1234);
end;

Zou je misschien alleen die onderse Button1Click kunnen uitleggen hoe die werkt/ wat ie doet?

If then else matters! - I5 12600KF, Asus Tuf GT501, Gigabyte Gaming OC 16G 5080 RTX, Asus Tuf Gaming H670 Pro, 48GB, Corsair RM850X PSU, SN850 1TB, Arctic Liquid Freezer 280, ASUS RT-AX1800U router


Verwijderd

Op woensdag 08 mei 2002 19:40 schreef XLerator het volgende:
Tnx hvdberg
Je bent een kei!!!!!
Hij werkt mjummie goed!!!! :)
Tnx!!!: D

Ik heb nu dit
code:
1
2
3
4
5
6
7
8
9
10
11
...
procedure TForm1.Button1Click(Sender: TObject);
  function EnumWindProc(hWnd: HWND;lParam: LPARAM): BOOL; stdcall;
  begin
    Result:=lParam=1234;
    if Result then
    ShowWindow(hWnd, SW_SHOWNORMAL);
  end;
begin
  EnumThreadWindows(pInfo.dwThreadId, @EnumWindProc, 1234);
end;

Zou je misschien alleen die onderse Button1Click kunnen uitleggen hoe die werkt/ wat ie doet?
Met behulp van de EnumThreadWindows API kun je alle windows van een een thread langslopen (enumeration). Deze API vraagt 3 parameters, namelijk:
1) Een thread ID (thread met windows om langs te lopen zeg maar)
2) Een callback functie (functie welke wordt aangeroepen door de API als er een window wordt gevonden). Door de @ geven we een pointer naar deze functie mee. In dit geval heb ik de functie genest binnen de Button1Click maar dat hoeft niet; deze mag er ook buiten. Let wel op de stdcall achter de functie.
3) Een parameter naar keuze. Deze wordt met bij het aanroepen van de callback functie meegegeven en kan derhalve als controle waarde worden meegegeven (1234 is zomaar een voorbeeld).

De callback functie wordt net zolang aangeroepen door de API totdat het resultaat False is of er geen windows in het gespecificeerde thread zijn om te enumeraten.
Pagina: 1