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

Panarchy's Avatar, Join Date: Nov 2007
Contributor
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 by Panarchy; 1May2009 at 04:48..
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
ShellExecute should return something based on the data opened successfully or not found kind of thing
Panarchy's Avatar, Join Date: Nov 2007
Contributor
Oh?

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

Thanks in advance,

Panarchy
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Quote:
Originally Posted by MSDN
If the function succeeds, it returns a value greater than 32. If the function fails, it returns an error value that indicates the cause of the failure. The return value is cast as an HINSTANCE for backward compatibility with 16-bit Windows applications. It is not a true HINSTANCE, however. It can be cast only to an int and compared to either 32 or the following error codes below.
Refer this

Example
Code:
if(ShellExecute('Your Sample Params Above') == ERROR_PATH_NOT_FOUND)
{
  Do something
}
else
{
  Do some other thing
}
Panarchy's Avatar, Join Date: Nov 2007
Contributor
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;

Quote:
main.cpp|7|warning: character constant too long for its type|
main.cpp||In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)':|
main.cpp|7|error: expected `)' before '\x4d414c29'|
main.cpp|7|warning: the address of `HINSTANCE__* ShellExecuteA(HWND__*, const CHAR*, const CHAR*, const CHAR*, const CHAR*, INT)', will always evaluate as `true'|
main.cpp|7|error: expected primary-expression before '==' token|
main.cpp|7|error: expected `;' before ')' token|
main.cpp|9|warning: unknown escape sequence '\s'|
main.cpp|12|error: expected primary-expression before "else"|
main.cpp|12|error: expected `;' before "else"|
||=== Build finished: 5 errors, 3 warnings ===|

Last edited by Panarchy; 1May2009 at 19:31..
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
Run and see if it works because at times return value is not always TRUE / FALSE which needs to be tested.
Panarchy's Avatar, Join Date: Nov 2007
Contributor
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);

Quote:
main.cpp|8|warning: unknown escape sequence '\s'|
main.cpp||In function `int WinMain(HINSTANCE__*, HINSTANCE__*, CHAR*, int)':|
main.cpp|10|error: expected primary-expression before "else"|
main.cpp|10|error: expected `;' before "else"|
||=== Build finished: 2 errors, 1 warnings ===|
Please tell me how to get the program to work!

Thanks in advance,

Panarchy
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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.
Panarchy's Avatar, Join Date: Nov 2007
Contributor
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
shabbir's Avatar, Join Date: Jul 2004
Go4Expert Founder
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