-
April 30th, 2009, 06:32 PM
#1
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
-
May 1st, 2009, 01:56 AM
#2
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;
}
-
May 1st, 2009, 09:24 AM
#3
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
-
May 2nd, 2009, 03:53 AM
#4
Re: How can I use ShellExecute() with if and else commands?
Originally Posted by Panarchy
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)
-
May 2nd, 2009, 07:51 AM
#5
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.
-
May 3rd, 2009, 08:31 AM
#6
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("\"%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.
-
May 4th, 2009, 01:07 AM
#7
-
May 4th, 2009, 04:48 PM
#8
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
-
May 5th, 2009, 11:21 AM
#9
-
May 6th, 2009, 12:07 AM
#10
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
-
June 14th, 2009, 01:11 AM
#11
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|