Author Topic: cannot run .exe file from File Explorer  (Read 507 times)

Offline TageB

  • Single posting newcomer
  • *
  • Posts: 6
cannot run .exe file from File Explorer
« 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?

Offline stahta01

  • Lives here!
  • ****
  • Posts: 6198
    • My Best Post
Re: cannot run .exe file from File Explorer
« Reply #1 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

Tim S.
« Last Edit: December 15, 2017, 03:10:50 pm by stahta01 »
C Programmer working to learn more about C++ and Git.
On Windows 7 64 bit and Windows 10 32 bit.
On Debian Testing, compiling CB Trunk against wxWidgets 3.0.2.
--
When in doubt, read the CB WiKi FAQ. http://wiki.codeblocks.org

Offline TageB

  • Single posting newcomer
  • *
  • Posts: 6
Re: cannot run .exe file from File Explorer
« Reply #2 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.

Offline sodev

  • Almost regular
  • **
  • Posts: 141
Re: cannot run .exe file from File Explorer
« Reply #3 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.

Offline TageB

  • Single posting newcomer
  • *
  • Posts: 6
Re: cannot run .exe file from File Explorer
« Reply #4 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.

Offline TageB

  • Single posting newcomer
  • *
  • Posts: 6
Re: cannot run .exe file from File Explorer
« Reply #5 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.

Offline TageB

  • Single posting newcomer
  • *
  • Posts: 6
Re: cannot run .exe file from File Explorer
« Reply #6 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...

Offline sodev

  • Almost regular
  • **
  • Posts: 141
Re: cannot run .exe file from File Explorer
« Reply #7 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: [Select]
@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: [Select]
@echo off
set APPLICATION=prog1.exe

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

"%BASEDIR%%APPLICATION%"
pause

Offline TageB

  • Single posting newcomer
  • *
  • Posts: 6
Re: cannot run .exe file from File Explorer
« Reply #8 on: December 15, 2017, 09:07:56 pm »
Thank you