Search code examples
winapiinno-setuppascalscript

Inno Setup - UrlCreateFromPath


I'd like to be able to use the UrlCreateFromPathW function from Shlwapi.dll in my installer script, but I haven't been able to get it to work.

HRESULT UrlCreateFromPath(
  _In_    PCTSTR pszPath,
  _Out_   PTSTR  pszUrl,
  _Inout_ DWORD  *pcchUrl,
          DWORD  dwFlags
);

I've cobbled together the following from other "Inno Setup" tagged questions here, and from glancing at the JEDI translation for the DLL. TryUrlCreateFromPath always returns false. Any assistance would be much appreciated.

[Code]
const
  INTERNET_MAX_URL_LENGTH = 2048 + 32 + 3;
  S_OK = $00000000;

function UrlCreateFromPathW(pszPath, pszUrl: string; var pcchUrl: DWORD; dwFlags: DWORD): HResult; external 'UrlCreateFromPathW@Shlwapi.dll stdcall';

function TryUrlCreateFromPath(const path: string; var url: string): Boolean;
var
  charcount: dword;
  flags: dword;
begin
  SetLength(url, INTERNET_MAX_URL_LENGTH);
  flags := 0;
  Result := UrlCreateFromPathW(path, url, charcount, flags) = S_OK;
  if Result then
    SetLength(url, charcount); 
end;

function InitializeSetup: Boolean;
var
  URL: string;
  ErrorCode: Integer;
begin
  Result := True;

  if TryUrlCreateFromPath('c:\temp', URL) then
    MsgBox('URL: ' + URL, mbConfirmation, MB_OK)
  else
    MsgBox('ERROR', mbError, MB_OK);
end;

Solution

  • The pcchUrl is an in/out argument. On input, it must contain a number of characters allocated in the pszUrl.

    You do not initialize it. It most likely defaults to 0, hence the UrlCreateFromPath returns E_INVALIDARG.

    Initialize it like:

    charcount := INTERNET_MAX_URL_LENGTH;