Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to force the initial directory to be 'My Computer'

How to force the initial directory to be 'My Computer'

Scheduled Pinned Locked Moved C / C++ / MFC
questionhelptutorial
11 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    Cyrus Dang
    wrote on last edited by
    #1

    Hi, How can I display an Open/Save File dialog (using CFileDialog) with the initial directory is 'My Computer'? I tried using SHGetSpecialFolderPath with param CSIDL_DRIVES, but the return string is _T(""), and the CFileDialog object seems not to understand an empty string in m_ofn.lpstrInitialDir. It ususally starts with the current directory. Please help me do this little thing. Thanks in advance.

    T 1 Reply Last reply
    0
    • C Cyrus Dang

      Hi, How can I display an Open/Save File dialog (using CFileDialog) with the initial directory is 'My Computer'? I tried using SHGetSpecialFolderPath with param CSIDL_DRIVES, but the return string is _T(""), and the CFileDialog object seems not to understand an empty string in m_ofn.lpstrInitialDir. It ususally starts with the current directory. Please help me do this little thing. Thanks in advance.

      T Offline
      T Offline
      TheGreatAndPowerfulOz
      wrote on last edited by
      #2

      use SHGetFolderPath instead From the docs: "This function is a superset of SHGetSpecialFolderPath, included with earlier versions of the Shell. On systems preceeding those including Shell32.dll version 5.0 (Windows Millennium Edition (Windows Me) and Windows 2000), SHGetFolderPath was obtained through SHFolder.dll, distributed with Microsoft Internet Explorer 4.0 and later versions. SHFolder.dll always calls the current platform's version of this function. If that fails, it will try to simulate the appropriate behavior. SHFolder.dll continues to be included for backward compatibility, though the function is now implemented in Shell32.dll."

      C 1 Reply Last reply
      0
      • T TheGreatAndPowerfulOz

        use SHGetFolderPath instead From the docs: "This function is a superset of SHGetSpecialFolderPath, included with earlier versions of the Shell. On systems preceeding those including Shell32.dll version 5.0 (Windows Millennium Edition (Windows Me) and Windows 2000), SHGetFolderPath was obtained through SHFolder.dll, distributed with Microsoft Internet Explorer 4.0 and later versions. SHFolder.dll always calls the current platform's version of this function. If that fails, it will try to simulate the appropriate behavior. SHFolder.dll continues to be included for backward compatibility, though the function is now implemented in Shell32.dll."

        C Offline
        C Offline
        Cyrus Dang
        wrote on last edited by
        #3

        Thanks, but it appears that SHGetFolderPath doesn't work with param CSIDL_DRIVES. The return string is empty. Sincerely,

        T 1 Reply Last reply
        0
        • C Cyrus Dang

          Thanks, but it appears that SHGetFolderPath doesn't work with param CSIDL_DRIVES. The return string is empty. Sincerely,

          T Offline
          T Offline
          TheGreatAndPowerfulOz
          wrote on last edited by
          #4

          what is the return value of SHGetFolderPath? pszPath [out] Pointer to a null-terminated string of length MAX_PATH which will receive the path. If an error occurs or S_FALSE is returned, this string will be empty. Return Value Returns standard HRESULT codes, including the following: S_FALSE SHGetFolderPathA only. The CSIDL in nFolder is valid, but the folder does not exist. Note that the failure code is different for the ANSI and Unicode versions of this function. E_FAIL SHGetFolderPathW only. The CSIDL in nFolder is valid, but the folder does not exist. Note that the failure code is different for the ANSI and Unicode versions of this function. E_INVALIDARG The CSIDL in nFolder is not valid.

          C 1 Reply Last reply
          0
          • T TheGreatAndPowerfulOz

            what is the return value of SHGetFolderPath? pszPath [out] Pointer to a null-terminated string of length MAX_PATH which will receive the path. If an error occurs or S_FALSE is returned, this string will be empty. Return Value Returns standard HRESULT codes, including the following: S_FALSE SHGetFolderPathA only. The CSIDL in nFolder is valid, but the folder does not exist. Note that the failure code is different for the ANSI and Unicode versions of this function. E_FAIL SHGetFolderPathW only. The CSIDL in nFolder is valid, but the folder does not exist. Note that the failure code is different for the ANSI and Unicode versions of this function. E_INVALIDARG The CSIDL in nFolder is not valid.

            C Offline
            C Offline
            Cyrus Dang
            wrote on last edited by
            #5

            It returns none of them. It's 0x80070057. This is exactly how I use the function: TCHAR szMyCompPath[MAX_PATH]; int rc; rc = SHGetFolderPath(NULL, CSIDL_DRIVES, NULL, 0, szMyCompPath); Do you have any ideas to set the initial directory of CFileDialog to "My Computer"? This is my ultimate goal. Thanks in advance.

            T 1 Reply Last reply
            0
            • C Cyrus Dang

              It returns none of them. It's 0x80070057. This is exactly how I use the function: TCHAR szMyCompPath[MAX_PATH]; int rc; rc = SHGetFolderPath(NULL, CSIDL_DRIVES, NULL, 0, szMyCompPath); Do you have any ideas to set the initial directory of CFileDialog to "My Computer"? This is my ultimate goal. Thanks in advance.

              T Offline
              T Offline
              TheGreatAndPowerfulOz
              wrote on last edited by
              #6

              That error code means "The Parameter Is Incorrect" In looking at your code, the second to last parameter is incorrect. It should be:

              dwFlags
              [in] Flags to specify which path is to be returned. It is used for cases where the folder associated with a CSIDL may be moved or renamed by the user.
              SHGFP_TYPE_CURRENT Return the folder's current path.
              SHGFP_TYPE_DEFAULT Return the folder's default path.

              so change your code to read: TCHAR szMyCompPath[MAX_PATH]; **HRESULT hr; hr** = SHGetFolderPath(NULL, CSIDL_DRIVES, NULL, **SHGFP_TYPE_CURRENT**, szMyCompPath);

              C 1 Reply Last reply
              0
              • T TheGreatAndPowerfulOz

                That error code means "The Parameter Is Incorrect" In looking at your code, the second to last parameter is incorrect. It should be:

                dwFlags
                [in] Flags to specify which path is to be returned. It is used for cases where the folder associated with a CSIDL may be moved or renamed by the user.
                SHGFP_TYPE_CURRENT Return the folder's current path.
                SHGFP_TYPE_DEFAULT Return the folder's default path.

                so change your code to read: TCHAR szMyCompPath[MAX_PATH]; **HRESULT hr; hr** = SHGetFolderPath(NULL, CSIDL_DRIVES, NULL, **SHGFP_TYPE_CURRENT**, szMyCompPath);

                C Offline
                C Offline
                Cyrus Dang
                wrote on last edited by
                #7

                It doesn't help. Actually SHGFP_TYPE_CURRENT is defined 0. Did you succeed in retrieving the "My Computer" path? What did the function return? Sincerely,

                T 2 Replies Last reply
                0
                • C Cyrus Dang

                  It doesn't help. Actually SHGFP_TYPE_CURRENT is defined 0. Did you succeed in retrieving the "My Computer" path? What did the function return? Sincerely,

                  T Offline
                  T Offline
                  TheGreatAndPowerfulOz
                  wrote on last edited by
                  #8

                  hmm, I get the same results. I think the reason is that CSIDL_DRIVES does not have a real directory path. There must be a way of doing it, but I'm not sure what it is. The closest that I can suggest right now is to use CSIDL_DESKTOPDIRECTORY which does have a real path and it shows "My Computer" as an item in that "directory" the only other suggestion I have is to hook the open file dialog and find the id of the "My computer" button and click it programmatically. good luck.

                  1 Reply Last reply
                  0
                  • C Cyrus Dang

                    It doesn't help. Actually SHGFP_TYPE_CURRENT is defined 0. Did you succeed in retrieving the "My Computer" path? What did the function return? Sincerely,

                    T Offline
                    T Offline
                    TheGreatAndPowerfulOz
                    wrote on last edited by
                    #9

                    here's some code that'll do what you want. paste it into a .cpp file and compile and run.

                    #define _WIN32_WINNT 0x0501
                    #define STRICT
                    
                    #include <windows.h>
                    #include <shlobj.h>
                    #include <shfolder.h>
                    #include <tchar.h>
                    
                    UINT_PTR __stdcall OpenFileHook(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
                    {
                        if (msg == WM_NOTIFY)
                        {
                            HWND parent = ::GetParent(hwnd);
                            HWND tbctl = ::GetDlgItem(parent, ctl1); // ctl1 is standard ctl id defined in dlgs.h
                            LRESULT count = ::SendMessage(tbctl, TB_BUTTONCOUNT, 0, 0);
                            LRESULT result;
                            for (int button = 0; button < count; ++button)
                            {
                                TCHAR buttonText[1024];
                                TBBUTTON buttonData;
                                ::ZeroMemory(buttonText, sizeof(buttonText));
                                ::ZeroMemory(&buttonData, sizeof(buttonData));
                                result = ::SendMessage(tbctl, TB_GETBUTTON, (WPARAM)button, (LPARAM)&buttonData);
                                result = ::SendMessage(tbctl, TB_GETBUTTONTEXT, (WPARAM)buttonData.idCommand, (LPARAM)buttonText);
                                if (result != -1 && _tcscmp(_T("My Computer"), buttonText) == 0)
                                {
                                    if (0 == ::SendMessage(tbctl, TB_ISBUTTONCHECKED, (WPARAM)buttonData.idCommand, 0))
                                    {
                                        // tried sending/posting TB_CHECKBUTTON, TB_PRESSBUTTON neither worked
                                        // so, instead, 
                                        // 1. get client rect of toolbar control and
                                        // 2. send WM_LBUTTONDOWN/WM_LBUTTONUP to proper area of toolbar to simulate click
                                        //
                                        RECT rect;
                                        ::ZeroMemory(&rect, 0);
                                        ::GetClientRect(tbctl, &rect);
                    
                                        // simulate click of button
                                        //
                                        ::PostMessage(tbctl, WM_LBUTTONDOWN, (WPARAM)MK_LBUTTON, MAKELPARAM(10, (rect.bottom - rect.top)/count * button));
                                        ::PostMessage(tbctl, WM_LBUTTONUP, (WPARAM)MK_LBUTTON, MAKELPARAM(10, (rect.bottom - rect.top)/count * button));
                                    }
                                }
                            }
                        }
                        return (UINT_PTR)FALSE;
                    }
                    
                    void main()
                    {
                        TCHAR fileBuffer[MAX_PATH * 10];
                        OPENFILENAME ofn;
                        ZeroMemory(&ofn, sizeof ofn);
                        ZeroMemory(fileBuffer, sizeof fileBuffer);
                    
                        ofn.lStructSize = sizeof ofn;
                        ofn.lpstrFile = fileBuffer;
                        ofn.nMaxFile = sizeof(fileBuffer)/sizeof(fileBuffer[0]);
                        ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
                        ofn.lpfnHook = OpenFileHook;
                        BO
                    
                    C 1 Reply Last reply
                    0
                    • T TheGreatAndPowerfulOz

                      here's some code that'll do what you want. paste it into a .cpp file and compile and run.

                      #define _WIN32_WINNT 0x0501
                      #define STRICT
                      
                      #include <windows.h>
                      #include <shlobj.h>
                      #include <shfolder.h>
                      #include <tchar.h>
                      
                      UINT_PTR __stdcall OpenFileHook(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
                      {
                          if (msg == WM_NOTIFY)
                          {
                              HWND parent = ::GetParent(hwnd);
                              HWND tbctl = ::GetDlgItem(parent, ctl1); // ctl1 is standard ctl id defined in dlgs.h
                              LRESULT count = ::SendMessage(tbctl, TB_BUTTONCOUNT, 0, 0);
                              LRESULT result;
                              for (int button = 0; button < count; ++button)
                              {
                                  TCHAR buttonText[1024];
                                  TBBUTTON buttonData;
                                  ::ZeroMemory(buttonText, sizeof(buttonText));
                                  ::ZeroMemory(&buttonData, sizeof(buttonData));
                                  result = ::SendMessage(tbctl, TB_GETBUTTON, (WPARAM)button, (LPARAM)&buttonData);
                                  result = ::SendMessage(tbctl, TB_GETBUTTONTEXT, (WPARAM)buttonData.idCommand, (LPARAM)buttonText);
                                  if (result != -1 && _tcscmp(_T("My Computer"), buttonText) == 0)
                                  {
                                      if (0 == ::SendMessage(tbctl, TB_ISBUTTONCHECKED, (WPARAM)buttonData.idCommand, 0))
                                      {
                                          // tried sending/posting TB_CHECKBUTTON, TB_PRESSBUTTON neither worked
                                          // so, instead, 
                                          // 1. get client rect of toolbar control and
                                          // 2. send WM_LBUTTONDOWN/WM_LBUTTONUP to proper area of toolbar to simulate click
                                          //
                                          RECT rect;
                                          ::ZeroMemory(&rect, 0);
                                          ::GetClientRect(tbctl, &rect);
                      
                                          // simulate click of button
                                          //
                                          ::PostMessage(tbctl, WM_LBUTTONDOWN, (WPARAM)MK_LBUTTON, MAKELPARAM(10, (rect.bottom - rect.top)/count * button));
                                          ::PostMessage(tbctl, WM_LBUTTONUP, (WPARAM)MK_LBUTTON, MAKELPARAM(10, (rect.bottom - rect.top)/count * button));
                                      }
                                  }
                              }
                          }
                          return (UINT_PTR)FALSE;
                      }
                      
                      void main()
                      {
                          TCHAR fileBuffer[MAX_PATH * 10];
                          OPENFILENAME ofn;
                          ZeroMemory(&ofn, sizeof ofn);
                          ZeroMemory(fileBuffer, sizeof fileBuffer);
                      
                          ofn.lStructSize = sizeof ofn;
                          ofn.lpstrFile = fileBuffer;
                          ofn.nMaxFile = sizeof(fileBuffer)/sizeof(fileBuffer[0]);
                          ofn.Flags = OFN_EXPLORER | OFN_ENABLEHOOK;
                          ofn.lpfnHook = OpenFileHook;
                          BO
                      
                      C Offline
                      C Offline
                      Cyrus Dang
                      wrote on last edited by
                      #10

                      Thank you so much. Do we have another way instead of simulating mouse clicks? Sincerely,

                      T 1 Reply Last reply
                      0
                      • C Cyrus Dang

                        Thank you so much. Do we have another way instead of simulating mouse clicks? Sincerely,

                        T Offline
                        T Offline
                        TheGreatAndPowerfulOz
                        wrote on last edited by
                        #11

                        sorry, i tried, you'll have to take it from here.

                        1 Reply Last reply
                        0
                        Reply
                        • Reply as topic
                        Log in to reply
                        • Oldest to Newest
                        • Newest to Oldest
                        • Most Votes


                        • Login

                        • Don't have an account? Register

                        • Login or register to search.
                        • First post
                          Last post
                        0
                        • Categories
                        • Recent
                        • Tags
                        • Popular
                        • World
                        • Users
                        • Groups