Jump to content

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

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Panarchy

Panarchy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 258 posts
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;

#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
Panarchy

Panarchy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 258 posts
Well, the code is pretty much complete now;

#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

Edited by Panarchy, 03 May 2009 - 06:07 AM.


#3
Panarchy

Panarchy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 258 posts
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

#4
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
There are several types of error handling. Try/catch blocks are one, using if statements to check for special cases are another. It all depends on the details of what you're watching out for.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#5
Panarchy

Panarchy

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 258 posts
To be more specific;

if location1 exists, open in explorer
else open location2
if location2 doesn't exist, open location3

I'd like for if location1 doesn't exist an error to be outputted, before opening location2/location3.

How would I go about doing that?

Thanks in advance for any more help with this,

Panarchy

#6
ikonia

ikonia

    Newbie

  • Members
  • PipPip
  • 24 posts
this should do

online C++ tutorial: Branching Statements (if, else, switch)