Search code examples
c++windowscodeblockssfmlshellexecute

Can not open .exe with shellExecute function in C++


i want to open a .exe (lets call it A) when i execute another c++ program (lets call it B), so i have A and B in execution at the same time.

For this, i use this function in B code:

HINSTANCE result;

result=ShellExecute(NULL,NULL,"d:\\Users\\bci\\Desktop\\Animation\\bin\\Release\\Animation2.exe",NULL ,NULL,SW_SHOWDEFAULT);

cout << "Result is " <<(int)result <<endl;

Result value is 42, and A does not manage to open. The error i get is this:

Failed to open file "player.png": Unable to open file
Failed to load player spritesheet!

This corresponds to this part of A´s code:

// load texture (spritesheet)
    sf::Texture texture;
    if (!texture.loadFromFile("player.png"))
    {
        std::cout << "Failed to load player spritesheet!" << std::endl;
        return 1;
    }

A and B were both built using code::blocks as IDE with the GCC/G++ compiler and GDB debugger from TDM-GCC (version 4.9.2, 32 bit, SJLJ).

I am doing this in windows 7 64 bits (enterprise and professional, both same error).

Also, i do not know if it is worth telling, but i am using SFML library in A, as you can see in

 sf::Texture texture;

I think this is related somehow to permissions for opening a file from a program you are calling, but i am not sure how to solve this.

Actually A and B work perfectly on their own, but B is unable to open A by this meanings.

I am pretty sure i have made that image as unrestricted as i can with the right click->properties->security options, but it does not seem enough.

Can someone help me with this?


Solution

  • ShellExecute has an lpDirectory parameter that sets the shell's current working directory to whatever you want for the duration of the call.

    You've set it to NULL, so the current working directory is unchanged (could be the location of B's executable, or whatever else you made it). Program A is using relative paths and therefore expects player.png to be in the current working directory; presumably the distribution makes it so that the file is actually found at the location of A's executable, and just assumes the two are the same. In this scenario, they're not.

    Only you can know what the current working directory is supposed to be for A, but based on the above assumption, you should set lpDirectory to "d:\\Users\\bci\\Desktop\\Animation\\bin\\Release\\".