Opening a webpage

New to FreeBASIC? Post your questions here.
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Re: Opening a webpage

Post by Zippy »

This is a solution using quoting:

Code: Select all

dim as string orgstr, newstr
orgstr = "http://www.ebay.com/sch/i.html?_&_nkw=beatles+1962&_sacat=11233"
newstr = "http://www.ebay.com/sch/i.html" & chr(34) & "?_&_nkw=beatles+1962&_sacat=11233" & chr(34)
'
print newstr
'
shell("start " & newstr)
'
sleep
"start" needs to be able to identify the program to find and use.
FreeFox
Posts: 69
Joined: Sep 28, 2016 23:45

Re: Opening a webpage

Post by FreeFox »

That works!

So it's the query part of the URL that needs to be enclosed in quotes, not the whole of the URL.

I still find it strange that the ShellExecute() function can sort this out without any assistance and the Start command can not, but at least we know now how to make FB's Shell command work with this sort of URL without resorting to calling API functions.

Thanks Zippy :)
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Opening a webpage

Post by dodicat »

Thanks Zippy.
Perhaps your code could be generalised by by:

Code: Select all


Function SAR(s0 As String,find As String,substitute As String) As String
    Dim s As String=s0
    Dim As Integer position=Instr(s,find)
    While position>0
        s=Mid(s,1,position-1) & substitute & Mid(s,position+Len(find))
        position=Instr(position+Len(substitute),s,find)
    Wend
    SAR=s
End Function
dim as string orgstr, newstr
orgstr = "http://www.ebay.com/sch/i.html?_&_nkw=beatles+1962&_sacat=11233"
'newstr = "http://www.ebay.com/sch/i.html" & chr(34) & "?_&_nkw=beatles+1962&_sacat=11233" & chr(34)

 newstr =sar(orgstr,"html","html""")


shell("start " & newstr)
print "Press a key to exit"
sleep 
Some of these albums are expensive!

Microsoft edge is the default browser here on Win 10.
Tourist Trap
Posts: 2958
Joined: Jun 02, 2015 16:24

Re: Opening a webpage

Post by Tourist Trap »

Zippy wrote:This is a solution using quoting:

Code: Select all

dim as string orgstr, newstr
orgstr = "http://www.ebay.com/sch/i.html?_&_nkw=beatles+1962&_sacat=11233"
newstr = "http://www.ebay.com/sch/i.html" & chr(34) & "?_&_nkw=beatles+1962&_sacat=11233" & chr(34)
'
print newstr
'
shell("start " & newstr)
'
sleep
"start" needs to be able to identify the program to find and use.
Thanks! I think this is worth to be added in the documentation (@fxm, the start page is tiny).
It occurs also that Start will fail to parse any non-url stuff like a string sent to a program as argument, as it occurred before to me as far as I can remember.
caseih
Posts: 2157
Joined: Feb 26, 2007 5:32

Re: Opening a webpage

Post by caseih »

What's the difference between using CHR(34) and the "" inside a string?

Code: Select all

a = "this " + chr(34) + "that + chr(34)
b = "this ""that"""
Zippy
Posts: 1295
Joined: Feb 10, 2006 18:05

Re: Opening a webpage

Post by Zippy »

caseih wrote:What's the difference between using CHR(34) and the "" inside a string?

Code: Select all

a = "this " + chr(34) + "that + chr(34)
b = "this ""that"""
There's no functional difference. chr(34) is easier for me to parse visually. Is this "'' 2 double quotes, or... not?
TeeEmCee
Posts: 375
Joined: Jul 22, 2006 0:54
Location: Auckland

Re: Opening a webpage

Post by TeeEmCee »

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
Oxford Punter
Posts: 3
Joined: Nov 10, 2016 2:47

Re: Opening a webpage

Post by Oxford Punter »

An embarrassed OP (that would be me...) is back in the building! The complexities of Life, and more importantly, not checking the "Notify me when a reply is posted" checkbox are to blame. (Whoops.) My apologies. It was not my intent to ask and then disappear. Especially with so many great responses.

So... thank you all for your wisdom! I always liked that there are multiple ways to approach a programming problem. I need to study your suggestions, try them out, and hope to absorb it. Like I said... it's been a while.

Best, ---Dave
Oxford Punter
Posts: 3
Joined: Nov 10, 2016 2:47

Re: Opening a webpage

Post by Oxford Punter »

More specifically, thank you caseih, FreeFox, MrSwiss, dodicat, Tourist Trap, Zippy, TeeEmCee for the lovely input.

(dodicat... I liked the "Sporting News" snooker news from your early message. It has been far too long since I have been able to combine snooker, a pint, and some friends. Alas.)
Post Reply