c++windowsshellexecute

ShellExecuteA cannot find file


I'm trying to open a file with a c++ program using ShellExecuteA in Windows10. (I'm also using VisualStudio2019 in case that's relevant.)
ShellExecute itself is working (I can use "explore" and "find" as it is intended), however it seems to be unable to find the file even though it exists in the directory. I have tried both an absolute as well as a relative path and neither work.
My code is this:

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

using namespace std;

string getCurrentDir() {
    char buff[MAX_PATH];
    GetModuleFileName(NULL, buff, MAX_PATH);
    string::size_type position = string(buff).find_last_of("\\/");
    return string(buff).substr(0, position);
}


int main()
{
    cout << "path: " << getCurrentDir() << endl;
    int ret1 = (int)ShellExecuteA(NULL, "open", "C:\\Users\\Sasha\\source\\repos\\shellopen\\Debug\\MyTextFile.txt", NULL, NULL, SW_SHOWNORMAL);
    cout << ret1 << endl;
    int ret2 = (int)ShellExecuteA(NULL, "open", "MyTextFile.txt", NULL, NULL, SW_SHOWNORMAL);
    cout << ret2 << endl;

    return 0;
}

The result is

path: C:\Users\Sasha\source\repos\shellopen\Debug
2
2

"2" apparently means that the file couldn't be found, however "MyTextFile.txt" definitely exists in the directory and there is no spelling mistake.
I've tried googling the problem but it seems to be uncommon enough that I haven't found anything that works for me. I'd be very grateful for any help.


Solution

  • Ok, it turned out it was a spelling mistake, causing my file to be named "MyTextFile.txt.txt"
    Thank you to everyone who tried to help me find the answer.