Click to See Complete Forum and Search --> : Telling Explorer to display a different folder?


iangoldby
August 24th, 2004, 04:14 AM
I'm looking for a way to tell an instance of Explorer that is already running to change directory and display a different folder (of my choice).

My guess is that there is probably a PostMessage() type solution to this. Where might I find documentation of the messages accepted by Windows Explorer?

Or any other ideas?

Cheers,

Ian

sbubis
August 24th, 2004, 05:28 AM
Not sure, but probably Shell helper API and/or Browser Helper Objects are what you need

Bond
August 24th, 2004, 08:40 AM
This will do it. I posted a similar method a while back showing how to get the address line from Internet Explorer.

#include <windows.h>

int main()
{
// The path we want to display in Explorer...
const TCHAR szPath[] = TEXT("c:\\");

// This is the "family tree" from Explorer's main window to the edit control...
const TCHAR szClassNames[][64] =
{
TEXT("WorkerW"),
TEXT("ReBarWindow32"),
TEXT("ComboBoxEx32"),
TEXT("ComboBox"),
TEXT("Edit")
};

// This is the number of generations (windows) in the family...
const int iWindows = sizeof(szClassNames) / sizeof(szClassNames[0]);

// Get the handle to the Explorer's main window (currently on "My Computer")...
HWND hExplorer = FindWindow(NULL, TEXT("My Computer"));

if (hExplorer == NULL)
{
// Couldn't find the main Explorer window...
return -1; // Error
}

// Start with the top level window...
HWND hChild = hExplorer;

// And continue getting children until we reach the edit control...
for (int i = 0; i < iWindows; i++)
if (!(hChild = FindWindowEx(hChild, NULL, szClassNames[i], NULL)))
return -1; // Error

// We now have a handle to Explorer's address bar edit control. Set the text...
SendMessage(hChild, WM_SETTEXT, NULL, (LPARAM) szPath);

// Now, simulate a press of the Enter key in the edit control...
SendMessage(hChild, WM_KEYDOWN, VK_RETURN, NULL);
SendMessage(hChild, WM_CHAR, VK_RETURN, NULL);
SendMessage(hChild, WM_KEYUP, VK_RETURN, NULL);

return 0;
}

sbubis
August 24th, 2004, 09:37 AM
Hi Bond,
Will the way you suggested work if I remove from View the address box? Other windows?
Is it not more robust to use automation?
As far as I know Explorer is a COM server. What we usually call "Explorer" is only a GUI that communicates with this server.

Bond
August 24th, 2004, 09:53 AM
Hi Bond,
Will the way you suggested work if I remove from View the address box? Other windows?
Yep. The edit window has still been created. You just can't see it.

Is it not more robust to use automation?
You could argue that. But then you have to use COM and everything that goes along with that (try/catch, CoInitialize(), HRESULT's, etc.).

However, if Automation is your game, you could do something like this (though this doesn't use an existing instance and it actually loads IE, not Explorer, but they're one and the same, right?

#include <windows.h>
#include <ExDisp.h>
#include <ExDispID.h>

int main()
{
CoInitialize(NULL);

HRESULT hr;
IWebBrowser2* pWebBrowser = NULL;
hr = CoCreateInstance (CLSID_InternetExplorer, NULL, CLSCTX_SERVER, IID_IWebBrowser2, (LPVOID*)&pWebBrowser);

if (SUCCEEDED (hr) && (pWebBrowser != NULL))
{
VARIANT vDummy = {0};
VARIANT vUrl;

vUrl.vt = VT_BSTR;
vUrl.bstrVal = SysAllocString(L"c:\\");

pWebBrowser->Navigate2(&vUrl, &vDummy, &vDummy,&vDummy, &vDummy);
pWebBrowser->put_Visible(VARIANT_TRUE);

VariantClear(&vUrl);
}
else
{
if (pWebBrowser)
pWebBrowser->Release();
}

CoUninitialize();

return 0;
}

sbubis
August 24th, 2004, 09:59 AM
Thank you Bond,
As for me, I would prefer automation.