Board index » delphi » Killing a process and saving edit text

Killing a process and saving edit text

Hi
I'm inexperienced with Delphi(2) and I have had a problem (2) with my
latest project. Basically I am writing a configuration GUI for a
windowless application (No forms).  

My first problem is killing a process. I know how to kill a window but I
don't know how to kill a 'windowless' process.

My second problem is saving the contents of an edit. I have been using
'lines.add' to put the contents of the edits into a memo and saving it
but when I use 'edit1.text := (Memo1.Lines[0])' to get the lines back I
only get individual characters.

Has anyone used the Talisman shell replacement with Delphi?

TiA
Paul

 

Re:Killing a process and saving edit text


Quote
> My second problem is saving the contents of an edit. I have been using
> 'lines.add' to put the contents of the edits into a memo and saving it
> but when I use 'edit1.text := (Memo1.Lines[0])' to get the lines back I
> only get individual characters.

I don't see any mistake in code you've posted, following code works in
Delphi 5 and it should work in any other version.

procedure TForm1.Button1Click(Sender: TObject);
begin
  Memo1.Lines.Clear;
  Memo1.Lines.Add(Edit1.Text);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  Edit1.Text := Memo1.Lines[0];
end;

--
Martin Prikryl
xpri...@vse.cz
http://sorry.vse.cz/~xprim14

Re:Killing a process and saving edit text


Hi Paul,

here's how to kill a process:

{---------------------------------------------------------------------------
---}
Procedure TerminateExe(Exename : String);
Var pID : DWORD;
Begin
  If WindowsIsNT
    then pID := GetProcessHandleFromExeNT(Exename)
    else pID := GetProcessHandleFromExe(Exename);
  If pID = 0
    then Raise Exception.Create(Exename+' is not running, cannot terminate
it!');
  If not TerminateProcess(pID, 0)
    then RaiseLastWin32Error;
End;

it uses PSAPI and TLHelp32 and these functions:

{---------------------------------------------------------------------------
---}
Function WindowsIsNT : Boolean;
Var osv : TOSVersionInfo;
Begin
  osv.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
  GetVersionEx(osv);
  If osv.dwPlatformId = VER_PLATFORM_WIN32_NT
    then Result := True
    else Result := False;
End;

{---------------------------------------------------------------------------
---}
Function GetProcessHandleFromExeNT(Filename : String) : DWORD;
Var i : LongInt;
    Exename : String;
    PIDs : Array[0..1000] of DWord; file://process-IDs
    ListSize, NoOfProcesses : DWord;
    Handle : THandle;
    szName, fullname : array [0..MAX_PATH - 1] of char;
Begin
  Filename := AnsiUpperCase(Trim(Filename));
  Result := 0;
  ListSize := SizeOf(PIDs);
  NoOfProcesses := 0;
  FillChar(PIDs, ListSize, 0);
  If not EnumProcesses(@PIDs, ListSize, NoOfProcesses)
    then RaiseLastWin32Error;
  NoOfProcesses := NoOfProcesses div SizeOf(DWord);
  For i := 0 to NoOfProcesses-1 do begin
    Handle := OpenProcess(PROCESS_QUERY_INFORMATION + PROCESS_VM_READ +
PROCESS_TERMINATE, False, PIDs[i]);
    If Handle = 0 then begin
      CloseHandle(Handle); file://##could be a mistake
      Continue; file://cannot open that process, guess I do not have the
necessary rights
    end;
    FillChar(szName, SizeOf(szName), 0);
    If GetModuleBaseName(Handle, 0, @szName, sizeof (szName)) > 0 then begin
      GetModuleFileNameEx(Handle, 0, @Fullname, sizeof(Fullname));
      Exename := AnsiUpperCase(Trim(StrPas(FullName)));
      If Exename = Filename then begin
        Result := Handle;
        Break;
      end else CloseHandle(Handle);
    end else CloseHandle(Handle);
  end;
End;

{---------------------------------------------------------------------------
---}
Function GetProcessHandleFromExe(Filename : String) : DWORD;
Var Snap  : Integer;
    Proc  : TProcessEntry32;
    Exename : String;
Begin
  Filename := AnsiUpperCase(Trim(Filename));
  Result := 0;
  Snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
  try
    Proc.dwSize := SizeOf(Proc);
    if(Process32First(Snap, Proc))then
    repeat
      Exename := StrPas(Proc.szExeFile);
      If AnsiUpperCase(Trim(Exename)) = Filename then begin
        Result := OpenProcess(PROCESS_TERMINATE, True, Proc.th32ProcessID);
        Break;
      end;
    until not(Process32Next(Snap,Proc));
  finally
    Windows.CloseHandle(Snap);
  end;
End;

HTH,
hannes
--
http://www.talknet.de/~hannes.breuer/
<P...@antionline.com> schrieb im Newsbeitrag
news:hDKEnBA7Ote5QwQL@btinternet.com...

Quote
> Hi
> I'm inexperienced with Delphi(2) and I have had a problem (2) with my
> latest project. Basically I am writing a configuration GUI for a
> windowless application (No forms).

> My first problem is killing a process. I know how to kill a window but I
> don't know how to kill a 'windowless' process.

> My second problem is saving the contents of an edit. I have been using
> 'lines.add' to put the contents of the edits into a memo and saving it
> but when I use 'edit1.text := (Memo1.Lines[0])' to get the lines back I
> only get individual characters.

> Has anyone used the Talisman shell replacement with Delphi?

> TiA
> Paul

Re:Killing a process and saving edit text


Thanks for the help.

In article <D8yf5.2279$T2.40...@news.tli.de>, Hannes Breuer
<hannes.bre...@talknet.de> writes

Quote
>Hi Paul,

>here's how to kill a process:

>{---------------------------------------------------------------------------
>---}
>Procedure TerminateExe(Exename : String);
>Var pID : DWORD;
>Begin
>  If WindowsIsNT
>    then pID := GetProcessHandleFromExeNT(Exename)
>    else pID := GetProcessHandleFromExe(Exename);
>  If pID = 0
>    then Raise Exception.Create(Exename+' is not running, cannot terminate
>it!');
>  If not TerminateProcess(pID, 0)
>    then RaiseLastWin32Error;
>End;

>it uses PSAPI and TLHelp32 and these functions:

>{---------------------------------------------------------------------------
>---}
>Function WindowsIsNT : Boolean;
>Var osv : TOSVersionInfo;
>Begin
>  osv.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
>  GetVersionEx(osv);
>  If osv.dwPlatformId = VER_PLATFORM_WIN32_NT
>    then Result := True
>    else Result := False;
>End;

>{---------------------------------------------------------------------------
>---}
>Function GetProcessHandleFromExeNT(Filename : String) : DWORD;
>Var i : LongInt;
>    Exename : String;
>    PIDs : Array[0..1000] of DWord; file://process-IDs
>    ListSize, NoOfProcesses : DWord;
>    Handle : THandle;
>    szName, fullname : array [0..MAX_PATH - 1] of char;
>Begin
>  Filename := AnsiUpperCase(Trim(Filename));
>  Result := 0;
>  ListSize := SizeOf(PIDs);
>  NoOfProcesses := 0;
>  FillChar(PIDs, ListSize, 0);
>  If not EnumProcesses(@PIDs, ListSize, NoOfProcesses)
>    then RaiseLastWin32Error;
>  NoOfProcesses := NoOfProcesses div SizeOf(DWord);
>  For i := 0 to NoOfProcesses-1 do begin
>    Handle := OpenProcess(PROCESS_QUERY_INFORMATION + PROCESS_VM_READ +
>PROCESS_TERMINATE, False, PIDs[i]);
>    If Handle = 0 then begin
>      CloseHandle(Handle); file://##could be a mistake
>      Continue; file://cannot open that process, guess I do not have the
>necessary rights
>    end;
>    FillChar(szName, SizeOf(szName), 0);
>    If GetModuleBaseName(Handle, 0, @szName, sizeof (szName)) > 0 then begin
>      GetModuleFileNameEx(Handle, 0, @Fullname, sizeof(Fullname));
>      Exename := AnsiUpperCase(Trim(StrPas(FullName)));
>      If Exename = Filename then begin
>        Result := Handle;
>        Break;
>      end else CloseHandle(Handle);
>    end else CloseHandle(Handle);
>  end;
>End;

>{---------------------------------------------------------------------------
>---}
>Function GetProcessHandleFromExe(Filename : String) : DWORD;
>Var Snap  : Integer;
>    Proc  : TProcessEntry32;
>    Exename : String;
>Begin
>  Filename := AnsiUpperCase(Trim(Filename));
>  Result := 0;
>  Snap := CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
>  try
>    Proc.dwSize := SizeOf(Proc);
>    if(Process32First(Snap, Proc))then
>    repeat
>      Exename := StrPas(Proc.szExeFile);
>      If AnsiUpperCase(Trim(Exename)) = Filename then begin
>        Result := OpenProcess(PROCESS_TERMINATE, True, Proc.th32ProcessID);
>        Break;
>      end;
>    until not(Process32Next(Snap,Proc));
>  finally
>    Windows.CloseHandle(Snap);
>  end;
>End;

>HTH,
>hannes
>--
>http://www.talknet.de/~hannes.breuer/
><P...@antionline.com> schrieb im Newsbeitrag
>news:hDKEnBA7Ote5QwQL@btinternet.com...
>> Hi
>> I'm inexperienced with Delphi(2) and I have had a problem (2) with my
>> latest project. Basically I am writing a configuration GUI for a
>> windowless application (No forms).

>> My first problem is killing a process. I know how to kill a window but I
>> don't know how to kill a 'windowless' process.

>> My second problem is saving the contents of an edit. I have been using
>> 'lines.add' to put the contents of the edits into a memo and saving it
>> but when I use 'edit1.text := (Memo1.Lines[0])' to get the lines back I
>> only get individual characters.

>> Has anyone used the Talisman shell replacement with Delphi?

>> TiA
>> Paul

Other Threads