-3

Basically I have a console application that opens another .exe. That console application works properly when I normally double click on it.

I added the application in regedit: Computer\HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run so that it automatically opens with Windows and that .exe is ran automatically.

I don't have an idea on how to fix this.

#include <windows.h>
#include <shellapi.h>
#include <iostream>

using namespace std;

int main()
{
    cout << "Test...\n";
    Sleep(500);
    cout << "Test..\n";
    ShellExecuteA(NULL, "open", "Manager.exe", NULL, NULL, SW_SHOWNORMAL);
    cout << "Test....\n";
    Sleep(500);
    return 0;
}

The thing is that the console opens when Windows starts up, but does not open the .exe file, just basically opens and closes. It's like bypasses the "ShellExecuteA"... line, displaying text on console and Sleep(...) works.

NOTE: Keep in mind that, as said above, it WORKS PROPERLY when I manually open this application, the "Manager.exe" opens. BUT I it doesn't work when this code is automatically opened with Windows. Any help?

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
petxd
  • 27
  • 7
  • 3
    Your program might not be executed with the correct working directory. Check [this answer](https://stackoverflow.com/questions/2822951/use-registry-to-startup-a-program-and-also-change-the-current-working-directory) for more information. – Gilles-Philippe Paillé May 21 '19 at 17:28
  • I don't have to do that since that console application it's just one file and doesn't depend on others. – petxd May 21 '19 at 17:31
  • 1
    In which directory is `Manager.exe` located? This executable is a dependency and it needs to be able to locate it properly for your program to work. – Gilles-Philippe Paillé May 21 '19 at 17:32
  • Both Manager.exe and the console app that uses the code(named test.exe) above are on desktop. In regedit I have a string value with path: "C:\Users\ciuca\Desktop\test.exe". – petxd May 21 '19 at 17:33
  • 1
    It looks like this is your problem. When Windows run a program at startup, the working directory is not the directory of the executed program ([look at the second answer here](https://social.msdn.microsoft.com/Forums/sqlserver/en-US/ba590643-528b-44a5-b379-8a1e3e4250d0/windows-launches-my-app-in-system32-on-windows-startup?forum=csharpgeneral)). When running the shell command, Windows will look for `Manager.exe` in the working directory (which is not the Desktop) and in the directories provided in environment variables. Thus, it will not find `Manager.exe` and will not execute it. – Gilles-Philippe Paillé May 21 '19 at 17:37
  • Alright, thanks for the tips, I will try to fix this, I hope it will work. – petxd May 21 '19 at 17:38
  • No problem, I hope it solves your problem. If not, come back here and we'll help you. And when you find the solution, please put it as an answer so the question can be marked as resolved. – Gilles-Philippe Paillé May 21 '19 at 17:47
  • This another example of why it is important to check for errors. As described for the documentation of ShellExecute you should in fact use ShellExecuteEx because it reports errors properly. And then you need to check for errors. If you'd done so then you'd have seen what the problem was. Saying all that, to create a process, CreateProcess is the correct api to use. Again, when you use is, make sure you include error checking code. – David Heffernan May 22 '19 at 06:14

1 Answers1

3

The working directory of applications executed by the Run key is an implementation detail and you are not specifying a path to Manager.exe so ShellExecuteA can't find the file.

Specify a full path to Manager.exe. You can call GetModuleFileName(NULL, ...) if you need the path of your console application.

Windows is not bypassing your call to ShellExecuteA, it is probably failing with a not found error but you don't know because you are not checking the return value!

Anders
  • 97,548
  • 12
  • 110
  • 164