C++ – Converting a PIDL to file path with SHGetPathFromIDList

cshellwinapi

My application receives a PIDL as a string:

QString pidl = "::{20D04FE1-3AEA-1069-A2D8-08002B30309B}";

In this case it corresponds to My Computer. I need to convert it to My Computer.

Theres a WINAPI function SHGetPathFromIDList which requires the LPCITEMIDLIST as the first parameter and converts it to a string.

How can I build that LPCITEMIDLIST?

UPDATE

This is what I got so far:

LPCWSTR csPath = (LPCWSTR)"::{20D04FE1-3AEA-1069-A2D8-08002B30309B}";
LPITEMIDLIST stId = 0;
SFGAOF stSFGAOFIn = 0;
SFGAOF stSFGAOFOut = 0;
if(!FAILED(SHParseDisplayName(csPath, 0, stId, stSFGAOFIn, &stSFGAOFOut)))
    msgBox("not failed")

Unfortunately this code crashes.

Best Answer

Your code crashes because you are not setting up the 1st and 3rd parameters of SHParseDisplayName() correctly. Try this instead:

LPCWSTR csPath = L"::{20D04FE1-3AEA-1069-A2D8-08002B30309B}"; 
PIDLIST_ABSOLUTE stId = NULL; 
SFGAOF stSFGAOFIn = 0; 
SFGAOF stSFGAOFOut = 0; 
if (!FAILED(SHParseDisplayName(csPath, NULL, &stId, stSFGAOFIn, &stSFGAOFOut))) 
    msgBox("not failed")