UrlGetPart, and the Parts it Won’t Get

This is a narrow post. If you don’t understand or don’t care, do skip it (you won’t be missing much). For the rest, and for the search engines, here’s some trivia.

There are a bunch of handy functions in the Win32 Shell API for handling file paths and URLs, provided by SHLWAPI.DLL. One of these, UrlGetPart, is described thusly:

Accepts a [sic] URL string and returns a specified part of that URL.

Very handy function, until you realize that the one part of an URL you can’t specify to be returned is the path. For example, in the (fictitious) url:

http://bond:007@example.com/topsecret/martini.cgi?method=shaken+not+stirred

I can use UrlGetPart to retrieve the scheme (http:), the username (bond), the password (007), the host (example.com), and the query (?method=shaken+not+stirred), but I cannot retrieve the path (/topsecret/martini.cgi).

So, How do extract the path from an URL using UrlGetPart? You don’t. Instead, use InternetCrackUrl, in wininet.dll. If you need to do this from VB6 (and this may work with VB5, but I’ve not tried it), take a look at this helper class, which also supports a few other handy URL-handling functions.

Both comments and pings are currently closed.

Comments are closed.