Recent

Author Topic: Running external program (*.exe file) problem.  (Read 366 times)

TomTom

  • Full Member
  • ***
  • Posts: 170
Running external program (*.exe file) problem.
« on: November 29, 2022, 01:56:44 pm »
I have Edit1.text:='C:\Cytadela\cytadela.exe'; I can run this program (game) on my system without any problem just by double clicking on it.
But I have something like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var
  3.   gametitle, lasttimeplayed, timesplayed: String;
  4.   b: LongInt;
  5.   I: Integer;
  6. begin
  7.       GameProcess := TProcess.Create(nil);      
  8.       GameProcess.Executable:=exepath.text; //exePath == TEdit
  9.       GameProcess.Execute;
  10. end;
  11.  

Screen resolutions changes and goes black and app is being closed. When I run this game by clicking on exe it runs just fine but in a window. What can be the cause of that?

Thaddy

  • Hero Member
  • *****
  • Posts: 12629
Re: Running external program (*.exe file) problem.
« Reply #1 on: November 29, 2022, 02:04:31 pm »
On Windows, Lazarus has by default no immediate console support, because a GUI application does not usually need it.
If you define {$apptype console} at the top of the lpr file it probably works. Note it takes some more work to hide the console window until it is needed and hide it again after the external program is finished.
But actually that is quite easy.
"Looks" OK, doesn't mean it is IS OK.

KodeZwerg

  • Hero Member
  • *****
  • Posts: 954
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Running external program (*.exe file) problem.
« Reply #2 on: November 29, 2022, 02:09:34 pm »
What can be the cause of that?
You probably free it too early.

I suggest to write it like this:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var
  3.   AProcess: TProcess;
  4. begin
  5.   if (not FileExists(exepath.text)) then
  6.     begin
  7.       ShowMessage('Not found!');
  8.       Exit;
  9.     end;
  10.   AProcess := TProcess.Create(nil);
  11.   AProcess.Executable:= exepath.text;
  12.   AProcess.Parameters.Add('');
  13.   AProcess.Options := AProcess.Options + [poWaitOnExit];
  14.   AProcess.Execute;
  15.   AProcess.Free;  
  16. end.
  17.  
Please test and tell if there is still something wrong.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: Running external program (*.exe file) problem.
« Reply #3 on: November 29, 2022, 02:11:04 pm »
I simplified the code in my post. I did it like You wrote. There is no difference. For example other apps run without problem... May it be because the game is in SDL ?
I also tried ShellExecute but effect is the same in this case.

I just want the app to start just like I would click on it in windows explorer. I don't care if there is console or not.
« Last Edit: November 29, 2022, 02:14:57 pm by TomTom »

KodeZwerg

  • Hero Member
  • *****
  • Posts: 954
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Running external program (*.exe file) problem.
« Reply #4 on: November 29, 2022, 02:13:29 pm »
I simplified the code in my post. I did it like You wrote. There is no difference. For example other apps run without problem... May it be because the game is in SDL ?

I just want the app to start just like I would click on it in windows explorer. I don't care if there is console or not.
Do you have "AProcess.Options := AProcess.Options + [poWaitOnExit];" added, yes?
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: Running external program (*.exe file) problem.
« Reply #5 on: November 29, 2022, 02:16:48 pm »
Yes I had all that. I can run that way for example Blender (3d app), PowderToy (sand game), Godot Engine ... But not this one (Cytadela is opensource port of old Amiga game, FPS game).

I simplified the code in my post. I did it like You wrote. There is no difference. For example other apps run without problem... May it be because the game is in SDL ?

I just want the app to start just like I would click on it in windows explorer. I don't care if there is console or not.
Do you have "AProcess.Options := AProcess.Options + [poWaitOnExit];" added, yes?

loaded

  • Hero Member
  • *****
  • Posts: 676
Re: Running external program (*.exe file) problem.
« Reply #6 on: November 29, 2022, 02:19:45 pm »
you can try it this way.
Code: Pascal  [Select][+][-]
  1. uses shellapi,LConvEncoding;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. begin
  5.   ShellExecute(0,nil, PChar(UTF8ToCP1254('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')),nil,nil,1);
  6. end;  
« Last Edit: November 29, 2022, 02:22:13 pm by loaded »
If Ide=Lazarus 2.2.4 32 Bit and Os=Win 10 Home 64 Bit 22H2 then Get up and do something useful! Because God is the helper of those who start again;

KodeZwerg

  • Hero Member
  • *****
  • Posts: 954
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Running external program (*.exe file) problem.
« Reply #7 on: November 29, 2022, 02:22:36 pm »
Lol, I just wanted to write something similar, if you want things are doing like Windows Explorer is doing, than use same functions  ;)

Actually I do not know why your app is closing right after you opened, to me your code look incomplete from point of what I do see.
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: Running external program (*.exe file) problem.
« Reply #8 on: November 29, 2022, 02:27:36 pm »
This works the same way. I think its something about that game. Every other app run just fine with both methods (TProcess and ShellExecute).
It looks like when I run the game from my app the game starts in full screen. But when I run it from windows explorer it starts in a window.

you can try it this way.
Code: Pascal  [Select][+][-]
  1. uses shellapi,LConvEncoding;
  2.  
  3. procedure TForm1.Button1Click(Sender: TObject);
  4. begin
  5.   ShellExecute(0,nil, PChar(UTF8ToCP1254('C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')),nil,nil,1);
  6. end;  

loaded

  • Hero Member
  • *****
  • Posts: 676
Re: Running external program (*.exe file) problem.
« Reply #9 on: November 29, 2022, 02:40:57 pm »
There may be parameters in the shortcut of the exe you are trying to run. You can see this in the shortcut properties. Can you check this situation?
I have prepared a sample parameter for you.
If Ide=Lazarus 2.2.4 32 Bit and Os=Win 10 Home 64 Bit 22H2 then Get up and do something useful! Because God is the helper of those who start again;

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: Running external program (*.exe file) problem.
« Reply #10 on: November 29, 2022, 02:53:40 pm »
There are no parameters. It's not a shortcut.
There may be parameters in the shortcut of the exe you are trying to run. You can see this in the shortcut properties. Can you check this situation?
I have prepared a sample parameter for you.

loaded

  • Hero Member
  • *****
  • Posts: 676
Re: Running external program (*.exe file) problem.
« Reply #11 on: November 29, 2022, 02:58:51 pm »
Then my last trump card  ;D

Code: Pascal  [Select][+][-]
  1. uses shellapi,LConvEncoding;
  2. procedure TForm1.Button1Click(Sender: TObject);
  3. var
  4. exe_path:String='C:\Program Files (x86)\Google\Chrome\Application\chrome.exe';
  5. begin
  6.   ShellExecute(Application.handle, 'open', 'cmd.exe', PChar('/c "'+exe_path+'"' ), nil, 1);
  7. end;  
If Ide=Lazarus 2.2.4 32 Bit and Os=Win 10 Home 64 Bit 22H2 then Get up and do something useful! Because God is the helper of those who start again;

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: Running external program (*.exe file) problem.
« Reply #12 on: November 29, 2022, 03:10:26 pm »
I found solution... Gameprocess.options := GameProcess.Options + [poDetached]... so it's not a child process of my app... I guess ;)

KodeZwerg

  • Hero Member
  • *****
  • Posts: 954
  • Fifty shades of code.
    • Delphi & FreePascal
Re: Running external program (*.exe file) problem.
« Reply #13 on: November 29, 2022, 03:21:52 pm »
Windows Explorer way of doing works too.
Code: Pascal  [Select][+][-]
  1. uses
  2.   ... Windows, ShellApi;    
  3.  
  4.  
  5. procedure TForm1.Button1Click(Sender: TObject);
  6. var
  7.   LTarget: string;
  8. begin
  9.   LTarget := 'c:\Games\cytadela_1.1.0_win32\cytadela.exe';
  10.   if (not FileExists(LTarget)) then
  11.     begin
  12.       ShowMessage('Not found!');
  13.       Exit;
  14.     end;
  15.   ShellExecute(0, 'open', PChar(LTarget), PChar(''), PChar(ExtractFilePath(LTarget)), SW_SHOWNORMAL);
  16. end;
I think it is because your app is not in the folder (Working folder).
« Last Edit: Tomorrow at 31:76:97 by KodeZwerg »

TomTom

  • Full Member
  • ***
  • Posts: 170
Re: Running external program (*.exe file) problem.
« Reply #14 on: November 29, 2022, 03:29:44 pm »
I'm checking if file exists before user is able to click the button and my Procedure looks like this and it's working fine :) :
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button3Click(Sender: TObject);
  2. var
  3.   i: Integer;
  4. begin
  5.  
  6.   GameProcess := TProcess.Create(nil);
  7.   try
  8.     GameProcess.InheritHandles := False;
  9.     GameProcess.Options := [poDetached];
  10.     GameProcess.ShowWindow := swoShow;
  11.     GameProcess.CurrentDirectory := ExtractFilePath(exepath.text);
  12.     for i := 1 to GetEnvironmentVariableCount do GameProcess.Environment.Add(GetEnvironmentString(i));
  13.     GameProcess.Executable:=exepath.text;
  14.     GameProcess.Execute;
  15.   finally
  16.     GameProcess.Free;
  17.   end;
  18.  

 

TinyPortal © 2005-2018