with the SHBrowseForDialog, is it necessary to use the IMalloc structure?
I have been using SHBrowseForDialog for ages without using IMalloc, but after reading that article i tried it but it just caused an exception (invalid memory address on free).
The code im using is here:
void ChooseDirectory(HWND hwnd, char* Directory)
{
//LPITEMIDLIST pidlRoot = NULL;
LPITEMIDLIST pidlSelected = NULL;
BROWSEINFO bi = {0};
LPMALLOC pMalloc;
char strFoldername[MAX_PATH] = "";
char strDirectory[MAX_PATH] = "";
char* pRelative = NULL;
if (SUCCEEDED(SHGetMalloc(&pMalloc)))
{
ZeroMemory(&bi,sizeof(bi));
bi.hwndOwner = hwnd;
bi.pidlRoot = 0;
bi.pszDisplayName = strFoldername;
bi.lpszTitle = "Choose a directory";
bi.ulFlags = BIF_RETURNONLYFSDIRS;
bi.lpfn = NULL;
bi.lParam = 0;
pidlSelected = SHBrowseForFolder(&bi);
if (pidlSelected)
{
SHGetPathFromIDList(pidlSelected, strFoldername);
if (strlen(strFoldername) > 0)
{
GetCurrentDirectory(MAX_PATH, strDirectory);
_strlwr(strDirectory);
_strlwr(strFoldername);
pRelative = strstr(strFoldername, strDirectory);
if (pRelative == NULL)
strcpy(Directory, strFoldername);
else
{
pRelative = pRelative + strlen(strDirectory);
strcpy(Directory, "."

;
strcpy(Directory + 1, pRelative);
}
}
pMalloc->Free(pMalloc);
}
pMalloc->Release();
}
}
Can you spot anything wrong? Is it necessary to have the pMalloc structure?