c# and launch of default browser, mail

  • Thread starter Thread starter Michel H
  • Start date Start date
M

Michel H

Hello everyone,

While it seems simple to me, I just want to have a URL in my windows
application that people can click and then launch the URL in the default
browser. Same thing with a mailto: link.

Here is the code i'm trying to use which comes straight from the microsoft
site:

string target= "http://www.microsoft.com";

try
{
System.Diagnostics.Process.Start(target);
}
catch
(
System.ComponentModel.Win32Exception noBrowser)
{
if (noBrowser.ErrorCode==-2147467259)
MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
MessageBox.Show(other.Message);
}

However, I get a "The system cannot find the file specified" .......



Ok so I tried something else, such as calling the Shell32.dll library:

I made a class as such:

public class Class_shell32
{
[DllImport("Shell32.dll",CharSet=CharSet.Auto)]
public static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpVerb,
string lpFile,
string lpParameters,
string lpDirectory,
int nShowCmd );
}

and I call it like this:

IntPtr returned_handle;
returned_handle = Class_shell32.ShellExecute(this.Handle,
"http://www.cnn.com","",null,null,3);

While this doesn't cause an error, the value of returned_handle is less than
32, indicating that the document could not be launched....


Anyone have any ideas?

Michel.
 
Michel,

What is the call stack and the type of the exception when you try it the
first way? Perhaps there is a problem with a lower-level component
somewhere.

Hope this helps.
 
Michel,

What is the call stack and the type of the exception when you try it the
first way? Perhaps there is a problem with a lower-level component
somewhere.

Hope this helps.
 
Hi Nicholas,


I don't know how to find the call stack ot type of exeption, or I'm just not
familiar with the terms you are using (my ignorance), but I will try to
answer:


Executing the code:
-------------------------------

string target= http://www.microsoft.com;
System.Diagnostics.Process.Start(target);



causes an error with the following message:
-------------------------------------------

An unhandled exception of type 'System.ComponentModel.Win32Exception'
occurred in system.dll
Additional information: The system cannot find the file specified



The output panel displays the information below:
-----------------------------------------------------------


An unhandled exception of type 'System.ComponentModel.Win32Exception'
occurred in system.dll
Additional information: The system cannot find the file specified

Unhandled Exception: System.ComponentModel.Win32Exception: The system cannot
find the file specified
at System.Diagnostics.Process.StartWithShellExecuteEx(ProcessStartInfo
startInfo)
at System.Diagnostics.Process.Start()
at System.Diagnostics.Process.Start(ProcessStartInfo startInfo)
at System.Diagnostics.Process.Start(String fileName)
at WindowsApplication3.Main_form.toolBar1_ButtonClick(Object sender,
ToolBarButtonClickEventArgs e) in e:\ms projects\email reference\visual
studio projects\email reference - main\form_main.cs:line 3533
at System.Windows.Forms.ToolBar.OnButtonClick(ToolBarButtonClickEventArgs e)
at System.Windows.Forms.ToolBar.WmReflectCommand(Message& m)
at System.Windows.Forms.ToolBar.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(IntPtr hWnd, Int32
msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr
lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message&
m)
at System.Windows.Forms.Control.WmCommand(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.ContainerControl.WndProc(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc,
IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button,
Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ToolBar.WndProc(Message& m)
at System.Windows.Forms.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32
msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at
System.Windows.Forms.ComponentManager.System.Windows.Forms.UnsafeNativeMetho
ds+IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason,
Int32 pvLoopData)
at System.Windows.Forms.ThreadContext.RunMessageLoop(Int32 reason,
ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at WindowsApplication3.Main_form.Main() in e:\ms projects\email
reference\visual studio projects\email reference - main\form_main.cs:line
2894
The program '[3384] Email reference.exe' has exited with code 0 (0x0).



















Nicholas Paldino said:
Michel,

What is the call stack and the type of the exception when you try it the
first way? Perhaps there is a problem with a lower-level component
somewhere.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Michel H said:
Hello everyone,

While it seems simple to me, I just want to have a URL in my windows
application that people can click and then launch the URL in the default
browser. Same thing with a mailto: link.

Here is the code i'm trying to use which comes straight from the microsoft
site:

string target= "http://www.microsoft.com";

try
{
System.Diagnostics.Process.Start(target);
}
catch
(
System.ComponentModel.Win32Exception noBrowser)
{
if (noBrowser.ErrorCode==-2147467259)
MessageBox.Show(noBrowser.Message);
}
catch (System.Exception other)
{
MessageBox.Show(other.Message);
}

However, I get a "The system cannot find the file specified" .......



Ok so I tried something else, such as calling the Shell32.dll library:

I made a class as such:

public class Class_shell32
{
[DllImport("Shell32.dll",CharSet=CharSet.Auto)]
public static extern IntPtr ShellExecute(
IntPtr hwnd,
string lpVerb,
string lpFile,
string lpParameters,
string lpDirectory,
int nShowCmd );
}

and I call it like this:

IntPtr returned_handle;
returned_handle = Class_shell32.ShellExecute(this.Handle,
"http://www.cnn.com","",null,null,3);

While this doesn't cause an error, the value of returned_handle is less than
32, indicating that the document could not be launched....


Anyone have any ideas?

Michel.
 
Back
Top