Thread: Difference between running a program from the EXE and from visual studio

  1. #1
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485

    Difference between running a program from the EXE and from visual studio

    Hallo,

    Why is there a difference between pressing the .EXE file created by visual studio and running the file from visual studio itself? When I press the .exe file the program crashes, but when I press the exe file in the folder the program crashes.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    There shouldn't be much of a difference at all; at most, I think, VS will add a little bit of code to make sure the command window stays open. Any run-time error will happen either way.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    That doesn't sound like a difference.

    The only difference between launching from VS and launching from the command line or the explorer is the working directory. VS will give you the project root as the working directory, while direct execution gives you the program's own directory. If your program does not correctly check results of file open operations, it may crash as a result of not being able to open a file.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #4
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    So if I run it from visual studio I can do this:
    Code:
    Openfile("Data\house.jpg);
    while if I run it from a folder I have to do:
    Code:
    Openfile("C:\programFiles\VS\myProject\Data\house.jpg")
    Also, when I run an other project I get an vector error, but it works fine if I press F5 in visual studio

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    It's a bug that happens when certain conditions are met, which aren't met when running it via Visual Studio.
    You need to re-create those conditions. For example, the path.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by h3ro View Post
    So if I run it from visual studio I can do this:
    Code:
    Openfile("Data\house.jpg);
    while if I run it from a folder I have to do:
    Code:
    Openfile("C:\programFiles\VS\myProject\Data\house.jpg")
    Also, when I run an other project I get an vector error, but it works fine if I press F5 in visual studio
    Within a string literal, the backslashes should generally be escaped; i.e.
    Code:
    Openfile("C:\\programFiles\\VS\\myProject\\Data\\house.jpg")
    (I simply prefer forward slashes.)

    Are you checking the return value of Openfile?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Within a string literal, the backslashes should generally be escaped; i.e.
    Code:

    Openfile("C:\\programFiles\\VS\\myProject\\Data\\h ouse.jpg")

    (I simply prefer forward slashes.)

    Are you checking the return value of Openfile?
    I know, but I typed the openfile() function while I was making the post and forgot about it. In my program I use an other function, where I use \\ and it returns a bool.

    Thanks for the replies so far, but I still dont fully understand why pressing the .exe causes the program to fail.

  8. #8
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Well, have you told us how it fails yet?
    Mainframe assembler programmer by trade. C coder when I can.

  9. #9
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    I have tried, but it might not be that easy to find between all my other random writing, sorry about that.

    I have two different .exe files (two versions of the same project) which work when I run them through visual studio, but fails when I press the .exe file in the folder.

    The first .exe file brings up the "this program has preformed an illegal operation and will now be closed window". No other information is given.

    The second .exe file brings up an error that there is an vector out of range in my program.

    The weird thing is that they both work fine when running from Visual studio.

    Some additional info about the project:
    * I am reading from several different files, all located inside the project folder.
    * I am linking to static libraries

  10. #10
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Command line arguments?

    Are you sure you are running the same executable (e.g. you may be building a debug version, and then running the release version, or other way around - I've made that mistake more than once!)

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  11. #11
    Registered User
    Join Date
    Oct 2006
    Location
    UK/Norway
    Posts
    485
    Command line arguments?
    Im not at home now, so im not sure, but i assume its what ever is default as I have not changed anything apart from linking to one libary.

    Are you sure you are running the same executable (e.g. you may be building a debug version, and then running the release version, or other way around - I've made that mistake more than once!)
    It happens in both debug and release.

    If I go back to a really really old version of the project I can run it from the .exe file, so it has to be something to do with my code. I just dont understand why and how

Popular pages Recent additions subscribe to a feed