Esempio n. 1
1
bool CDeskBand::FindPaths()
{
    m_currentDirectory.clear();
    m_selectedItems.clear();
    m_bFilesSelected = false;
    m_bFolderSelected = false;

    if (m_pSite == NULL)
        return false;
    IServiceProvider * pServiceProvider;
    if (SUCCEEDED(m_pSite->QueryInterface(IID_IServiceProvider, (LPVOID*)&pServiceProvider)))
    {
        IShellBrowser * pShellBrowser;
        if (SUCCEEDED(pServiceProvider->QueryService(SID_SShellBrowser, IID_IShellBrowser, (LPVOID*)&pShellBrowser)))
        {
            IShellView * pShellView;
            if (SUCCEEDED(pShellBrowser->QueryActiveShellView(&pShellView)))
            {
                IFolderView * pFolderView;
                if (SUCCEEDED(pShellView->QueryInterface(IID_IFolderView, (LPVOID*)&pFolderView)))
                {
                    // hooray! we got the IFolderView interface!
                    // that means the explorer is active and well :)

                    // but we also need the IShellFolder interface because
                    // we need its GetCurFolder() method
                    IPersistFolder2 * pPersistFolder;
                    if (SUCCEEDED(pFolderView->GetFolder(IID_IPersistFolder2, (LPVOID*)&pPersistFolder)))
                    {
                        LPITEMIDLIST folderpidl;
                        if (SUCCEEDED(pPersistFolder->GetCurFolder(&folderpidl)))
                        {
                            // we have the current folder
                            TCHAR buf[MAX_PATH] = {0};
                            // find the path of the folder
                            if (SHGetPathFromIDList(folderpidl, buf))
                            {
                                m_currentDirectory = buf;
                            }
                            // if m_currentDirectory is empty here, that means
                            // the current directory is a virtual path

                            IShellFolder * pShellFolder;
                            if (SUCCEEDED(pPersistFolder->QueryInterface(IID_IShellFolder, (LPVOID*)&pShellFolder)))
                            {
                                // if there was a new folder created but not found to set into editing mode,
                                // we try here to do that
                                if (!m_newfolderPidls.empty())
                                {
                                    int nCount2 = 0;
                                    IShellFolder * pShellFolder;
                                    if (SUCCEEDED(pPersistFolder->QueryInterface(IID_IShellFolder, (LPVOID*)&pShellFolder)))
                                    {
                                        if (SUCCEEDED(pFolderView->ItemCount(SVGIO_ALLVIEW, &nCount2)))
                                        {
                                            for (int i=0; i<nCount2; ++i)
                                            {
                                                LPITEMIDLIST pidl;
                                                pFolderView->Item(i, &pidl);
                                                bool bFound = false;
                                                for (std::vector<LPITEMIDLIST>::iterator it = m_newfolderPidls.begin(); it != m_newfolderPidls.end(); ++it)
                                                {
                                                    HRESULT hr = pShellFolder->CompareIDs(0, pidl, *it);
                                                    if (HRESULT_CODE(hr) == 0)
                                                    {
                                                        // this item was there before, so it's not the new folder
                                                        CoTaskMemFree(*it);
                                                        m_newfolderPidls.erase(it);
                                                        bFound = true;
                                                        break;
                                                    }
                                                }
                                                if (!bFound)
                                                {
                                                    pShellView->SelectItem(pidl, SVSI_EDIT);
                                                }
                                                CoTaskMemFree(pidl);
                                            }
                                        }
                                        if ((nCount2)||(m_newfolderTimeoutCounter-- <= 0))
                                        {
                                            m_newfolderTimeoutCounter = 0;
                                            for (std::vector<LPITEMIDLIST>::iterator it = m_newfolderPidls.begin(); it != m_newfolderPidls.end(); ++it)
                                            {
                                                CoTaskMemFree(*it);
                                            }
                                            m_newfolderPidls.clear();
                                        }
                                        pShellFolder->Release();
                                    }

                                }
                                // find all selected items
                                IEnumIDList * pEnum;
                                if (SUCCEEDED(pFolderView->Items(SVGIO_SELECTION, IID_IEnumIDList, (LPVOID*)&pEnum)))
                                {
                                    LPITEMIDLIST pidl;
                                    WCHAR buf[MAX_PATH] = {0};
                                    ULONG fetched = 0;
                                    ULONG attribs = 0;
                                    do
                                    {
                                        pidl = NULL;
                                        if (SUCCEEDED(pEnum->Next(1, &pidl, &fetched)))
                                        {
                                            if (fetched)
                                            {
                                                // the pidl we get here is relative!
                                                attribs = SFGAO_FILESYSTEM|SFGAO_FOLDER;
                                                if (SUCCEEDED(pShellFolder->GetAttributesOf(1, (LPCITEMIDLIST*)&pidl, &attribs)))
                                                {
                                                    if (attribs & SFGAO_FILESYSTEM)
                                                    {
                                                        // create an absolute pidl with the pidl we got above
                                                        LPITEMIDLIST abspidl = CPidl::Append(folderpidl, pidl);
                                                        if (abspidl)
                                                        {
                                                            if (SHGetPathFromIDList(abspidl, buf))
                                                            {
                                                                m_selectedItems[std::wstring(buf)] = attribs;
                                                                if (m_currentDirectory.empty())
                                                                {
                                                                    // remove the last part of the path of the selected item
                                                                    WCHAR * pSlash = _tcsrchr(buf, '\\');
                                                                    if (pSlash)
                                                                        *pSlash = 0;
                                                                    m_currentDirectory = std::wstring(buf);
                                                                }
                                                            }
                                                            CoTaskMemFree(abspidl);
                                                        }
                                                        if (attribs & SFGAO_FOLDER)
                                                            m_bFolderSelected = true;
                                                        else
                                                            m_bFilesSelected = true;
                                                    }
                                                }
                                            }
                                            CoTaskMemFree(pidl);
                                        }
                                    } while(fetched);
                                    pEnum->Release();
                                }
                                pShellFolder->Release();
                            }
                            CoTaskMemFree(folderpidl);
                        }
                        pPersistFolder->Release();
                    }
                    pFolderView->Release();
                }
                pShellView->Release();
            }
            pShellBrowser->Release();
        }
        pServiceProvider->Release();
    }
    return ((!m_currentDirectory.empty()) || (!m_selectedItems.empty()));
}
Esempio n. 2
0
// @pymethod |PyIShellBrowser|SetMenuSB|Attaches a shared menu to a shell view window
PyObject *PyIShellBrowser::SetMenuSB(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	HMENU hmenuShared;
	HOLEMENU holemenuRes;
	HWND hwndActiveObject;
	PyObject *obhmenuShared, *obholemenuRes, *obhwndActiveObject;
	if (!PyArg_ParseTuple(args, "OOO:SetMenuSB",
		&obhmenuShared,			// @pyparm <o PyHANDLE>|hmenuShared||Handle to the shared menu
		&obholemenuRes,			// @pyparm <o PyHANDLE>|holemenuRes||Reserved, use only None (or 0)
		&obhwndActiveObject))	// @pyparm <o PyHANDLE>|hwndActiveObject||Handle to the shell window
		return NULL;
	if (!PyWinObject_AsHANDLE(obhmenuShared, (HANDLE *)&hmenuShared))
		return NULL;
	if (!PyWinObject_AsHANDLE(obholemenuRes, (HANDLE *)&holemenuRes))
		return NULL;
	if (!PyWinObject_AsHANDLE(obhwndActiveObject, (HANDLE *)&hwndActiveObject))
		return NULL;

	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->SetMenuSB( hmenuShared, holemenuRes, hwndActiveObject );
	PY_INTERFACE_POSTCALL;

	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	Py_INCREF(Py_None);
	return Py_None;

}
Esempio n. 3
0
static int MyShellDispatch_ShellDispatchProc(const shell_dispatch_handler_t handler, void *const data)
{
	int iSuccess = SHELL_DISPATCH_FAILED;

	IShellWindows *psw = NULL;
	HRESULT hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&psw));
	if(SUCCEEDED(hr))
	{
		HWND desktopHwnd = 0;
		IDispatch* pdisp = NULL;
		variant_t vEmpty;
		if(S_OK == psw->FindWindowSW(vEmpty, vEmpty, SWC_DESKTOP, (long*)&desktopHwnd, SWFO_NEEDDISPATCH, &pdisp))
		{
			if(VALID_HANDLE(desktopHwnd))
			{
				IShellBrowser *psb;
				hr = IUnknown_QueryService(pdisp, SID_STopLevelBrowser, IID_PPV_ARGS(&psb));
				if(SUCCEEDED(hr))
				{
					IShellView *psv = NULL;
					hr = psb->QueryActiveShellView(&psv);
					if(SUCCEEDED(hr))
					{
						IDispatch *pdispBackground = NULL;
						HRESULT hr = psv->GetItemObject(SVGIO_BACKGROUND, IID_PPV_ARGS(&pdispBackground));
						if(SUCCEEDED(hr))
						{
							MyShellDispatch_AllowSetForegroundWindow(desktopHwnd);
							IShellFolderViewDual *psfvd = NULL;
							HRESULT hr = pdispBackground->QueryInterface(IID_PPV_ARGS(&psfvd));
							if(SUCCEEDED(hr))
							{
								IDispatch *pdisp = NULL;
								hr = psfvd->get_Application(&pdisp);
								if(SUCCEEDED(hr))
								{
									IShellDispatch2 *pShellDispatch;
									hr = pdisp->QueryInterface(IID_PPV_ARGS(&pShellDispatch));
									if(SUCCEEDED(hr))
									{
										iSuccess = handler(pShellDispatch, data);
									}
									RELEASE_OBJ(pdisp);
								}
								RELEASE_OBJ(psfvd);
							}
							RELEASE_OBJ(pdispBackground);
						}
						RELEASE_OBJ(psv);
					}
					RELEASE_OBJ(psb);
				}
			}
			RELEASE_OBJ(pdisp);
		}
		RELEASE_OBJ(psw);
	}

	return iSuccess;
}
Esempio n. 4
0
// @pymethod int|PyIShellBrowser|SendControlMsg|Sends a control msg to browser's toolbar or status bar
PyObject *PyIShellBrowser::SendControlMsg(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	// @pyparm int|id||shellcon.FCW_TOOLBAR or FCW_STATUS
	// @pyparm int|uMsg||The message to send
	// @pyparm int|wParam||Value is dependent on the message
	// @pyparm long|lParam||Value is dependent on the message
	UINT id;
	UINT uMsg;
	PyObject *obwparam, *oblparam;
	if ( !PyArg_ParseTuple(args, "IIOO:SendControlMsg", &id, &uMsg, &obwparam, &oblparam) )
		return NULL;
	WPARAM wParam;
	LPARAM lParam;
	if (!PyWinObject_AsPARAM(obwparam, &wParam))
		return NULL;
	// WPARAM and LPARAM are defined as UINT_PTR and LONG_PTR, so they can't be used interchangeably without a cast
	if (!PyWinObject_AsPARAM(oblparam, (WPARAM *)&lParam))
		return NULL;
	
	HRESULT hr;
	LRESULT ret;
	PY_INTERFACE_PRECALL;
	hr = pISB->SendControlMsg( id, uMsg, wParam, lParam, &ret );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	// LRESULT is defined as LONG_PTR.  This should work for either 32 or 64 bit.
	return PyLong_FromLongLong(ret);
}
Esempio n. 5
0
// @pymethod |PyIShellBrowser|SetToolbarItems|Adds toolbar buttons to the browser's toolbar
PyObject *PyIShellBrowser::SetToolbarItems(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	LPTBBUTTONSB lpButtons;
	PyObject *oblpButtons;
	
	
	UINT nButtons;
	UINT uFlags;
	if (!PyArg_ParseTuple(args, "OI:SetToolbarItems",
		&oblpButtons,	// @pyparm <o PyLPTBBUTTONSB>|lpButtons||Sequence of tuples describing the buttons to be added
		&uFlags))		// @pyparm int|uFlags||Indicates button positions, combination of shellcon.FCT_*
		return NULL;
	if (!PyObject_AsTBBUTTONs( oblpButtons, &lpButtons, &nButtons ))
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->SetToolbarItems( lpButtons, nButtons, uFlags );
	PyObject_FreeTBBUTTONs(lpButtons, nButtons);
	PY_INTERFACE_POSTCALL;

	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	Py_INCREF(Py_None);
	return Py_None;

}
Esempio n. 6
0
// @pymethod |PyIShellBrowser|RemoveMenusSB|Asks container to remove any items it added to a composite menu
PyObject *PyIShellBrowser::RemoveMenusSB(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	PyObject *obhmenuShared;
	HMENU hmenuShared;
	if (!PyArg_ParseTuple(args, "O:RemoveMenusSB", 
		&obhmenuShared))	// @pyparm <o PyHANDLE>|hmenuShared||Handle to the composite menu
		return NULL;
	if (!PyWinObject_AsHANDLE(obhmenuShared, (HANDLE *)&hmenuShared))
		return NULL;

	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->RemoveMenusSB( hmenuShared );

	PY_INTERFACE_POSTCALL;

	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	Py_INCREF(Py_None);
	return Py_None;

}
Esempio n. 7
0
void GetExplorerWindows(std::vector<PairHwndPath>& windows, BOOL needPaths) {
    IShellWindows *psw;
    if(SUCCEEDED(CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_ALL, IID_IShellWindows, (void**)&psw))) {
        VARIANT v;
        V_VT(&v) = VT_I4;
        IDispatch* pdisp;
        for(V_I4(&v) = 0; psw->Item(v, &pdisp) == S_OK; V_I4(&v)++) {
            IWebBrowserApp *pwba;
            if(SUCCEEDED(pdisp->QueryInterface(IID_IWebBrowserApp, (void**)&pwba))) {
                PairHwndPath pair;
                if(SUCCEEDED(pwba->get_HWND((LONG_PTR*)&pair.hwnd))) {
                    IServiceProvider *psp;
                    if(needPaths && SUCCEEDED(pwba->QueryInterface(IID_IServiceProvider, (void**)&psp))) {
                        IShellBrowser *psb;
                        if(SUCCEEDED(psp->QueryService(SID_STopLevelBrowser, IID_IShellBrowser, (void**)&psb))) {
                            IShellView *psv;
                            if(SUCCEEDED(psb->QueryActiveShellView(&psv))) {
                                IFolderView *pfv;
                                if(SUCCEEDED(psv->QueryInterface(IID_IFolderView, (void**)&pfv))) {
                                    IPersistFolder2 *ppf2;
                                    if(SUCCEEDED(pfv->GetFolder(IID_IPersistFolder2, (void**)&ppf2))) {
                                        LPITEMIDLIST pidlFolder;
                                        if(SUCCEEDED(ppf2->GetCurFolder(&pidlFolder))) {
                                            if(!SHGetPathFromIDList(pidlFolder, pair.path)) {
                                                IShellFolder* psf;
                                                LPCITEMIDLIST pidlLast;
                                                if(SUCCEEDED(SHBindToParent(pidlFolder, IID_IShellFolder, (void**)&psf, &pidlLast))) {
                                                    STRRET strret;
                                                    if(SUCCEEDED(psf->GetDisplayNameOf(pidlLast, 0x8000, &strret))) {
                                                        StrRetToBuf(&strret, pidlLast, pair.path, MAX_PATH);
                                                    }
                                                    else {
                                                        pair.path[0] = 0;
                                                    }
                                                    psf->Release();
                                                }
                                            }
                                            CoTaskMemFree(pidlFolder);
                                        }
                                        ppf2->Release();
                                    }
                                    pfv->Release();
                                }
                                psv->Release();
                            }
                            psb->Release();
                        }
                        psp->Release();
                    }
                    windows.push_back(pair);
                }
                pwba->Release();
            }
            pdisp->Release();
        }
        psw->Release();
    }
}
Esempio n. 8
0
static HRESULT GetDesktopShellView(
    __in REFIID riid,
    __out void **ppv
    )
{
    HRESULT hr = S_OK;
    IShellWindows* psw = NULL;
    HWND hwnd = NULL;
    IDispatch* pdisp = NULL;
    VARIANT vEmpty = {}; // VT_EMPTY
    IShellBrowser* psb = NULL;
    IShellFolder* psf = NULL;
    IShellView* psv = NULL;

    // use the shell view for the desktop using the shell windows automation to find the 
    // desktop web browser and then grabs its view
    // returns IShellView, IFolderView and related interfaces
    hr = ::CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&psw));
    ExitOnFailure(hr, "Failed to get shell view.");

    hr = psw->FindWindowSW(&vEmpty, &vEmpty, SWC_DESKTOP, (long*)&hwnd, SWFO_NEEDDISPATCH, &pdisp);
    if (S_OK == hr)
    {
        hr = IUnknown_QueryService(pdisp, SID_STopLevelBrowser, IID_PPV_ARGS(&psb));
        ExitOnFailure(hr, "Failed to get desktop window.");

        hr = psb->QueryActiveShellView(&psv);
        ExitOnFailure(hr, "Failed to get active shell view.");

        hr = psv->QueryInterface(riid, ppv);
        ExitOnFailure(hr, "Failed to query for the desktop shell view.");
    }
    else if (S_FALSE == hr)
    {
        //Windows XP
        hr = SHGetDesktopFolder(&psf);
        ExitOnFailure(hr, "Failed to get desktop folder.");

        hr = psf->CreateViewObject(NULL, IID_IShellView, ppv);
        ExitOnFailure(hr, "Failed to query for the desktop shell view.");
    }
    else
    {
        ExitOnFailure(hr, "Failed to get desktop window.");
    }

LExit:
    ReleaseObject(psv);
    ReleaseObject(psb);
    ReleaseObject(psf);
    ReleaseObject(pdisp);
    ReleaseObject(psw);

    return hr;
}
Esempio n. 9
0
// @pymethod <o PyIShellView>|PyIShellBrowser|QueryActiveShellView|Returns the currently displayed view
PyObject *PyIShellBrowser::QueryActiveShellView(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	IShellView * ppshv;
	if ( !PyArg_ParseTuple(args, ":QueryActiveShellView") )
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->QueryActiveShellView( &ppshv );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	return PyCom_PyObjectFromIUnknown(ppshv, IID_IShellView, FALSE);
}
Esempio n. 10
0
// @pymethod |PyIShellBrowser|GetControlWindow|Returns a handle to one of the browser's control elements
PyObject *PyIShellBrowser::GetControlWindow(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	// @pyparm int|id||One of shellcon.FCW_* values
	UINT id;
	if ( !PyArg_ParseTuple(args, "I:GetControlWindow", &id) )
		return NULL;
	HWND hwnd;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->GetControlWindow( id, &hwnd );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	return PyWinLong_FromHANDLE(hwnd);
}
Esempio n. 11
0
// @pymethod <o PyIStream>|PyIShellBrowser|GetViewStateStream|Returns a stream that can be used to access view state information
PyObject *PyIShellBrowser::GetViewStateStream(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	// @pyparm int|grfMode||Read/write mode, one of STGM_READ,STGM_WRITE,STGM_READWRITE
	DWORD grfMode;
	IStream * ppStrm;
	if ( !PyArg_ParseTuple(args, "l:GetViewStateStream", &grfMode) )
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->GetViewStateStream( grfMode, &ppStrm );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	return PyCom_PyObjectFromIUnknown(ppStrm, IID_IStream, FALSE);
}
Esempio n. 12
0
// @pymethod |PyIShellBrowser|EnableModelessSB|Enables or disables modeless dialogs
PyObject *PyIShellBrowser::EnableModelessSB(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	BOOL fEnable;
	if (!PyArg_ParseTuple(args, "i:EnableModelessSB", 
		&fEnable))	// @pyparm boolean|fEnable||Use True to enable or False to disable modeless dialog boxes
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->EnableModelessSB( fEnable );

	PY_INTERFACE_POSTCALL;

	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	Py_INCREF(Py_None);
	return Py_None;

}
Esempio n. 13
0
// @pymethod |PyIShellBrowser|SetStatusTextSB|Sets the status text in view's status bar
PyObject *PyIShellBrowser::SetStatusTextSB(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	PyObject *obStatusText;
	TmpWCHAR StatusText;
	if (!PyArg_ParseTuple(args, "O:SetStatusTextSB",
		&obStatusText))	// @pyparm str|pszStatusText||New status to be displayed
		return NULL;
	if (!PyWinObject_AsWCHAR(obStatusText, &StatusText))
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->SetStatusTextSB(StatusText );
	PY_INTERFACE_POSTCALL;

	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	Py_INCREF(Py_None);
	return Py_None;
}
Esempio n. 14
0
// @pymethod int|PyIShellView|CreateViewWindow|Description of CreateViewWindow.
PyObject *PyIShellView::CreateViewWindow(PyObject *self, PyObject *args)
{
	IShellView *pISV = GetI(self);
	if ( pISV == NULL )
		return NULL;
	// @pyparm <o PyIShellView>|psvPrevious||Description for psvPrevious
	FOLDERSETTINGS fs;
	PyObject *obpfs;
	// @pyparm (int, int)|pfs||Description for pfs
	// @pyparm <o PyIShellBrowser>|psb||Description for psb
	RECT rcView;
	PyObject *obprcView;
	// @pyparm (int, int, int, int)|prcView||Description for prcView
	PyObject *obpsvPrevious;
	PyObject *obpsb;
	IShellView *psvPrevious;
	IShellBrowser *psb;
	if ( !PyArg_ParseTuple(args, "OOOO:CreateViewWindow", &obpsvPrevious, &obpfs, &obpsb, &obprcView) )
		return NULL;
	BOOL bPythonIsHappy = TRUE;
	if (bPythonIsHappy && !PyCom_InterfaceFromPyInstanceOrObject(obpsvPrevious, IID_IShellView, (void **)&psvPrevious, TRUE /* bNoneOK */))
		 bPythonIsHappy = FALSE;
	if (bPythonIsHappy && !PyObject_AsFOLDERSETTINGS( obpfs, &fs )) bPythonIsHappy = FALSE;
	if (bPythonIsHappy && !PyCom_InterfaceFromPyInstanceOrObject(obpsb, IID_IShellBrowser, (void **)&psb, TRUE /* bNoneOK */))
		 bPythonIsHappy = FALSE;
	if (bPythonIsHappy && !PyObject_AsRECT( obprcView, &rcView )) bPythonIsHappy = FALSE;
	if (!bPythonIsHappy) return NULL;
	HWND hWnd;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISV->CreateViewWindow( psvPrevious, &fs, psb, &rcView, &hWnd );
	if (psvPrevious) psvPrevious->Release();
	if (psb) psb->Release();
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISV, IID_IShellView );
	// @rdesc The result is an integer handle to the new window.
	return PyWinLong_FromHANDLE(hWnd);
}
Esempio n. 15
0
// @pymethod |PyIShellBrowser|OnViewWindowActive|Callback triggered when a view window is activated
PyObject *PyIShellBrowser::OnViewWindowActive(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	// @pyparm <o PyIShellView>|pshv||The activated view object
	PyObject *obpshv;
	IShellView *pshv;
	if ( !PyArg_ParseTuple(args, "O:OnViewWindowActive", &obpshv) )
		return NULL;
	if (!PyCom_InterfaceFromPyInstanceOrObject(obpshv, IID_IShellView, (void **)&pshv, TRUE /* bNoneOK */))
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->OnViewWindowActive( pshv );
	if (pshv) pshv->Release();
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	Py_INCREF(Py_None);
	return Py_None;

}
Esempio n. 16
0
// @pymethod |PyIShellBrowser|TranslateAcceleratorSB|Translates keystrokes used as menu item activators
PyObject *PyIShellBrowser::TranslateAcceleratorSB(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	MSG msg;
	PyObject *obpmsg;
	WORD wID;
	if (!PyArg_ParseTuple(args, "OH:TranslateAcceleratorSB",
		&obpmsg,	// @pyparm <o PyMSG>|pmsg||Keystroke message to be translated
		&wID))		// @pyparm int|wID||Menu command id for the keystroke
		return NULL;
	if (!PyObject_AsMSG(obpmsg, &msg))
		return NULL;

	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->TranslateAcceleratorSB( &msg, wID );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	Py_INCREF(Py_None);
	return Py_None;
}
Esempio n. 17
0
// @pymethod <o PyOLEMENUGROUPWIDTHS>|PyIShellBrowser|InsertMenusSB|Updates a composite menu with container's options
PyObject *PyIShellBrowser::InsertMenusSB(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	OLEMENUGROUPWIDTHS menuWidths;
	PyObject *obMenuWidths, *obhmenuShared;
	HMENU hmenuShared;
	if ( !PyArg_ParseTuple(args, "OO:InsertMenusSB",
		&obhmenuShared,		// @pyparm <o PyHANDLE>|hmenuShared||Newly created menu that contains no items
		&obMenuWidths))		// @pyparm <o PyOLEMENUGROUPWIDTHS>|lpMenuWidths||Tuple of 6 ints.  Items 0,2,and 4 are updated when the tuple is returned.
		return NULL;
	if (!PyWinObject_AsHANDLE(obhmenuShared, (HANDLE *)&hmenuShared))
		return NULL;
	if (!PyObject_AsOLEMENUGROUPWIDTHS( obMenuWidths, &menuWidths))
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->InsertMenusSB( hmenuShared, &menuWidths );
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	return PyObject_FromOLEMENUGROUPWIDTHS(&menuWidths);
}
HRESULT GetShellViewForDesktop(REFIID riid, void **ppv)
{
    *ppv = NULL;

    IShellWindows *psw;
    HRESULT hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&psw));
    if (SUCCEEDED(hr))
    {
        HWND hwnd;
        IDispatch* pdisp;
        VARIANT vEmpty = {}; // VT_EMPTY
        if (S_OK == psw->FindWindowSW(&vEmpty, &vEmpty, SWC_DESKTOP, (long*)&hwnd, SWFO_NEEDDISPATCH, &pdisp))
        {
            IShellBrowser *psb;
            hr = IUnknown_QueryService(pdisp, SID_STopLevelBrowser, IID_PPV_ARGS(&psb));
            if (SUCCEEDED(hr))
            {
                IShellView *psv;
                hr = psb->QueryActiveShellView(&psv);
                if (SUCCEEDED(hr))
                {
                    hr = psv->QueryInterface(riid, ppv);
                    psv->Release();
                }
                psb->Release();
            }
            pdisp->Release();
        }
        else
        {
            hr = E_FAIL;
        }
        psw->Release();
    }
    return hr;
}
Esempio n. 19
0
// @pymethod |PyIShellBrowser|BrowseObject|Navigates to a different location
PyObject *PyIShellBrowser::BrowseObject(PyObject *self, PyObject *args)
{
	IShellBrowser *pISB = GetI(self);
	if ( pISB == NULL )
		return NULL;
	PyObject *obpidl;
	LPITEMIDLIST pidl;
	UINT wFlags;
	if ( !PyArg_ParseTuple(args, "OI:BrowseObject",
		&obpidl,	// @pyparm <o PyIDL>|pidl||Item id list that specifies the new browse location, can be None
		&wFlags))	// @pyparm int|wFlags||Combination of shellcon.SBSP_* flags
		return NULL;
	if (!PyObject_AsPIDL(obpidl, &pidl, TRUE))
		return NULL;
	HRESULT hr;
	PY_INTERFACE_PRECALL;
	hr = pISB->BrowseObject( pidl, wFlags );
	PyObject_FreePIDL(pidl);
	PY_INTERFACE_POSTCALL;
	if ( FAILED(hr) )
		return PyCom_BuildPyException(hr, pISB, IID_IShellBrowser );
	Py_INCREF(Py_None);
	return Py_None;
}
Esempio n. 20
0
void CDeskBand::Rename(HWND hwnd, const std::map<std::wstring, ULONG>& items)
{
    // fill the list of selected file/foldernames
    m_filelist.clear();
    if (items.size() > 1)
    {
        for (std::map<std::wstring, ULONG>::const_iterator it = items.begin(); it != items.end(); ++it)
        {
            size_t pos = it->first.find_last_of('\\');
            if (pos != std::wstring::npos)
            {
                m_filelist.insert(it->first.substr(pos+1));
            }
        }
    }
    else if (items.size() == 1)
    {
        for (std::map<std::wstring, ULONG>::const_iterator it = items.begin(); it != items.end(); ++it)
        {
            size_t pos = it->first.find_last_of('\\');
            if (pos != std::wstring::npos)
            {
                m_filelist.insert(it->first.substr(pos+1));
            }
        }
    }
    else
    {
        // no files or only one file were selected.
        // use all files and folders in the current folder instead
        IServiceProvider * pServiceProvider = NULL;
        if (SUCCEEDED(GetIServiceProvider(hwnd, &pServiceProvider)))
        {
            IShellBrowser * pShellBrowser;
            if (SUCCEEDED(pServiceProvider->QueryService(SID_SShellBrowser, IID_IShellBrowser, (LPVOID*)&pShellBrowser)))
            {
                IShellView * pShellView;
                if (SUCCEEDED(pShellBrowser->QueryActiveShellView(&pShellView)))
                {
                    IFolderView * pFolderView;
                    if (SUCCEEDED(pShellView->QueryInterface(IID_IFolderView, (LPVOID*)&pFolderView)))
                    {
                        // hooray! we got the IFolderView interface!
                        // that means the explorer is active and well :)

                        // but we also need the IShellFolder interface because
                        // we need its GetCurFolder() method
                        IPersistFolder2 * pPersistFolder;
                        if (SUCCEEDED(pFolderView->GetFolder(IID_IPersistFolder2, (LPVOID*)&pPersistFolder)))
                        {
                            LPITEMIDLIST folderpidl;
                            if (SUCCEEDED(pPersistFolder->GetCurFolder(&folderpidl)))
                            {
                                // we have the current folder
                                TCHAR buf[MAX_PATH] = {0};
                                // find the path of the folder
                                if (SHGetPathFromIDList(folderpidl, buf))
                                {
                                    m_currentDirectory = buf;
                                }
                                // if m_currentDirectory is empty here, that means
                                // the current directory is a virtual path

                                IShellFolder * pShellFolder;
                                if (SUCCEEDED(pPersistFolder->QueryInterface(IID_IShellFolder, (LPVOID*)&pShellFolder)))
                                {
                                    // find all selected items
                                    IEnumIDList * pEnum;
                                    if (SUCCEEDED(pFolderView->Items(SVGIO_ALLVIEW, IID_IEnumIDList, (LPVOID*)&pEnum)))
                                    {
                                        LPITEMIDLIST pidl;
                                        WCHAR buf[MAX_PATH] = {0};
                                        ULONG fetched = 0;
                                        ULONG attribs = 0;
                                        do
                                        {
                                            pidl = NULL;
                                            if (SUCCEEDED(pEnum->Next(1, &pidl, &fetched)))
                                            {
                                                if (fetched)
                                                {
                                                    // the pidl we get here is relative!
                                                    attribs = SFGAO_FILESYSTEM|SFGAO_FOLDER;
                                                    if (SUCCEEDED(pShellFolder->GetAttributesOf(1, (LPCITEMIDLIST*)&pidl, &attribs)))
                                                    {
                                                        if (attribs & SFGAO_FILESYSTEM)
                                                        {
                                                            // create an absolute pidl with the pidl we got above
                                                            LPITEMIDLIST abspidl = CPidl::Append(folderpidl, pidl);
                                                            if (abspidl)
                                                            {
                                                                if (SHGetPathFromIDList(abspidl, buf))
                                                                {
                                                                    std::wstring p = buf;
                                                                    size_t pos = p.find_last_of('\\');
                                                                    if (pos != std::wstring::npos)
                                                                    {
                                                                        m_filelist.insert(p.substr(pos+1));
                                                                    }
                                                                }
                                                                CoTaskMemFree(abspidl);
                                                            }
                                                        }
                                                    }
                                                }
                                                CoTaskMemFree(pidl);
                                            }
                                        } while(fetched);
                                        pEnum->Release();
                                    }
                                    pShellFolder->Release();
                                }
                                CoTaskMemFree(folderpidl);
                            }
                            pPersistFolder->Release();
                        }
                        pFolderView->Release();
                    }
                    pShellView->Release();
                }
                pShellBrowser->Release();
            }
            pServiceProvider->Release();
        }
    }

    // show the rename dialog
    m_bDialogShown = TRUE;
    CRenameDlg dlg(hwnd);
    dlg.SetFileList(m_filelist);
    if (dlg.DoModal(g_hInst, IDD_RENAMEDLG, hwnd, NULL) == IDOK)
    {
        try
        {
            const std::tr1::wregex regCheck(dlg.GetMatchString(), dlg.GetRegexFlags());
            NumberReplaceHandler handler(dlg.GetReplaceString());

            // start renaming the files
            IServiceProvider * pServiceProvider = NULL;
            if (SUCCEEDED(GetIServiceProvider(hwnd, &pServiceProvider)))
            {
                IShellBrowser * pShellBrowser;
                if (SUCCEEDED(pServiceProvider->QueryService(SID_SShellBrowser, IID_IShellBrowser, (LPVOID*)&pShellBrowser)))
                {
                    IShellView * pShellView;
                    if (SUCCEEDED(pShellBrowser->QueryActiveShellView(&pShellView)))
                    {
                        IFolderView * pFolderView;
                        if (SUCCEEDED(pShellView->QueryInterface(IID_IFolderView, (LPVOID*)&pFolderView)))
                        {
                            // hooray! we got the IFolderView interface!
                            // that means the explorer is active and well :)

                            // but we also need the IShellFolder interface because
                            // we need its GetDisplayNameOf() method
                            IPersistFolder2 * pPersistFolder;
                            if (SUCCEEDED(pFolderView->GetFolder(IID_IPersistFolder2, (LPVOID*)&pPersistFolder)))
                            {
                                IShellFolder * pShellFolder;
                                if (SUCCEEDED(pPersistFolder->QueryInterface(IID_IShellFolder, (LPVOID*)&pShellFolder)))
                                {
                                    // our next task is to enumerate all the
                                    // items in the folder view and select those
                                    // which match the text in the edit control

                                    int nCount = 0;
                                    if (SUCCEEDED(pFolderView->ItemCount(SVGIO_ALLVIEW, &nCount)))
                                    {
                                        for (int i=0; i<nCount; ++i)
                                        {
                                            LPITEMIDLIST pidl;
                                            if (SUCCEEDED(pFolderView->Item(i, &pidl)))
                                            {
                                                STRRET str;
                                                if (SUCCEEDED(pShellFolder->GetDisplayNameOf(pidl,
                                                    // SHGDN_FORPARSING needed to get the extensions even if they're not shown
                                                    SHGDN_INFOLDER|SHGDN_FORPARSING,
                                                    &str)))
                                                {
                                                    TCHAR dispname[MAX_PATH];
                                                    StrRetToBuf(&str, pidl, dispname, _countof(dispname));

                                                    std::wstring replaced;
                                                    try
                                                    {
                                                        std::wstring sDispName = dispname;
                                                        // check if the item is in the list of selected items
                                                        if (m_filelist.find(sDispName) != m_filelist.end())
                                                        {
                                                            replaced = std::tr1::regex_replace(sDispName, regCheck, dlg.GetReplaceString());
                                                            replaced = handler.ReplaceCounters(replaced);
                                                            if (replaced.compare(sDispName))
                                                            {
                                                                ITEMIDLIST * pidlrenamed;
                                                                pShellFolder->SetNameOf(NULL, pidl, replaced.c_str(), SHGDN_FORPARSING|SHGDN_INFOLDER, &pidlrenamed);
                                                                // if the rename was successful, select the renamed item
                                                                if (pidlrenamed)
                                                                    pFolderView->SelectItem(i, SVSI_CHECK|SVSI_SELECT);
                                                            }
                                                        }
                                                    }
                                                    catch (std::exception)
                                                    {
                                                    }
                                                }
                                                CoTaskMemFree(pidl);
                                            }
                                        }
                                    }
                                    pShellFolder->Release();
                                }
                                pPersistFolder->Release();
                            }
                            pFolderView->Release();
                        }
                        pShellView->Release();
                    }
                    pShellBrowser->Release();
                }
                pServiceProvider->Release();
            }
        }
        catch (std::exception)
        {
        }
    }
    m_bDialogShown = FALSE;
}
Esempio n. 21
0
int ShellExecAsUser(const TCHAR *pcOperation, const TCHAR *pcFileName, const TCHAR *pcParameters, const HWND parentHwnd)
{
	/*BOOL bRet;
	HANDLE hToken;
	HANDLE hNewToken;

	// Notepad is used as an example
	//WCHAR wszProcessName[MAX_PATH] = L"C:\\Windows\\Notepad.exe";

	// Low integrity SID: 0x1000 = 4096. To use Medium integrity, use 0x2000 = 8192
	WCHAR wszIntegritySid[20] = L"S-1-16-4096";
	PSID pIntegritySid = NULL;

	TOKEN_MANDATORY_LABEL TIL = {0};
	PROCESS_INFORMATION ProcInfo = {0};
	STARTUPINFO StartupInfo = {0};
	ULONG ExitCode = 0;

	if (OpenProcessToken(GetCurrentProcess(), MAXIMUM_ALLOWED, &hToken)) {
		if (DuplicateTokenEx(hToken, MAXIMUM_ALLOWED, NULL, SecurityImpersonation, TokenPrimary, &hNewToken)) {
			if (ConvertStringSidToSid(wszIntegritySid, &pIntegritySid)) {
				TIL.Label.Attributes = SE_GROUP_INTEGRITY;
				TIL.Label.Sid = pIntegritySid;

				// Set the process integrity level
				if (SetTokenInformation(hNewToken, TokenIntegrityLevel, &TIL, sizeof(TOKEN_MANDATORY_LABEL) + GetLengthSid(pIntegritySid))) {
					// Create the new process at Low integrity
					bRet = CreateProcessAsUser(hNewToken, NULL, (LPWSTR)pcFileName, NULL, NULL, FALSE, 0, NULL, NULL, &StartupInfo, &ProcInfo);
				}

				LocalFree(pIntegritySid);
			}
			CloseHandle(hNewToken);
		}
		CloseHandle(hToken);
	}

	return bRet;*/

	int bSuccess = 0;

	HRESULT hr = CoInitialize(NULL);
	if((hr == S_FALSE) || (hr == S_OK))
	{
		IShellWindows *psw = NULL;
		hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&psw));
		if(SUCCEEDED(hr))
		{
			HWND hwnd = 0;
			IDispatch* pdisp = NULL;
			variant_t vEmpty;
			if(S_OK == psw->FindWindowSW(&vEmpty.get(), &vEmpty.get(), SWC_DESKTOP, (long*)&hwnd, SWFO_NEEDDISPATCH, &pdisp))
			{
				if((hwnd != NULL) && (hwnd != INVALID_HANDLE_VALUE))
				{
					IShellBrowser *psb;
					hr = IUnknown_QueryService(pdisp, SID_STopLevelBrowser, IID_PPV_ARGS(&psb));
					if(SUCCEEDED(hr))
					{
						IShellView *psv = NULL;
						hr = psb->QueryActiveShellView(&psv);
						if(SUCCEEDED(hr))
						{
							IDispatch *pdispBackground = NULL;
							hr = psv->GetItemObject(SVGIO_BACKGROUND, IID_PPV_ARGS(&pdispBackground));
							if (SUCCEEDED(hr))
							{
								IShellFolderViewDual *psfvd = NULL;
								hr = pdispBackground->QueryInterface(IID_PPV_ARGS(&psfvd));
								if (SUCCEEDED(hr))
								{
									IDispatch *pdisp2 = NULL;
									hr = psfvd->get_Application(&pdisp2);
									if (SUCCEEDED(hr))
									{
										IShellDispatch2 *psd;
										hr = pdisp2->QueryInterface(IID_PPV_ARGS(&psd));
										if(SUCCEEDED(hr))
										{
											variant_t verb(pcOperation);
											variant_t file(pcFileName);
											variant_t para(pcParameters);
											variant_t show(SW_SHOWNORMAL);
											hr = psd->ShellExecute(file.get().bstrVal, para.get(), vEmpty.get(), verb.get(), show.get());
											if(SUCCEEDED(hr)) bSuccess = 1;
											psd->Release();
											psd = NULL;
										}
										pdisp2->Release();
										pdisp2 = NULL;
									}
								}
								pdispBackground->Release();
								pdispBackground = NULL;
							}
							psv->Release();
							psv = NULL;
						}
						psb->Release();
						psb = NULL;
					}
				}
				pdisp->Release();
				pdisp = NULL;
			}
			psw->Release();
			psw = NULL;
		}
		CoUninitialize();
	}
	

	if(bSuccess < 1)
	{
		dcassert(0);
		HINSTANCE hInst = ShellExecuteW(parentHwnd, pcOperation, pcFileName, pcParameters, NULL, SW_SHOWNORMAL);
		if(((int) hInst) <= 32) bSuccess = -1;
	}

	return bSuccess;
}
Esempio n. 22
0
static int ShellExecAsUser_ShellDispatchProc(const TCHAR *pcOperation, const TCHAR *pcFileName, const TCHAR *pcParameters, const HWND parentHwnd)
{
	int iSuccess = SHELLEXECASUSER_ERROR_FAILED;

	IShellWindows *psw = NULL;
	HRESULT hr = CoCreateInstance(CLSID_ShellWindows, NULL, CLSCTX_LOCAL_SERVER, IID_PPV_ARGS(&psw));
	if(SUCCEEDED(hr))
	{
		HWND hwnd = 0;
		IDispatch* pdisp = NULL;
		variant_t vEmpty;
		if(S_OK == psw->FindWindowSW(vEmpty, vEmpty, SWC_DESKTOP, (long*)&hwnd, SWFO_NEEDDISPATCH, &pdisp))
		{
			if((hwnd != NULL) && (hwnd != INVALID_HANDLE_VALUE))
			{
				IShellBrowser *psb;
				hr = IUnknown_QueryService(pdisp, SID_STopLevelBrowser, IID_PPV_ARGS(&psb));
				if(SUCCEEDED(hr))
				{
					IShellView *psv = NULL;
					hr = psb->QueryActiveShellView(&psv);
					if(SUCCEEDED(hr))
					{
						IDispatch *pdispBackground = NULL;
						HRESULT hr = psv->GetItemObject(SVGIO_BACKGROUND, IID_PPV_ARGS(&pdispBackground));
						if (SUCCEEDED(hr))
						{
							IShellFolderViewDual *psfvd = NULL;
							hr = pdispBackground->QueryInterface(IID_PPV_ARGS(&psfvd));
							if (SUCCEEDED(hr))
							{
								IDispatch *pdisp = NULL;
								hr = psfvd->get_Application(&pdisp);
								if (SUCCEEDED(hr))
								{
									IShellDispatch2 *psd;
									hr = pdisp->QueryInterface(IID_PPV_ARGS(&psd));
									if(SUCCEEDED(hr))
									{
										DispatchPendingMessages(125);
										variant_t verb(pcOperation);
										variant_t file(pcFileName);
										variant_t para(pcParameters);
										variant_t show(SW_SHOWNORMAL);
										hr = psd->ShellExecute(file, para, vEmpty, verb, show);
										if(SUCCEEDED(hr))
										{
											iSuccess = SHELLEXECASUSER_ERROR_SUCCESS;
										}
										psd->Release();
										psd = NULL;
									}
									pdisp->Release();
									pdisp = NULL;
								}
							}
							pdispBackground->Release();
							pdispBackground = NULL;
						}
						psv->Release();
						psv = NULL;
					}
					psb->Release();
					psb = NULL;
				}
			}
			pdisp->Release();
			pdisp = NULL;
		}
		psw->Release();
		psw = NULL;
	}

	return iSuccess;
}
Esempio n. 23
0
void FileDialog::FilterFiles(HWND hDlg, bool refresh)
{
   HWND parent = ::GetParent(hDlg);
   IShellFolder *ishell = NULL;
   IShellBrowser *ishellbrowser = NULL;  // Does not have to be released
   IShellView *ishellview = NULL;
   IFolderView *ifolderview = NULL;
   LPMALLOC imalloc = NULL;
   HRESULT hr;
   
   // Get pointer to the ListView control
   HWND lv = ::GetDlgItem(::GetDlgItem(parent, lst2), 1);
   if (lv == NULL)
   {
      wxASSERT(lv != NULL);
      return;
   }
   
   // Get shell's memory allocation interface (must be Release()'d)
   hr = SHGetMalloc(&imalloc);
   if ((hr != NOERROR) || (imalloc == NULL))
   {
      wxASSERT((hr == NOERROR) && (imalloc != NULL));
      return;
   }

   // Get IShellBrowser interface for current dialog
   ishellbrowser = (IShellBrowser*)::SendMessage(parent, WM_GETISHELLBROWSER, 0, 0);
   if (ishellbrowser)
   {
      // Get IShellBrowser interface for returned browser
      if (ishellbrowser->QueryActiveShellView(&ishellview) == S_OK)
      {
         // Get the IFolderView interface...available on XP or greater
         ishellview->QueryInterface(IID_IFolderView, (void **)&ifolderview);
      }
   }

   // Init
   LVITEM lvi;
   wxZeroMemory(lvi);

   // Process all items
   int fltcnt = (int) m_Filters.GetCount();
   int itmcnt = ::SendMessage(lv, LVM_GETITEMCOUNT, 0, 0);
   for (int itm = 0; itm < itmcnt; itm++)
   {
      // Retrieve the file IDL
      lvi.iItem = itm;
      lvi.mask = LVIF_PARAM;
      if (ListView_GetItem(lv, &lvi) != TRUE)
      {
         wxASSERT(FALSE);
         break;
      }

      LPCITEMIDLIST fidl = (LPCITEMIDLIST) lvi.lParam;

      // On Vista, lParam no longer contains the pidl so retrieve it via the
      // IFolderView interface.  This interface is only available on XP or higher
      // so if that limitation isn't workable, use IShellView::GetItemObject() to
      // retrieve items.
      if (fidl == NULL && ifolderview != NULL)
      {
         ifolderview->Item(itm, (LPITEMIDLIST *) &fidl);
      }

      if (fidl == NULL)
      {
         wxASSERT(fidl != NULL);
         break;
      }

      // Retrieve the IShellFolder interface of the parent (must be Release()'d)
      if (ishell == NULL)
      {
         hr = SHBindToParentLocal(fidl, IID_IShellFolder, (void **)&ishell, NULL);
         if (!SUCCEEDED(hr))
         {
            wxASSERT(SUCCEEDED(hr));
            break;
         }
      }
      
      // Get the attributes of the object
      DWORD attr = SFGAO_FOLDER | SFGAO_BROWSABLE;
      hr = ishell->GetAttributesOf(1, &fidl, &attr);
      if (!SUCCEEDED(hr))
      {
         wxASSERT(SUCCEEDED(hr));
         break;
      }
      
      // Allow all folders (things like zip files get filtered below)
      if ((attr & (SFGAO_FOLDER)) && !(attr & SFGAO_BROWSABLE))
      {
         continue;
      }
      
      // Retrieve the parsable name of the object (includes extension)
      STRRET str;
      hr = ishell->GetDisplayNameOf(fidl, SHGDN_INFOLDER | SHGDN_FORPARSING, &str);
      if (hr != NOERROR)
      {
         // For some objects, we get back an error of 80070057.  I'm assuming this
         // means there is no way to represent the name (like some sort of virtual name)
         // or I've not used the correct PIDL.  But, in either case, it "probably"
         // represents some sort of folder (at least in all cases I've seen), so we
         // simply allow it to display.
         continue;
      }
      
      // Convert result to wxString
      wxString filename;
      switch (str.uType)
      {
         case STRRET_WSTR:
            filename = str.pOleStr;
            imalloc->Free(str.pOleStr);
            break;
            
         case STRRET_OFFSET:
            filename = wxString(((char *)fidl) + str.uOffset, wxConvISO8859_1);
            break;
            
         case STRRET_CSTR:
            filename = wxString(str.cStr, wxConvISO8859_1);
            break;
      }
      
      // Convert the filename to lowercase (and remember to write filters in lowercase!)
      filename = filename.Lower();

      // Attempt to match it to all of our filters
      bool match = false;
      for (int flt = 0; flt < fltcnt; flt++)
      {
         if (wxMatchWild(m_Filters[flt], filename, false))
         {
            match = true;
            break;
         }
      }
      
      // Remove it from the display if it didn't match any of the filters.
      if (!match)
      {
         ListView_DeleteItem(lv, itm);
         itm--;
         itmcnt--;
      }
   }

   // On Vista and maybe XP, we seem to need to refresh the view after
   // changing the filters.  But, only refresh for type changes and not
   // selection changes since it causes additional selection change
   // events to occur.
   if (ishellview && refresh)
   {
      ishellview->Refresh();
   }

   // Release the interface
   if (ifolderview)
   {
      ifolderview->Release();
   }

   // Release the interface
   if (ishellview)
   {
      ishellview->Release();
   }

   // Release the interface
   if (ishell)
   {
      ishell->Release();
   }
   
   // Release the interface
   if (imalloc)
   {
      imalloc->Release();
   }
}
Esempio n. 24
0
bool CDeskBand::Filter(LPTSTR filter)
{
    bool bReturn = false;
    IServiceProvider * pServiceProvider;
    if (SUCCEEDED(m_pSite->QueryInterface(IID_IServiceProvider, (LPVOID*)&pServiceProvider)))
    {
        IShellBrowser * pShellBrowser;
        if (SUCCEEDED(pServiceProvider->QueryService(SID_SShellBrowser, IID_IShellBrowser, (LPVOID*)&pShellBrowser)))
        {
            IShellView * pShellView;
            if (SUCCEEDED(pShellBrowser->QueryActiveShellView(&pShellView)))
            {
                IFolderView * pFolderView;
                if (SUCCEEDED(pShellView->QueryInterface(IID_IFolderView, (LPVOID*)&pFolderView)))
                {
                    // hooray! we got the IFolderView interface!
                    // that means the explorer is active and well :)
                    IShellFolderView * pShellFolderView;
                    if (SUCCEEDED(pShellView->QueryInterface(IID_IShellFolderView, (LPVOID*)&pShellFolderView)))
                    {
                        // the first thing we do is to deselect all already selected entries
                        pFolderView->SelectItem(NULL, SVSI_DESELECTOTHERS);

                        // but we also need the IShellFolder interface because
                        // we need its GetDisplayNameOf() method
                        IPersistFolder2 * pPersistFolder;
                        if (SUCCEEDED(pFolderView->GetFolder(IID_IPersistFolder2, (LPVOID*)&pPersistFolder)))
                        {
                            LPITEMIDLIST curFolder;
                            pPersistFolder->GetCurFolder(&curFolder);
                            if (ILIsEqual(m_currentFolder, curFolder))
                            {
                                CoTaskMemFree(curFolder);
                            }
                            else
                            {
                                CoTaskMemFree(m_currentFolder);
                                m_currentFolder = curFolder;
                                for (size_t i=0; i<m_noShows.size(); ++i)
                                {
                                    CoTaskMemFree(m_noShows[i]);
                                }
                                m_noShows.clear();
                            }
                            IShellFolder * pShellFolder;
                            if (SUCCEEDED(pPersistFolder->QueryInterface(IID_IShellFolder, (LPVOID*)&pShellFolder)))
                            {
                                // our next task is to enumerate all the
                                // items in the folder view and select those
                                // which match the text in the edit control

                                bool bUseRegex = (filter[0] == '\\');

                                try
                                {
                                    const std::tr1::wregex regCheck(&filter[1], std::tr1::regex_constants::icase | std::tr1::regex_constants::ECMAScript);
                                }
                                catch (std::exception)
                                {
                                    bUseRegex = false;
                                }

                                if (!bUseRegex)
                                {
                                    // force the filter to lowercase
                                    TCHAR * pString = filter;
                                    while (*pString)
                                    {
                                        *pString = _totlower(*pString);
                                        pString++;
                                    }
                                }

                                int nCount = 0;
                                if (SUCCEEDED(pFolderView->ItemCount(SVGIO_ALLVIEW, &nCount)))
                                {
                                    pShellFolderView->SetRedraw(FALSE);
                                    HWND listView = GetListView32(pShellView);
                                    LRESULT viewType = 0;
                                    if (listView)
                                    {
                                        // inserting items in the list view if the list view is set to
                                        // e.g., LV_VIEW_LIST is painfully slow. So save the current view
                                        // and set it to LV_VIEW_DETAILS (which is much faster for inserting)
                                        // and restore the view after we're done.
                                        viewType = SendMessage(listView, LVM_GETVIEW, 0, 0);
                                        SendMessage(listView, LVM_SETVIEW, LV_VIEW_DETAILS, 0);
                                    }
                                    std::vector<LPITEMIDLIST> noShows;
                                    for (int i=0; i<nCount; ++i)
                                    {
                                        LPITEMIDLIST pidl;
                                        if (SUCCEEDED(pFolderView->Item(i, &pidl)))
                                        {
                                            if (CheckDisplayName(pShellFolder, pidl, filter, bUseRegex))
                                            {
                                                // remove now shown items which are in the no-show list
                                                // this is necessary since we don't get a notification
                                                // if the shell refreshes its view
                                                for (std::vector<LPITEMIDLIST>::iterator it = m_noShows.begin(); it != m_noShows.end(); ++it )
                                                {
                                                    if (HRESULT_CODE(pShellFolder->CompareIDs(SHCIDS_CANONICALONLY, *it, pidl))==0)
                                                    {
                                                        m_noShows.erase(it);
                                                        break;
                                                    }
                                                }
                                                CoTaskMemFree(pidl);
                                            }
                                            else
                                            {
                                                UINT puItem = 0;
                                                if (pShellFolderView->RemoveObject(pidl, &puItem) == S_OK)
                                                {
                                                    i--;
                                                    nCount--;
                                                    noShows.push_back(pidl);
                                                }
                                            }
                                        }
                                    }
                                    // now add all those items again which were removed by a previous filter string
                                    // but don't match this new one
                                    //pShellFolderView->SetObjectCount(5000, SFVSOC_INVALIDATE_ALL|SFVSOC_NOSCROLL);
                                    for (size_t i=0; i<m_noShows.size(); ++i)
                                    {
                                        LPITEMIDLIST pidlNoShow = m_noShows[i];
                                        if (CheckDisplayName(pShellFolder, pidlNoShow, filter, bUseRegex))
                                        {
                                            m_noShows.erase(m_noShows.begin() + i);
                                            i--;
                                            UINT puItem = (UINT)i;
                                            pShellFolderView->AddObject(pidlNoShow, &puItem);
                                            CoTaskMemFree(pidlNoShow);
                                        }
                                    }
                                    for (size_t i=0; i<noShows.size(); ++i)
                                    {
                                        m_noShows.push_back(noShows[i]);
                                    }
                                    if (listView)
                                    {
                                        SendMessage(listView, LVM_SETVIEW, viewType, 0);
                                    }

                                    pShellFolderView->SetRedraw(TRUE);
                                }
                                pShellFolder->Release();
                            }
                            pPersistFolder->Release();
                        }
                        pShellFolderView->Release();
                    }
                    pFolderView->Release();
                }
                pShellView->Release();
            }
            pShellBrowser->Release();
        }
        pServiceProvider->Release();
    }
    return bReturn;
}