Hello,

I am very new to Win32 Shell Programming. I am doing a simple project which needs me to select a folder from the hard-drive.

The following is small parts of the program:
1 : This is the "Brows For folders" calling part.

case IDC_BROWSE:
			{
				char buf[MAX_PATH];
				BrowseFolders(hDlg, buf);
				SendMessage(GetDlgItem(hDlg,IDC_EDIT1), WM_SETTEXT, 0, (LPARAM)buf);
				return TRUE;
			}

2 : This is the BrowseFolders func with its accompanying CALLBACK.

int CALLBACK BrowseForFolderCallback(HWND hwnd,UINT uMsg,LPARAM lParam, LPARAM pData)
{
	wchar_t szPath[MAX_PATH];

	switch(uMsg)
	{
		case BFFM_INITIALIZED:
			SendMessage(hwnd, BFFM_SETSELECTION, TRUE, pData);
			break;

		case BFFM_SELCHANGED: 
			{
			TCHAR szText[MAX_PATH] = {0};
            SHGetPathFromIDList(
                reinterpret_cast<LPITEMIDLIST>(lParam), szText);
            SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0,
                reinterpret_cast<LPARAM>(szText));
			}

			break;
	}

	return 0;
}

BOOL BrowseFolders(HWND hwnd, LPSTR lpszFolder)
{
	BROWSEINFO bi;
	wchar_t szPath[MAX_PATH + 1];
	LPITEMIDLIST pidl;
	BOOL bResult = FALSE;

	LPMALLOC pMalloc;

    if (SUCCEEDED(SHGetMalloc(&pMalloc))) 
	{
		bi.hwndOwner = hwnd;
		bi.pidlRoot = NULL;
		bi.pszDisplayName = NULL;
		bi.lpszTitle = L"Browse for folder";
		bi.ulFlags = BIF_STATUSTEXT;
		bi.lpfn = BrowseForFolderCallback;
		bi.lParam = (LPARAM)lpszFolder;
		
		pidl = SHBrowseForFolder(&bi);
		if (pidl)
		{
			if (SHGetPathFromIDList(pidl,szPath))
			{
				bResult = TRUE;
				strcpy(lpszFolder, (const char *)szPath);
			}

		   pMalloc->Free(pidl);
		   pMalloc->Release();
            			
		}
	}

	return bResult;
	
}

I have also tried this in place for BFFM_SELCHANGED :

if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szPath)) 
			{
				SendMessage(hwnd, BFFM_SETSTATUSTEXT,0,(LPARAM)szPath);	

			}

The problem is that the buf is filled with some garbage values. If I want to SETTEXT in an edit field, the buf data, I only get the first letter of the path selected. For ex: for C:/AAA/ , I get C followed by many little boxes(too many of them) .

I don't know what wrong I'm doing. Can anybody please help me in recognizing my fault for getting garbage values in buf!

Thanks in Advance

*BUMP* This has been here for more than 7 hours. Please somebody answer this...

Ah! I got the solution to my problem. It was because of the wrong datatype, and in the code above I have passed String in for LPSTR(which I made a mistake here).

So, much for a datatype!!!

But, now, the problem is, the BrowseFolder function is not returning the right folder!

Can anybody help!

I don't think anyone is even interested in answering this! But, I found out strpy is not used correctly in VisualStudio.

***Marking as solved*** - Starting a new topic!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.