Equivalent ShellExecute in C++/CLI or NET

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hello,

Somebody knows which is the equivalent one in C++/CLI ?

HINSTANCE ShellExecute(HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);
 
Tomas said:
Somebody knows which is the equivalent one in C++/CLI ?

HINSTANCE ShellExecute(HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);

You create a Process object. In that object you insert some startup
information (see the ProcessStartInfo class) - specifically the fact that
the process exists to "open" a file and the name of the file. Then you call
the Start method on the process object. The example below (oops - in C#)
runs Notepad to open the file foo.txt

Regards,
Will
www.ivrforbeginners.com

using System;
using System.Diagnostics;

namespace Test
{
class Test
{
static void Main(string[] args)
{
Process p;
ProcessStartInfo pInfo;

pInfo = new ProcessStartInfo();
pInfo.Verb = "open";
pInfo.FileName = "c:\\foo.txt";
pInfo.UseShellExecute = true;

p = Process.Start(pInfo);
}
}
}
 
William,

Since when has C# been C++?

You can do exactly what you said in 3 line of code in C#/VB.NET because you
can say (VB.NET code):

If IO.File.Exists("C:\Foo.txt") = True Then
System.Diagnostics.Process.Start("C:\Foo.txt")
End If

or if you want:

If IO.File.Exists("C:\Foo.txt") = True Then
System.Disagnotics.Process.Start("C:\Foo.txt")

Which now is 1 line of code being an inline 'If' statement

As Open is the default verb in 'notepad.exe %1' you don't need to declare
the ProcessStartInfo. Plus, you do not need to use ShellExecute either
because the process will start a new instance of notepad launching what txt
file you passed to it

but the user wanted C/C++ & probably not managed Framework code either.

--
Newbie Coder
(It's just a name)

William DePalo said:
Tomas said:
Somebody knows which is the equivalent one in C++/CLI ?

HINSTANCE ShellExecute(HWND hwnd,
LPCTSTR lpOperation,
LPCTSTR lpFile,
LPCTSTR lpParameters,
LPCTSTR lpDirectory,
INT nShowCmd
);

You create a Process object. In that object you insert some startup
information (see the ProcessStartInfo class) - specifically the fact that
the process exists to "open" a file and the name of the file. Then you call
the Start method on the process object. The example below (oops - in C#)
runs Notepad to open the file foo.txt

Regards,
Will
www.ivrforbeginners.com

using System;
using System.Diagnostics;

namespace Test
{
class Test
{
static void Main(string[] args)
{
Process p;
ProcessStartInfo pInfo;

pInfo = new ProcessStartInfo();
pInfo.Verb = "open";
pInfo.FileName = "c:\\foo.txt";
pInfo.UseShellExecute = true;

p = Process.Start(pInfo);
}
}
}
 
Newbie Coder said:
Since when has C# been C++?

That's a pretty stupid question. Where did you read that I or anyone else
said that it was?
You can do exactly what you said in 3 line of code in
C#/VB.NET because you can say (VB.NET code):

So what?
As Open is the default verb in 'notepad.exe %1' you don't need to declare
the ProcessStartInfo. Plus, you do not need to use ShellExecute either
because the process will start a new instance of notepad launching what
txt
file you passed to it

No kidding? The OP asked about the .Net framework equivalent of Win32's
ShellExecute(). I could have simply pointed him to the framework classes in
question and told him he needed to set the UseShellExecute flag. Instead I
gave him a copy of C# compilable code. Sorry I disappointed you.

I could have given him C++, but didn't for three reasons.

1) I don't like to post code I haven't compiled and didn't have the time
when I posted to open up the IDE and build a sample.
2) There are two managed variants of C++, and I don't know which one he is
using.
3) For small tasks that make use of the framework, the difference between C#
and C++ is little more than syntatctic sugar for competent developers.

That said, this is the C++/CLI equivalent of the C# I posted:

// Process.cpp : main project file.

#include "stdafx.h"

using namespace System;
using namespace System::Diagnostics;

int main(array<System::String ^> ^args)
{
Process ^p;
ProcessStartInfo ^pInfo;

pInfo = gcnew ProcessStartInfo();
pInfo->Verb = "open";
pInfo->FileName = "c:\\foo.txt";
pInfo->UseShellExecute = true;

p = Process::Start(pInfo);
}

Regards,
Will
 
You can also just use the SHELL keyword

Example:

Shell("C:\SomePath\SomeFile.txt", AppWinStyle.NormalFocus)
Shell("C:\SomePath\SomeFile.txt", AppWinStyle.NormalFocus, True,
1000)

True in the second example is WaitForExit & the integer of 1000 is how many
milliseconds to wait
 
Back
Top