Code::Blocks Forums

User forums => Help => Topic started by: TageB on December 15, 2017, 02:06:51 pm

Title: cannot run .exe file from File Explorer
Post by: TageB on December 15, 2017, 02:06:51 pm
Windows 10

CodeBlocks installed under D:\CodeBlocks\
path added:  D:\CodeBlocks\MinGW\bin
Simple printout program is saved under D:\C-lib\progr1

Executes in CodeBlocks and in PC Shell ./prog1, but when trying to execute it in File Explorer (as administrator) a window pops up and closes immediately.
I have all permissions to the prog1.exe file. Virus program has also been turned off.

Why can't I run my prog1.exe file from File Explorer?
Title: Re: cannot run .exe file from File Explorer
Post by: stahta01 on December 15, 2017, 02:54:28 pm
Try running it from an cmd.exe prompt.
Does it work?

Edit: Added link to fix the most common user mistake https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1042856625&id=1043284385 (https://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1042856625&id=1043284385)

Tim S.
Title: Re: cannot run .exe file from File Explorer
Post by: TageB on December 15, 2017, 06:18:24 pm
It works in Code::Blocks, PowerShell (sorry I wrote PC Shell) and it works in cmd.exe.

I could not find any help in you link. I have installed the latest Code::Blocks and everything seems to work, except the built and compiled exe file, executed from File Explorer.
Title: Re: cannot run .exe file from File Explorer
Post by: sodev on December 15, 2017, 06:54:44 pm
You have build a console application that does some output and terminates then on its own. What explorer does is open a shell, run the program, and close the shell again after the program terminates. So what you are seeing is absolut normal and correct behavior.

Don't like it? Either prevent your application from terminating itself or write a simple wrapper shellscript that starts your program and pause's after it. Or simply start it manually in a shell like you already did.
Title: Re: cannot run .exe file from File Explorer
Post by: TageB on December 15, 2017, 06:56:06 pm
I got something from Tim's link. Thank you.

If I add 'getchar()' before the 'return 0' the window will stay open, showing the result, until I press enter.
But then I have to use 'enter' to quit the program, which wasn't the case when running it before in terminal.

So I would like a solution that shows the result and end the program, as in terminal.
Title: Re: cannot run .exe file from File Explorer
Post by: TageB on December 15, 2017, 07:02:25 pm
OK sodev,
I was writing a reply when your answer arrived so I didn't see it.
As I understand you, there is no way to run the program, keeping the result window open, and quitting the program from File Explorer.
Title: Re: cannot run .exe file from File Explorer
Post by: TageB on December 15, 2017, 07:17:35 pm
sodev,
Could you or someone else give med give me an example of a shell script I could run from File Explorer.
Suppose it should have the exe file as an argument, open a shell, running the exe file from shell, and then the program ends and the shell stays open? I am not used to good shell scripting...
Title: Re: cannot run .exe file from File Explorer
Post by: sodev on December 15, 2017, 08:39:31 pm
Well, a very basic solution with hardcoded application name that requires the application to be in the current working directory (which explorer sets to the directory of the script) would be like this:

launch1.cmd
Code
@echo off
prog1.exe
pause

The premium version with hardcoded default application name and optional specified as command line parameter and the possibility to launch with a random working directory (but the application must be in the same directory of the script) would be like this:

launch2.cmd
Code
@echo off
set APPLICATION=prog1.exe

set BASEDIR=%~dp0
if not "x%1" == "x" set APPLICATION=%1

"%BASEDIR%%APPLICATION%"
pause
Title: Re: cannot run .exe file from File Explorer
Post by: TageB on December 15, 2017, 09:07:56 pm
Thank you