How to force the initial directory to be 'My Computer'
-
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.
-
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.
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."
-
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."
Thanks, but it appears that SHGetFolderPath doesn't work with param CSIDL_DRIVES. The return string is empty. Sincerely,
-
Thanks, but it appears that SHGetFolderPath doesn't work with param CSIDL_DRIVES. The return string is empty. Sincerely,
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.
-
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.
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. -
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.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);
-
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);
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,
-
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,
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.
-
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,
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
-
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
Thank you so much. Do we have another way instead of simulating mouse clicks? Sincerely,
-
Thank you so much. Do we have another way instead of simulating mouse clicks? Sincerely,
sorry, i tried, you'll have to take it from here.