How can I use ShellExecute() with if and else commands?

Discussion in 'C++' started by Panarchy, May 1, 2009.

  1. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Hello

    Very simple problem I have here.

    If network share is found, open it in explorer. Else open LocalFolder1. If LocalFolder1 isn't found, open LocalFolder2.

    Current code;

    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <iostream>
    
    int APIENTRY WinMain(HINSTANCE hinstance, HINSTANCE hprevious, LPSTR cmdline, int cmdshow) {
    
    	ShellExecute(NULL, "explore", "\\\\panarchy\\share", NULL, NULL, SW_SHOWNORMAL);
    
    	return 0;
    }
    Please tell me how to use Programming Controllers within the above code.

    Thanks in advance,

    Panarchy
     
    Last edited: May 1, 2009
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    ShellExecute should return something based on the data opened successfully or not found kind of thing
     
  3. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Oh?

    Could you please tell me how I can set it up to do so?

    Thanks in advance,

    Panarchy
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Refer this

    Example
    Code:
    if(ShellExecute('Your Sample Params Above') == ERROR_PATH_NOT_FOUND)
    {
      Do something
    }
    else
    {
      Do some other thing
    }
     
  5. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for replying.

    Do you think the following code would work?;

    Code:
    if(ShellExecute(NULL, "explore", "\\\\panarchy\\share", NULL, NULL, SW_SHOWNORMAL); == ERROR_PATH_NOT_FOUND)
    {
      ShellExecute(NULL, "explore", "C:\\Backup\sony\\", NULL, NULL, SW_SHOWNORMAL);
    }
    else
    {
      ShellExecute(NULL, "explore", "D:\\Backup\\", NULL, NULL, SW_SHOWNORMAL);
    }
    Tell me if you think it would work.

    Thanks in advance,

    Panarchy

    PS: The reason I can't test it myself is because I don't have any shares on my network.

    EDIT: Just tried it, did not work. Here is my complete log file;

     
    Last edited: May 1, 2009
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Run and see if it works because at times return value is not always TRUE / FALSE which needs to be tested.
     
  7. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Hello

    Still not working for me. Have simplified it as much as I can, here is my complete code;
    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <iostream>
    
    int APIENTRY WinMain(HINSTANCE hinstance, HINSTANCE hprevious, LPSTR cmdline, int cmdshow) {
    
    {
      ShellExecute(NULL, "explore", "C:\\Backup\\sony\\", NULL, NULL, SW_SHOWNORMAL);
    }
    else
    {
      ShellExecute(NULL, "explore", "D:\\Backup\\", NULL, NULL, SW_SHOWNORMAL);
    }
    
    	return 0;
    }
    Please tell me how to make it work, as it is still giving me the following errors (can't compile the program cause of them);

    Please tell me how to get the program to work!

    Thanks in advance,

    Panarchy
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    There are lots of error of basic programming and not related to shell execute and I would try to point some of them

    Code:
    if(ShellExecute(NULL, "explore", "\\\\panarchy\\share", NULL, NULL, SW_SHOWNORMAL); == ERROR_PATH_NOT_FOUND)
    In if statement we should not have a ;

    In the last post you have else without an if.
     
  9. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Thanks for replying.
    However, I still don't understand.

    Here is my latest code;
    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <iostream>
    
    int APIENTRY WinMain(HINSTANCE hinstance, HINSTANCE hprevious, LPSTR cmdline, int cmdshow) {
    
    
    if(ShellExecute(NULL, "explore", "\\\\panarchy\\share", NULL, NULL, SW_SHOWNORMAL); == ERROR_PATH_NOT_FOUND);
    
    else
    {
      ShellExecute(NULL, "explore", "D:\\Backup\\", NULL, NULL, SW_SHOWNORMAL);
    }
    
    	return 0;
    }
    Doesn't compile properly. Have also tried putting { a line before and } a line after, to no avail.

    Please tell me how to get my code to work!

    For the end result I am wishing, there is 1 network (UNC) path & 2 local paths.

    Please help me reach my goal!

    Thanks in advance,

    Panarchy
     
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    It would not

    Code:
    if(ShellExecute(NULL, "explore", "\\\\panarchy\\share", NULL, NULL, SW_SHOWNORMAL); == ERROR_PATH_NOT_FOUND);
    Should be

    Code:
    if(ShellExecute(NULL, "explore", "\\\\panarchy\\share", NULL, NULL, SW_SHOWNORMAL) == ERROR_PATH_NOT_FOUND);
    So I guess you need to first get around your coding debug before you can attempt the errors in ShellExecute return things
     
  11. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Aha! The colon was in the wrong place.

    I'll try it now, thanks for the pointer!

    EDIT: Just gave it a go, got the following;

    ||=== Release ===|
    main.cpp||In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)':|
    main.cpp|8|error: ISO C++ forbids comparison between pointer and integer|
    ||=== Build finished: 1 errors, 0 warnings ===|

    Should I be using a different IDE/compiler? (using Code::Blocks with MinGW gcc)
     
    Last edited: May 3, 2009
  12. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Well, the code is pretty much complete now;

    Code:
    #include <windows.h>
    #include <tchar.h>
    #include <iostream>
    
    #ifdef UNICODE
    #define tcout wcout
    #else
    #define tcout cout
    #endif
    
    int APIENTRY WinMain(HINSTANCE hinstance, HINSTANCE hprevious, LPSTR cmdline, int cmdshow) {
    
        STARTUPINFO si = {0};
        PROCESS_INFORMATION pi = {0};
        TCHAR lpCmdLine[MAX_PATH] = {0};
        ::ExpandEnvironmentStrings(_T("\"%ProgramFiles%\\Symantec\\LiveUpdate\\LUALL.exe\""),
                                  lpCmdLine, MAX_PATH);
    
        std::tcout << _T("Command line : ") << lpCmdLine << std::endl;
    
        BOOL bRet = ::CreateProcess(NULL, lpCmdLine, NULL, NULL, FALSE,
                                   CREATE_NO_WINDOW, NULL, NULL, &si, &pi);
    
        ShellExecute(NULL, TEXT("open"), TEXT("explorer"), TEXT("\\Panarchy\\share"), NULL, SW_HIDE);
        ShellExecute(NULL, TEXT("open"), TEXT("control"), TEXT("schedtasks\0"), NULL, SW_HIDE);
        ShellExecute(NULL, TEXT("open"), TEXT("control"), TEXT("sysdm.cpl"), NULL, SW_HIDE);
        ShellExecute(NULL, TEXT("open"), TEXT("diskmgmt.msc"), NULL, NULL, SW_HIDE);
    
        {
            if( reinterpret_cast<int>(ShellExecute(NULL, "explore", "\\\\panarchy\\share", NULL, NULL, SW_SHOWNORMAL)) <= 32)
                if( reinterpret_cast<int>(ShellExecute(NULL, "explore", "D:\\BackYouUp\\", NULL, NULL, SW_SHOWNORMAL)) <= 32)
                {
                    ShellExecute(NULL, "explore", "C:\\Backups\\bac\\", NULL, NULL, SW_SHOWNORMAL);
                }
        }
    
        return 0;
    }
    The only caveat is that it takes a while for the program to process the ifs. If anyone knows a way around that, I'd appreciate it. If not, then the program is complete.

    Panarchy
     
    Last edited: May 3, 2009
  13. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Try doing them in a thread because Shell functions you are executing are at times time consuming
     
  14. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    How do I do them in a thread?

    Please tell me how!

    Thanks in advance,

    Panarchy
     
  15. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Look at the CreateThread API
     
  16. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    I see the information, however I don't understand it.

    Please give me an example
     
  17. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
  18. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Yes, that is exactly what I don't understand.

    Please give me an example of it, if possible, within my complete code.

    Thanks in advance,

    Panarchy
     
  19. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    I would still suggest you see the MSDN and read more about threads because it can be quite troublesome when dealing with threads.

    If you think you still need a sample refe Worker Threads in MFC and codes there but I am sure writing codes for threads needs a good knowledge before even attempting it.
     
  20. Panarchy

    Panarchy New Member

    Joined:
    Nov 29, 2007
    Messages:
    86
    Likes Received:
    0
    Trophy Points:
    0
    Are we speaking about converting my program to a multi-threaded one, which will launch everything at once?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice