0

I created this code which allows the user to open file such as notepad++, excel, etc'. And I want to check if he opened a file that exists. If not just give an error message. If it is a file print "it works" or something like that.

The answer that you gave me is completely unrelated, it's about something else with a different function, it's too general.

Iv'e tried to check if the file is true but it doesn't work.

    if (qorn == "notreal" || qorn == "4") {

            if (fileopen("ex") == true) { // all ways true ex is not a real file
                cout << "The file is good";
            }
            else {
                cout << "you don't have this file";
            }
        }

    bool fileopen(string url) {
    bool f = false;
    // .exe etc
    // what to do with the file - open is default
    // what is the name of the file you want to do something with
     ShellExecuteA(NULL,NULL/*null- open it as a deffualt*/, url.c_str(), NULL, NULL, SW_SHOWNORMAL);
     if (ShellExecuteA(NULL, NULL, url.c_str(), NULL, NULL, SW_SHOWNORMAL)) {
         f = true;
     }
     return f;
}



> You adviced me to this this as well which returns 000002 at the end if
> i print it

HINSTANCE fileopen(string url) {
    HINSTANCE num = 0;
    // .exe etc
    // what to do with the file - open is deffualt
    // what is the name of the file you want to do wsomething with
     num =ShellExecuteA(NULL,NULL/*null- open it as a deffualt*/, url.c_str(), NULL, NULL, SW_SHOWNORMAL);
     return num;

}
mdnfiras
  • 882
  • 1
  • 15
  • 28
  • Possible duplicate of [Fastest way to check if a file exist using standard C++/C++11/C?](https://stackoverflow.com/questions/12774207/fastest-way-to-check-if-a-file-exist-using-standard-c-c11-c) – MasterAler Jul 16 '19 at 21:30
  • Note: The return value from `ShellExecuteA` is a bit more complicated than you've provided code to handle. I recommend a read through [the documentation](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea). You're pretty much always going to get a value that equates to true. – user4581301 Jul 16 '19 at 21:33
  • Smurf, that's not unrelated at all. That's pretty much the whole bug. – user4581301 Jul 16 '19 at 21:35
  • @user4581301 I know that it will always be true –  Jul 16 '19 at 21:36
  • @user4581301 what do you suggest that i'd do? –  Jul 16 '19 at 21:38
  • Easiest way to check if a file exists is to try to open it anyway and handle any error that comes out. – Etienne de Martel Jul 16 '19 at 21:42
  • I suggest you do what it says to do in [the documentation](https://learn.microsoft.com/en-us/windows/win32/api/shellapi/nf-shellapi-shellexecutea): Return true only if `ShellExecuteA`'s return value is greater than 32. – user4581301 Jul 16 '19 at 21:43
  • Note that this only means the program cannot be run, not that it doesn't exist. – user4581301 Jul 16 '19 at 21:44
  • @user4581301 I understand but how can i compare it to 32 i didn't understand that –  Jul 16 '19 at 21:50
  • How would you normally test if a number is greater than 32? – user4581301 Jul 16 '19 at 22:00
  • do an if statement which in this case doesn't work when i return it it returns 0000002: look at the code above –  Jul 16 '19 at 22:05
  • @EtiennedeMartel How do I do it though –  Jul 16 '19 at 22:22
  • @user4581301 why would you - rep me if you are wrong –  Jul 16 '19 at 22:37
  • The answers here work for you: https://stackoverflow.com/questions/12774207/fastest-way-to-check-if-a-file-exist-using-standard-c-c11-c – DanyAlejandro Jul 16 '19 at 23:16

1 Answers1

0

As the doc says, compare the return value to 32 and display an error message otherwise.

(and you tagged only C++, but it is also Windows/Winapi as you use ShellExecute

Like this (always Unicode on Windows) =>

(if you replace Excel by a non-existant file, it will return 2 (File Not Found))

                int hInst = (int)ShellExecute(NULL, NULL, L"Excel", NULL, NULL, SW_SHOWNORMAL);
                if (hInst <= 32)
                {
                    LPVOID lpMsgBuf = NULL;
                    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, hInst /* = GetLastError() */, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPWSTR)&lpMsgBuf, 0, NULL);
                    if (lpMsgBuf != NULL)
                    {
                        MessageBox(NULL, (LPCTSTR)lpMsgBuf, L"ShellExecute Error", MB_OK | MB_ICONSTOP);
                        LocalFree(lpMsgBuf);
                    }
                }
Castorix
  • 1,465
  • 1
  • 9
  • 11
  • Thank you so much! What are these messeges are though? I am using console are they sopust to show up? Because it doesn't I just printed it to the cmd file exist or file doesn't exist –  Jul 17 '19 at 12:39
  • I used MessageBox for the sample, but for a console app, you can display the message with _wcout_, like : `#include #include _setmode(_fileno(stdout), _O_U16TEXT); wcout << (LPCTSTR)lpMsgBuf << endl;` – Castorix Jul 17 '19 at 13:16
  • Thank and I have one more qustion if you are willing to answer: So I can open execl notepad etc but why can't I open for example minecraft? –  Jul 17 '19 at 14:02
  • If you don't set the full path, apps must be in the PATH, System directories, **App Paths** key in registry See [Finding an Application Executable](https://learn.microsoft.com/en-us/windows/win32/shell/app-registration) – Castorix Jul 17 '19 at 14:24
  • @Caseorix can't I just do c:/ and it'd do all the apps and if not how can i do all the apps in my computer? –  Jul 17 '19 at 15:01