ShellExecute to open HTML file in VB.Net

R

Robert Styma

I have used SHellExecute from C++ successfully on
Visual Studio 6. I am currently using Visual Studio 2008
coding in Visual Basic. All the examples I have tracked down
to use ShellExecute to open a file with the appropriate application
end up looking something like this:


Private Declare Function ShellExecute Lib "shell32.dll" _
Alias "ShellExecuteA" _
(ByVal hwnd As Long, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Long) As Long

Private Sub Command1_Click()
Const SW_SHOWDEFAULT As Long = 10
Dim sFullPathToFile As String
Dim rc As Long
sFullPathToFile = "C:\tmp\EditMenu.htm"

rc = ShellExecute(0, "Open", sFullPathToFile _
, 0&, 0&, SW_SHOWDEFAULT)
End Sub

When I execute the above, it does not throw an exception, It does not
generate anything in the event log. It appears to run.
rc is returned as 1642688007033061378
x"16CA000A00000002"
According to the documentation, this is a handle and means the
call worked.
The path to the file does exist and IE is defined as the
default application for .html files. The problem is that
nothing happens. I have tried changing the command
to SW_SHOWNORMAL As Long = 1 with the same result.

The command shows it worked, but nothing appears

Can someone give me a pointer as to how to fix this issue?
 
R

Robert Styma

Thank you. That worked well. The Sample code now appears as follows:


Private Sub Command1_Click()
Dim sFullPathToFile As String
sFullPathToFile = "C:\tmp\EditMenu.htm"
Try
Process.Start(sFullPathToFile)
Catch ex As Exception
MessageBox.Show(ex.Message & vbNewLine & ex.ToString)
End Try
End Sub

--
Robert Styma
Principal Engineer (DMTS)
Alcatel-Lucent


Patrice said:
You can do the same with Process.Start without using interop
(http://msdn.microsoft.com/en-us/library/system.diagnostics.process.start.aspx)...

If you still want to sue interop try
http://www.pinvoke.net/default.aspx/shell32/ShellExecute.html (not that Long
in preivous releases is now Integer so take care of this when taking a
Declare statement that was working with pre .NET VB releases).
 
J

Jie Wang [MSFT]

Hi Robert,

Besides Patrice's solution (thank you for sharing, Patrice), I'd also like
to explain a little bit about why your VB6 code is not working in VB.NET.

Your original VB6 code declares the first parameter hwnd as Long, which is
fine in VB6 because Long is a 32bit value back then.

However, in VB.NET, Long is a 64bit value. You must be using a 32bit OS
where a hwnd should be 32 bits. So this is corrupting the stack, and
acutally the ShellExecute returns an error code &H2. But again, the return
value type has been declared wrong as Long, so you will get extra data
ended up like &H16CA000A00000002.

To fix it up, we need the make changes to the data types used:

Private Declare Auto Function ShellExecute Lib "shell32.dll" ( _
ByVal hwnd As IntPtr, _
ByVal lpOperation As String, _
ByVal lpFile As String, _
ByVal lpParameters As String, _
ByVal lpDirectory As String, _
ByVal nShowCmd As Integer) As Integer

Private Const SW_SHOWDEFAULT As Integer = 10

Here, we use IntPtr as the type of hwnd, it is a platform-specific type
that is used to represent a pointer or a handle. And other "Long"s are
replaced with "Integer"s.

When calling this function, it looks like this:

Dim sFullPathToFile As String
Dim rc As Long
sFullPathToFile = "C:\tmp\EditMenu.htm"

rc = ShellExecute(IntPtr.Zero, "Open", sFullPathToFile _
, Nothing, Nothing, SW_SHOWDEFAULT)

And regarding the return value of ShellExecute, it is not really a handle.

Quoting MSDN document:

Returns a value greater than 32 if successful, or an error value that is
less than or equal to 32 otherwise. The following table lists the error
values. The return value is cast as an HINSTANCE for backward compatibility
with 16-bit Windows applications. It is not a true HINSTANCE, however. The
only thing that can be done with the returned HINSTANCE is to cast it to an
int and compare it with the value 32 or one of the error codes below.

http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx

In VB.NET, using Process class instead of calling ShellExecute can be a
cleaner solution. I'm explaining above data type things to you just in case
there might be other similar problems in your project.

If you have any further questions or concerns, please feel free to post
here.

Best regards,

Jie Wang ([email protected], remove 'online.')

Microsoft Online Community Support

Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/en-us/subscriptions/aa948868.aspx#notifications.

Note: MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 2 business days is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions. Issues of this
nature are best handled working with a dedicated Microsoft Support Engineer
by contacting Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/en-us/subscriptions/aa948874.aspx
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top