CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Nov 2007
    Posts
    92

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

    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

  2. #2
    Join Date
    Aug 2006
    Posts
    157

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

    You can do something like:

    Code:
    int APIENTRY WinMain(HINSTANCE hinstance, HINSTANCE hprevious, LPSTR cmdline, int cmdshow) {
       if (GetFileAttributes(szFilePath) != 0xffffffff) {
          ShellExecute(NULL, "explore", szFilePath, NULL, NULL, SW_SHOWNORMAL);
       } else if (GetFileAttributes(szFilePath2) != 0xffffffff) {
          ShellExecute(NULL, "explore", szFilePath2, NULL, NULL, SW_SHOWNORMAL);
       }
       return 0;
    }

  3. #3
    Join Date
    Nov 2007
    Posts
    92

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

    Thanks

    However, I don't understand. Where do I define szFilePath?

    Please tell me where to define it.

    Thanks in advance,

    Panarchy

  4. #4
    Join Date
    Apr 2009
    Location
    Cochin
    Posts
    83

    Thumbs up Re: How can I use ShellExecute() with if and else commands?

    Quote Originally Posted by Panarchy View Post
    Thanks

    However, I don't understand. Where do I define szFilePath?

    Please tell me where to define it.

    Thanks in advance,

    Panarchy
    GetFileAttributes() is used to get the file attributes of a given file/folder.


    The "szFilePath" is a valid path to the file or folder.

    in your case its Hard-Coded as "\\\\panarchy\\share"

    Its not a good practice of Hard-Coding. You may search for an API which retrives you the Share folder of current user

    You can check the return value of ShellExecute() API with if...else conditions

    Code:
    HINSTANCE hInst_1;
    
    hInst_1 = ShellExecute(NULL, "explore", "\\\\panarchy\\share", NULL, NULL, SW_SHOWNORMAL);
    
                     if( hInst_1 > 32 && hInst_1 ! = 0 )
                           {
                                // Function call Sucessful
                           }
                      else
                           {
                                // Function failed, or An error is returned!
                           }
    Hopes this helps..
    "I studied everything but never topped. Today, toppers of the world's best universities are my employees"

    -William Henry Gates (Bill Gates)

  5. #5
    Join Date
    Nov 2007
    Posts
    92

    Resolved Thanks for your help... however, I still don't understand!

    Hello

    Thanks for replying.

    The UNC Path \\\\panarchy\\share is for the External hard-drive, which, always has that aforementioned UNC Path.

    I'm still a little confused, so the following would be correct?

    Code:
    HINSTANCE hInst_1;
    
    hInst_1 = ShellExecute(NULL, "explore", "\\\\panarchy\\share", NULL, NULL, SW_SHOWNORMAL);
    
                     if( hInst_1 > 32 && hInst_1 ! = 0 )
                           {
                                // Function call Successful
                           }
                      else
                           {
                                // Function failed, or An error is returned!
                           }
    hInst_2 = ShellExecute(NULL, "explore", "C:\\Backup", NULL, NULL, SW_SHOWNORMAL);
    
                     if( hInst_2 > 32 && hInst_2 ! = 0 )
                           {
                                // Function call Successful
                           }
                      else
                           {
                                // Function failed, or An error is returned!
                           }
    From looking over what I've written, it's obvious I don't understand!

    Could you please explain again?

    Thanks in advance,

    Panarchy

    PS: Perhaps an example with 3 folders? (1 UNC and 2 complete paths)

    EDIT: Perhaps something more like this?

    Code:
    HINSTANCE hInst_1;
    
    hInst_1 = ShellExecute(NULL, "explore", "\\\\panarchy\\share", NULL, NULL, SW_SHOWNORMAL);
    
                     if( hInst_1 > 32 && hInst_1 ! = 0 )
                           {
                                // Function call Sucessful
                           }
                      else
                           {
                                ShellExecute(NULL, "explore", "C:\\Backup", NULL, NULL, SW_SHOWNORMAL);
                                // Function failed, or An error is returned!
                           }
    Last edited by Panarchy; May 2nd, 2009 at 07:55 AM.

  6. #6
    Join Date
    Nov 2007
    Posts
    92

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

    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("\"&#37;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 by Panarchy; May 3rd, 2009 at 09:07 AM.

  7. #7
    Join Date
    Apr 2009
    Location
    Cochin
    Posts
    83

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

    Are you sure... that its the if's causing trouble??

    Better way to track is by putting a break point 2 or 3 lines above the suspicious area... and go through each lines.

    by the by, i cant understansd the logic of using that reinterpret casting template for the ShellExecute function, as you can directly eveluate return value because th function itself returns an integer.
    "I studied everything but never topped. Today, toppers of the world's best universities are my employees"

    -William Henry Gates (Bill Gates)

  8. #8
    Join Date
    Nov 2007
    Posts
    92

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

    I know for certain that it is. As the only thing I've added recently has been the ifs.

    I've been recommended to convert the program to a multi-threaded one. Could you help me do so?

    Thanks in advance,

    Panarchy

  9. #9
    Join Date
    Apr 2009
    Location
    Cochin
    Posts
    83

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

    lol.. i Cant help ...jus kidding....

    but.. CreateThread() API can help you...

    Read the MSDN Documentation for the API. There is a Pretty good example too...

    Actually... i dont understand the need of creating this small program as Multi-Threaded...

    maybe i'm wrong.. but i didnt find any big modules... a single thread seems to be enough..

    anyway, do so.. if you've got an advice from an expert...
    "I studied everything but never topped. Today, toppers of the world's best universities are my employees"

    -William Henry Gates (Bill Gates)

  10. #10
    Join Date
    Nov 2007
    Posts
    92

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

    Hi

    I don't know how to!

    Could you please help me make program multi-threaded?

    Thanks in advance,

    Panarchy

  11. #11
    Join Date
    Nov 2007
    Posts
    92

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

    How do I input error handling within the programming [if, else, etc.] controllers?

    Would appreciate your knowledge of how this could be done, within an example.

    Thanks in advance,

    Panarchy

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured