2

This is not a problem, more a good to know topic. I can life without an solution but it was nice to have an Solution.

When i run my .exe with normal privileges i got the correct path where the .exe is saved. When i run the .exe with highest privileges the returned path is "C:\Windows\System32". The correct location is i.e. "C:\Msys64\home\testproject\".

Picture from Taskscheduler with setting highest privileges

My code to get the path uses the Windows command "cd":

bool get_path(Arg* arg)
{
    //This function will only return the correct path when not run with highest privilegs over Taskscheduler
    FILE* fp = _popen("cd", "r");
    arg->workingdir_cnt = 0;
    while (!(feof(fp))) 
    { 
        arg->workingdir[arg->workingdir_cnt++] = fgetc(fp); 
        if (arg->workingdir_cnt > sizeof(arg->workingdir)) { fclose(fp); return 0; }
    }
    _pclose(fp);

    //Add backslash for future use
    arg->workingdir[arg->workingdir_cnt-2] = '\\';
    arg->workingdir[arg->workingdir_cnt-1] = '\0';

    return 1;
}
Gabriel Staples
  • 36,492
  • 15
  • 194
  • 265
Erz
  • 87
  • 7
  • 3
    It sounds like you are not interested in the current working directory, but rather in the directory where the executable is located (the two are generally unrelated). You can obtain the running EXE's file name with `GetModuleFileName(NULL)` – Igor Tandetnik May 07 '23 at 21:32
  • 1
    And if you do want current working directory, there are easier ways than shelling out to `cd` command. See `GetCurrentDirectory` (Windows-specific) or `_getcwd` (somewhat more portable). – Igor Tandetnik May 07 '23 at 21:35
  • Thats correct. I wan´t to know where is the .exe saved. In example above i expect the return to path "C:\Msys64\home\testproject". I will try your Solution. In MicrosoftWeb it looks like good. – Erz May 07 '23 at 21:37
  • 1
    "Current working directory" and "directory where EXE is located" are in general two different directories. If they happen to be the same, it's only by coincidence. Current working directory can be changed at any time (see e.g. `SetCurrentDirectory`). I showed you how to get both; you just need to figure out which one you actually want. – Igor Tandetnik May 07 '23 at 21:39
  • Use the standard library's `std::filesystem::current_path()`. You have tagged the question with [tag:c++], hence my answer is eligible. Seriously, use `GetModuleFileName()` – 273K May 07 '23 at 21:39
  • `while (!(feof(fp)))`??? You need to read [**Why is “while( !feof(file) )” always wrong?**](https://stackoverflow.com/questions/5431941/why-is-while-feoffile-always-wrong) – Andrew Henle May 07 '23 at 23:14
  • @273K he tagged it c. – Andrew Truckle May 08 '23 at 07:17
  • 1
    @AndrewTruckle Look at the edit history. – 273K May 08 '23 at 07:55

1 Answers1

2

GetModuleFileNameA() works for me. It output in every execute method the same Path (where the .exe is saved). Thanks @Igor Tandetnik

Erz
  • 87
  • 7