winapi - SHBrowseForFolder not returning Selected folder string by reference -


hello there i'm working on windows application using windows api ms vc++ 2010 , have following code selecting folders:

bool browsefolder(tchar *result) {     browseinfo brwinfo = { 0 };     brwinfo.lpsztitle = _t("select source directory");     brwinfo.hwndowner = hwnd;     lpitemidlist pitemidl = shbrowseforfolder (&brwinfo);      if (pitemidl == 0) return false;       // full path of folder     tchar path[max_path];     if (shgetpathfromidlist (pitemidl, path)) result = path;      imalloc *pmalloc = 0;     if (succeeded(shgetmalloc(&pmalloc)))     {         pmalloc->free(pitemidl);         pmalloc->release();     }      ::messagebox(hwnd, result, "input", mb_ok);     ::messagebox(hwnd, inputfolder, "input", mb_ok); // reference test     return true; } 

so, opens browse folder dialog, saves selected folder string in reference parameter "result" , returns true if ok.

later call:

browsefolder(inputfolder); 

and when try print out content of "inputfolder", shows blank (inputfolder global variable tchar* inputfolder)

as can see in browsefolder definition send 2 message boxes 1 "result" , other "inputfolder" (this last 1 shows blank)

so question is.. if call: browsefolder(inputfolder); shouldn't "inputfolder" modified reference? why show empty?

thanks in advance.

if (shgetpathfromidlist (pitemidl, path)) result = path; 

this line problem. result isn't class std::string, it's pointer buffer of 1 or more tchar. assigning changes pointer point path, doesn't copy path buffer result points to.

if don't want change use string class need call function copy string supplied buffer. example:

stringcchcopy(result, max_path, path); 

(this assumes buffer max_path characters in size).


Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -