• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 456
  • Last Modified:

Browseforfolder callback: global variable?

Hi experts,
I would like to have some customizable things in my programms.
I specifically would like to set a selected path in a BrowseForFolder dialog.
I found a callback function on EE that would take care of that:

int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lp, LPARAM pData)
{
   TCHAR szDir[MAX_PATH];
   switch(uMsg)
   {
   case BFFM_INITIALIZED:
      if (GetCurrentDirectory(sizeof(szDir)/sizeof(TCHAR), szDir))
      {
//Hardwirering the path in the sendmessage here.
//Want that to be a variable.
              SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)_T("c:\\"));
      }
      break;
   case BFFM_SELCHANGED:
      if (SHGetPathFromIDList((LPITEMIDLIST) lp ,szDir))
      {
              SendMessage(hWnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szDir);
      }
      break;
   }
   return 0;
}

1.However, I need to specify the path in SendMessage(,,,LPARAM(PATH)).
   I would like to use a variable to do that.
2. The Callback function is global, right?
    Where in a project would I put it if I have multiple Dialogs that would call that function?
3. Where to put the global variable (if needed).
4. Is there a better way to do it?

BTW pidlRoot should stay NULL since I want the user to be able to change the selection
to whatever diretory he chooses on his computer.

Thanks alot,
Jens
0
allmer
Asked:
allmer
  • 3
  • 3
1 Solution
 
AlexFMCommented:
LPARAM pData  is user data which is set in BROWSEINFO::lParam member. You can pass "this" pointer in lParam and access caller class using this pointer:

BROWSEINFO b;
...
b.lParam = this;
...
SHBrowseForFolder(&b);

int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lp, LPARAM pData)
{
    CMyClass* p = (CMyClass*)pData;   // get pointer to caller instance

    // access caller data through p pointer


}
0
 
allmerAuthor Commented:
I donot seem to be able to do the above :(
b.lParam = this;
Error msg:
error C2440: '=' : cannot convert from 'CSequestExtractionDlg *const ' to 'LPARAM'
Anyway,
how would I send a string or CString or anything similar to the callback function?
I would be fine with something like
b.lParam = "c:\\";
And getting the string in the callback function again.
Jens
0
 
AlexFMCommented:
b.lParam = (LPARAM)this;
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
AlexFMCommented:
If only one Folders dialog may be shown at the same time, you can use global variable, in this case lParam is not necessary.

CString g_sPath;

void 'CSequestExtractionDlg::ShowFolderDialog()
{
    g_sPath = "C:\\";

    SHBrowseForFolder(...);
}    

int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lp, LPARAM pData)
{
    // use g_sPath directly
}
0
 
allmerAuthor Commented:
Forget the last comment Alex.
I got it:

    BROWSEINFO bi;      
...
   string p="c:\\";
   bi.lParam = (LPARAM)&p;
   bi.lpfn = &BrowseCallbackProc;
   pidl = ::SHBrowseForFolder(&bi);
...

int CALLBACK BrowseCallbackProc(HWND hWnd, UINT uMsg, LPARAM lp, LPARAM pData)
{
      TCHAR szDir[MAX_PATH];
      string *path = (string*)pData;
      switch(uMsg)
      {
      case BFFM_INITIALIZED:
            SendMessage(hWnd, BFFM_SETSELECTION, TRUE, (LPARAM)path->c_str());
            break;
...
}
Thanks alot,
Jens
0
 
allmerAuthor Commented:
Thanks alot for the quick answer!
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now