lesmydad
asked on
FolderBrowserDialogue problem
Hi Experts,
I am fairly new to Visual C++, my expertise lies within C#. I have this problem, i have a Windows.Form it containes a listView panel, a textbox and a button. I want when users click that button the folderbrowser dialogue attribute to open and show all the files from that directory in the listView panel, also the the textbox should show the directory path of the folder selected.
Can someone direct me on how i should go about doing this. I done it in C# but find it difficult to convert it into C++.
Thank You.
Les
I am fairly new to Visual C++, my expertise lies within C#. I have this problem, i have a Windows.Form it containes a listView panel, a textbox and a button. I want when users click that button the folderbrowser dialogue attribute to open and show all the files from that directory in the listView panel, also the the textbox should show the directory path of the folder selected.
Can someone direct me on how i should go about doing this. I done it in C# but find it difficult to convert it into C++.
Thank You.
Les
HRESULT hr;
ITEMIDLIST *pItemList;
BROWSEINFO browseinfo;
TCHAR path[MAX_PATH];
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
{
TRACE("CoInitEx failed: %x\n", hr);
return;
}
IMalloc *pMalloc = NULL;
hr = SHGetMalloc(&pMalloc);
if (FAILED(hr))
{
TRACE("Can't retrieve system's IMalloc interface: %x\n", hr);
return;
}
ASSERT(pMalloc);
memset(&browseinfo, 0, sizeof(BROWSEINFO));
CString strCaption;
browseinfo.hwndOwner = GetSafeHwnd();
browseinfo.pszDisplayName = path;
browseinfo.ulFlags = BIF_RETURNONLYFSDIRS;
strCaption = "Auu";
browseinfo.lpszTitle = strCaption;
pItemList = SHBrowseForFolder(&browsei nfo);
if (pItemList)
{
// Behold the elegance of CStrBuf!
SHGetPathFromIDList(pItemL ist, path);
pMalloc->Free(pItemList);
UpdateData(FALSE);
}
pMalloc->Release();
pMalloc = NULL;
CoUninitialize();
Must
#include "objbase.h"
#include "shlobj.h"
Link against ole32.lib and shell32.lib
ITEMIDLIST *pItemList;
BROWSEINFO browseinfo;
TCHAR path[MAX_PATH];
hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
if (FAILED(hr))
{
TRACE("CoInitEx failed: %x\n", hr);
return;
}
IMalloc *pMalloc = NULL;
hr = SHGetMalloc(&pMalloc);
if (FAILED(hr))
{
TRACE("Can't retrieve system's IMalloc interface: %x\n", hr);
return;
}
ASSERT(pMalloc);
memset(&browseinfo, 0, sizeof(BROWSEINFO));
CString strCaption;
browseinfo.hwndOwner = GetSafeHwnd();
browseinfo.pszDisplayName = path;
browseinfo.ulFlags = BIF_RETURNONLYFSDIRS;
strCaption = "Auu";
browseinfo.lpszTitle = strCaption;
pItemList = SHBrowseForFolder(&browsei
if (pItemList)
{
// Behold the elegance of CStrBuf!
SHGetPathFromIDList(pItemL
pMalloc->Free(pItemList);
UpdateData(FALSE);
}
pMalloc->Release();
pMalloc = NULL;
CoUninitialize();
Must
#include "objbase.h"
#include "shlobj.h"
Link against ole32.lib and shell32.lib
ASKER
Hi nonubik
How do i link to ole32.lib and shell32.lib using visual C++ in VS.NET.
Thanks
Les
How do i link to ole32.lib and shell32.lib using visual C++ in VS.NET.
Thanks
Les
Open project properties, go to Linker->Input->'Additional dependencies' field. Here enter the 2 libs
ASKER
I still get some error,
error C2065: CString is undeclared identifies and same for strCaption, ASSERT identifier not found etc.
Dont have a clue why these errors are comming up, did i put the script in the wrong constructor. I pasted thed above code under the button event handler. is that could be the problem.
Thanks
Les
error C2065: CString is undeclared identifies and same for strCaption, ASSERT identifier not found etc.
Dont have a clue why these errors are comming up, did i put the script in the wrong constructor. I pasted thed above code under the button event handler. is that could be the problem.
Thanks
Les
Replace
ASSERT(pMalloc);
memset(&browseinfo, 0, sizeof(BROWSEINFO));
CString strCaption;
browseinfo.hwndOwner = GetSafeHwnd();
browseinfo.pszDisplayName = path;
browseinfo.ulFlags = BIF_RETURNONLYFSDIRS;
strCaption = "Auu";
browseinfo.lpszTitle = strCaption;
with
if(!pMalloc)
return; //if you are within a function or some error message
memset(&browseinfo, 0, sizeof(BROWSEINFO));
TCHAR szCaption[256];
browseinfo.hwndOwner = GetSafeHwnd();
browseinfo.pszDisplayName = path;
browseinfo.ulFlags = BIF_RETURNONLYFSDIRS;
strCaption = "Auu";
browseinfo.lpszTitle = szCaption;
ASSERT(pMalloc);
memset(&browseinfo, 0, sizeof(BROWSEINFO));
CString strCaption;
browseinfo.hwndOwner = GetSafeHwnd();
browseinfo.pszDisplayName = path;
browseinfo.ulFlags = BIF_RETURNONLYFSDIRS;
strCaption = "Auu";
browseinfo.lpszTitle = strCaption;
with
if(!pMalloc)
return; //if you are within a function or some error message
memset(&browseinfo, 0, sizeof(BROWSEINFO));
TCHAR szCaption[256];
browseinfo.hwndOwner = GetSafeHwnd();
browseinfo.pszDisplayName = path;
browseinfo.ulFlags = BIF_RETURNONLYFSDIRS;
strCaption = "Auu";
browseinfo.lpszTitle = szCaption;
ASKER
Hi nonubik
I still get errors of C2065 with strCaption = "Auu"; and around four errors on C3861 with scripts like hr = CoInitializeEx, browseinfo.hwndOwner = GetSafeHwnd(); etc. dont have a clue why this problem is hapenning.
As i mention C++ is something new to me therefore i'm not understanding the concept properly.
Thank You
Les
I still get errors of C2065 with strCaption = "Auu"; and around four errors on C3861 with scripts like hr = CoInitializeEx, browseinfo.hwndOwner = GetSafeHwnd(); etc. dont have a clue why this problem is hapenning.
As i mention C++ is something new to me therefore i'm not understanding the concept properly.
Thank You
Les
sorry, my mistake :o)
>strCaption = "Auu";
replace
>lstrcat(szCaption, _T("My title"));
>strCaption = "Auu";
replace
>lstrcat(szCaption, _T("My title"));
again, rushed
>lstrcpy(szCaption, _T("My title"));
>lstrcpy(szCaption, _T("My title"));
ASKER
still have other problems arosing. here are the errors which pops up:
(102): error C3861: 'CoInitializeEx': identifier not found, even with argument-dependent lookup
(105): error C3861: 'TRACE': identifier not found, even with argument-dependent lookup
(113): error C3861: 'TRACE': identifier not found, even with argument-dependent lookup
(121): error C3861: 'GetSafeHwnd': identifier not found, even with argument-dependent lookup
(134): error C3861: 'UpdateData': identifier not found, even with argument-dependent lookup
What do i do to solve them.
Thanks,
Les
(102): error C3861: 'CoInitializeEx': identifier not found, even with argument-dependent lookup
(105): error C3861: 'TRACE': identifier not found, even with argument-dependent lookup
(113): error C3861: 'TRACE': identifier not found, even with argument-dependent lookup
(121): error C3861: 'GetSafeHwnd': identifier not found, even with argument-dependent lookup
(134): error C3861: 'UpdateData': identifier not found, even with argument-dependent lookup
What do i do to solve them.
Thanks,
Les
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
heres one code i got from net, paste the code in ur button click event
//////////////////////////
CString m_strInitDir, szDirectory;
int m_iImageIndex;
HRESULT hr;
OLECHAR chOlePath[255];
ULONG pchEaten;
ULONG dwAttributes;
LPSHELLFOLDER pDesktopFolder;
BROWSEINFO browseInfo;
LPITEMIDLIST lpItemList;
ZeroMemory ( (PVOID) &browseInfo,sizeof (BROWSEINFO));
LPMALLOC lpM;
hr = SHGetMalloc (&lpM) ;
if (FAILED(hr) ) return ;
hr = SHGetDesktopFolder(&pDeskt
if ( FAILED(hr) ) return;
MultiByteToWideChar(CP_ACP
chOlePath, MAX_PATH);
m_strInitDir.ReleaseBuffer
hr = pDesktopFolder->ParseDispl
NULL,
chOlePath,
&pchEaten,
&lpItemList,
&dwAttributes);
if (FAILED(hr))
{
lpM->Free (lpItemList);
lpM->Release ();
return ;
}
browseInfo.pidlRoot = lpItemList;
browseInfo.hwndOwner = NULL;
browseInfo.pszDisplayName = szDirectory.GetBuffer (MAX_PATH);
browseInfo.lpszTitle = "Open Folder ";
browseInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_
browseInfo.lParam = (LPARAM)this; // pass address of object to callback function
if ((lpItemList = ::SHBrowseForFolder(&brows
{
return ;
}
szDirectory.ReleaseBuffer(
m_iImageIndex = browseInfo.iImage;
if (::SHGetPathFromIDList(lpI
{
// Get the selected directory name
szDirectory.ReleaseBuffer(
}
lpM->Free(lpItemList);
lpM->Release();
//////////////////////////
u can also search MSDN and on other sites for similar code. Sometimes use will get a more simpler code :)
also plz chk the link
http://www.codeproject.com/dialog/folderbrowser.asp
hope this helps
kiranvj