How to use PathCreateFromUrlA API

0

I need to convert a uri path like file:///C:/test folder/file.text to windows style path C:/test folder/file.text. For that, I am trying to use PathCreatesFromUrlA API using the following code but I just don't understand how to set up the last two parameters to the API (marked as ??? in the code below).

std::string uri("file:///C:/test folder/file.text");  
char buf[255];  
auto result = ::PathCreateFromUrlA(uri.c_str(), buf, ???, ???); 

The PathCreateFromUrlA is declared as follows.

LWSTDAPI PathCreateFromUrlA(_In_ PCSTR pszUrl, _Out_writes_to_(*pcchPath, *pcchPath) PSTR pszPath, _Inout_ DWORD *pcchPath, DWORD dwFlags);

According to this, the third parameter is a pointer to DWORD, I don't understand what is the purpose and how to use it. The fourth parameter is a DWORD for some flags. The shlwapi.h declares following flags

#define URL_UNESCAPE                    0x10000000
#define URL_ESCAPE_UNSAFE               0x20000000
#define URL_PLUGGABLE_PROTOCOL          0x40000000
#define URL_WININET_COMPATIBILITY       0x80000000
#define URL_DONT_ESCAPE_EXTRA_INFO      0x02000000
#define URL_DONT_UNESCAPE_EXTRA_INFO    URL_DONT_ESCAPE_EXTRA_INFO
#define URL_BROWSER_MODE                URL_DONT_ESCAPE_EXTRA_INFO
#define URL_ESCAPE_SPACES_ONLY          0x04000000
#define URL_DONT_SIMPLIFY               0x08000000
#define URL_NO_META                     URL_DONT_SIMPLIFY
#define URL_UNESCAPE_INPLACE            0x00100000
#define URL_CONVERT_IF_DOSPATH          0x00200000
#define URL_UNESCAPE_HIGH_ANSI_ONLY     0x00400000
#define URL_INTERNAL_PATH               0x00800000  // Will escape #'s in paths
#define URL_FILE_USE_PATHURL            0x00010000
#if (_WIN32_IE >= _WIN32_IE_IE60SP2)
#define URL_DONT_UNESCAPE               0x00020000  // Do not unescape the path/url at all
#endif // _WIN32_IE_IE60SP2
#if (NTDDI_VERSION >= NTDDI_WIN7)
#define URL_ESCAPE_AS_UTF8              0x00040000  // Percent-encode all non-ASCII characters as their UTF-8 equivalents.
#endif // (NTDDI_VERSION >= NTDDI_WIN7)
#if (NTDDI_VERSION >= NTDDI_WIN8)
#define URL_UNESCAPE_AS_UTF8            URL_ESCAPE_AS_UTF8
#define URL_ESCAPE_ASCII_URI_COMPONENT  0x00080000  // Percent-encode all ASCII characters outside of the unreserved set from URI RFC 3986 (a-zA-Z0-9-.~_) (i.e.) No need for URL_ESCAPE_PERCENT along with this.
#define URL_ESCAPE_URI_COMPONENT        (URL_ESCAPE_ASCII_URI_COMPONENT | URL_ESCAPE_AS_UTF8)
#define URL_UNESCAPE_URI_COMPONENT      URL_UNESCAPE_AS_UTF8
#endif // (NTDDI_VERSION >= NTDDI_WIN8)
#define URL_ESCAPE_PERCENT              0x00001000
#define URL_ESCAPE_SEGMENT_ONLY         0x00002000  // Treat the entire URL param as one URL segment.

#define URL_PARTFLAG_KEEPSCHEME         0x00000001

#define URL_APPLY_DEFAULT               0x00000001
#define URL_APPLY_GUESSSCHEME           0x00000002
#define URL_APPLY_GUESSFILE             0x00000004
#define URL_APPLY_FORCEAPPLY            0x00000008

I don't know which of the flag will be the right one for my use case. Any suggestions?

c++
windows
visual-studio
uri
asked on Stack Overflow Apr 27, 2019 by BKS • edited Apr 27, 2019 by BKS

1 Answer

2

The third parameter indicates the size of the buffer (and most likely contains the number of characters written or required after the function returns). MSDN says the fourth parameter is reserved and must be 0.

std::string uri("file:///C:/test folder/file.text");  
char buf[MAX_PATH];  
DWORD cch = ARRAYSIZE(buf);
HRESULT hr = ::PathCreateFromUrlA(uri.c_str(), buf, &cch, 0);
if (SUCCEEDED(hr)) ...
answered on Stack Overflow Apr 27, 2019 by Anders

User contributions licensed under CC BY-SA 3.0