Make existing instance of Windows Explorer navigate to specific folder
See the question and my original answer on StackOverflowYou can use the IWebBrowser2::Navigate2 Method, something like this:
int main()
{
CoInitialize(nullptr);
{
// build the optional VARIANT
// https://learn.microsoft.com/en-us/openspecs/windows_protocols/ms-oaut/e93f90dc-8afb-434f-afa9-94f8564fe19b
VARIANT optional;
VariantInit(&optional);
V_VT(&optional) = VT_ERROR;
V_I4(&optional) = DISP_E_PARAMNOTFOUND;
IShellWindows* windows;
if (SUCCEEDED(CoCreateInstance(CLSID_ShellWindows, nullptr, CLSCTX_ALL, IID_PPV_ARGS(&windows))))
{
long count;
windows->get_Count(&count);
for (auto i = 0; i < count; i++)
{
VARIANT index;
VariantInit(&index);
V_VT(&index) = VT_I4;
V_I4(&index) = i;
IDispatch* obj;
if (SUCCEEDED(windows->Item(index, &obj)))
{
IWebBrowser2* app;
if (SUCCEEDED(obj->QueryInterface(IID_PPV_ARGS(&app))))
{
// get a PIDL from a path
// note pidl could represent a virtual folder too
auto pidl = ILCreateFromPath(L"c:\\temp");
if (pidl)
{
// build a VARIANT from the PIDL
VARIANT url;
VariantInit(&url);
if (SUCCEEDED(InitVariantFromBuffer(pidl, ILGetSize(pidl), &url))) // propvarutil.h
{
app->Navigate2(&url, &optional, &optional, &optional, &optional);
}
VariantClear(&url);
ILFree(pidl);
}
app->Release();
}
obj->Release();
}
VariantClear(&index);
}
windows->Release();
}
}
CoUninitialize();
return 0;
}