FreeFox already posted the minimal code to call ShellExecute, but here's a complete version that checks for errors and uses ShellExecuteEx so that you can make
advanced requests (like opening in background? I haven't tried anything fancy)
(Edit:) One advantage of doing it this way is that a console window doesn't pop up momentarily, as with using SHELL. Plus an error if the webbrowser can't be launched, I hope. However, you still don't get any error message if the URL is invalid or malformed.
Code: Select all
#include once "windows.bi"
#include once "win/shlobj.bi"
'FormatMessage is such an awfully complex function
function get_windows_error (byval errcode as integer) as string
dim strbuf as string * 256
FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, NULL, errcode, 0, strptr(strbuf), 255, NULL)
return strbuf
end function
'Opens a file (or URL) with default handler.
'If successful returns "", otherwise returns an error message.
function open_document (filename as string) as string
'Initialise COM; may be necessary. May be called multiple times
'as long as the args are the same.
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED OR COINIT_DISABLE_OLE1DDE)
dim info as SHELLEXECUTEINFO
info.cbSize = SIZEOF(SHELLEXECUTEINFO)
'SEE_MASK_NOASYNC probably unneeded. Waits for the 'execute operation' to complete (does that
'mean better error catching?). Needed when called from background thread.
info.fmask = SEE_MASK_NOASYNC
info.lpVerb = @"" ' Use the default action for this URL/filename. Can instead explicitly pass @"open", but that doesn't work 100% of the time
info.lpFile = STRPTR(filename)
info.nShow = SW_SHOWNORMAL
if ShellExecuteEx(@info) = 0 then
return get_windows_error(GetLastError())
end if
return ""
end function