Exemple #1
0
bool _GetAppDataFolderFromKfm( CString& str ) {
	IKnownFolderManager* pkfm = NULL;
	IKnownFolder* pkf = NULL;
	LPWSTR path = NULL;
	bool ret = false;
	HRESULT hr;

	hr = CoCreateInstance( CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS( &pkfm ) );
	if ( FAILED(hr) ) {
		debug( L"CoCreateInstance(CLSID_KnownFolderManager) failed: %08x\n", hr );
		goto out1;
	}

	hr = pkfm->GetFolder( FOLDERID_RoamingAppData, &pkf );
	if ( FAILED( hr ) ) {
		debug( L"pkfm->GetFolder(FOLDERID_RoamingAppData) failed: %08x\n", hr );
		goto out2;
	}

	hr = pkf->GetPath( 0, &path );
	if ( FAILED( hr ) ) {
		debug( L"pkf->GetPath failed: %08x\n", hr );
		goto out3;
	}

	str = path;
	CoTaskMemFree( path );

	ret = true;

out3:	pkf->Release( );
out2:	pkfm->Release( );
out1:	return ret;
}
HRESULT RemovePhysicalFolder(REFKNOWNFOLDERID kfid)
{
    IKnownFolderManager *pkfm;
    HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm));
    if (SUCCEEDED(hr))
    {
        IKnownFolder *pkf;
        hr = pkfm->GetFolder(kfid, &pkf);
        if (SUCCEEDED(hr))
        {
            PWSTR pszPath;
            hr = pkf->GetPath(0, &pszPath);
            if (SUCCEEDED(hr))
            {
                SHFILEOPSTRUCT fos = {};
                fos.wFunc = FO_DELETE;
                fos.pFrom = pszPath;
                fos.fFlags = FOF_NOCONFIRMATION | FOF_NOERRORUI | FOF_SILENT;
                if (0 != SHFileOperation(&fos))
                {
                    hr = E_FAIL;
                }

                CoTaskMemFree(pszPath);
            }
            pkf->Release();
        }
        pkfm->Release();
    }
    return hr;
}
HRESULT GetKnownFolderForPath(PCWSTR pszPath, KNOWNFOLDERID *pkfid, KNOWNFOLDER_DEFINITION *pkfd)
{
    IKnownFolderManager *pkfm;
    HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm));
    if (SUCCEEDED(hr))
    {
        IKnownFolder *pkf;
        hr = pkfm->FindFolderFromPath(pszPath, FFFP_EXACTMATCH, &pkf);
        if (S_OK == hr)
        {
            hr = pkf->GetId(pkfid);
            if (S_OK == hr)
            {
                hr = pkf->GetFolderDefinition(pkfd);
                if (S_OK != hr)
                {
                    wprintf(L"IKnownFolderManager::GetFolderDefinition return hr=0x%x\n", hr);
                }
            }
            else
            {
                wprintf(L"IKnownFolder::GetId return hr=0x%x\n", hr);
            }
            pkf->Release();
        }
        else
        {
            wprintf(L"IKnownFolderManager::FindFolderFromPath return hr=0x%x\n", hr);
        }
        pkfm->Release();
    }
    return hr;
}
bool _GetAppDataFolderFromKfm( LPWSTR& lpsz ) {
	IKnownFolderManager* pkfm = nullptr;
	IKnownFolder* pkf = nullptr;
	bool ret = false;
	HRESULT hr;

	hr = CoCreateInstance( CLSID_KnownFolderManager, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS( &pkfm ) );
	if ( FAILED(hr) ) {
		debug( L"_GetAppDataFolderFromKfm: CoCreateInstance(CLSID_KnownFolderManager) failed: 0x%08x\n", hr );
		goto out1;
	}

	hr = pkfm->GetFolder( FOLDERID_RoamingAppData, &pkf );
	if ( FAILED( hr ) ) {
		debug( L"_GetAppDataFolderFromKfm: pkfm->GetFolder(FOLDERID_RoamingAppData) failed: 0x%08x\n", hr );
		goto out2;
	}

	hr = pkf->GetPath( 0, &lpsz );
	if ( FAILED( hr ) ) {
		debug( L"_GetAppDataFolderFromKfm: pkf->GetPath failed: 0x%08x\n", hr );
		goto out3;
	}

	ret = true;

out3:	pkf->Release( );
out2:	pkfm->Release( );
out1:	return ret;
}
Exemple #5
0
bool CWorkshareMenu::GetPathForSendToFolder(CStdString& sSendToPath) const
{
	IKnownFolderManager* pkfm = NULL;
	HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm));

	if( SUCCEEDED(hr) )
	{
		IKnownFolder* pkf = NULL;
		hr = pkfm->GetFolder(FOLDERID_SendTo, &pkf);

		if( SUCCEEDED(hr) )
		{
			LPWSTR pszPath = NULL;
			hr = pkf->GetPath(0, &pszPath);

			if( SUCCEEDED(hr) )
			{
				sSendToPath = pszPath;                

				CoTaskMemFree(pszPath);
			}

			pkf->Release();
		}

		pkfm->Release();
	}

	return SUCCEEDED(hr);
}
std::vector<std::wstring> APlayerWindow::showOpenFile()
{
	HRESULT hr = S_OK;
	std::vector<std::wstring> filePaths;

	IFileOpenDialog *fileDlg = NULL;
	hr = CoCreateInstance(CLSID_FileOpenDialog,
		NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fileDlg));
	if (FAILED(hr)) return filePaths;
	ON_SCOPE_EXIT([&] { fileDlg->Release(); });

	IKnownFolderManager *pkfm = NULL;
	hr = CoCreateInstance(CLSID_KnownFolderManager,
		NULL,
		CLSCTX_INPROC_SERVER,
		IID_PPV_ARGS(&pkfm));
	if (FAILED(hr)) return filePaths;
	ON_SCOPE_EXIT([&] { pkfm->Release(); });

	IKnownFolder *pKnownFolder = NULL;
	hr = pkfm->GetFolder(FOLDERID_PublicMusic, &pKnownFolder);
	if (FAILED(hr)) return filePaths;
	ON_SCOPE_EXIT([&] { pKnownFolder->Release(); });

	IShellItem *psi = NULL;
	hr = pKnownFolder->GetShellItem(0, IID_PPV_ARGS(&psi));
	if (FAILED(hr)) return filePaths;
	ON_SCOPE_EXIT([&] { psi->Release(); });

	hr = fileDlg->AddPlace(psi, FDAP_BOTTOM);
	COMDLG_FILTERSPEC rgSpec[] = {
		{ L"ÒôÀÖÎļþ", SupportType }
	};
	fileDlg->SetFileTypes(1, rgSpec);

	DWORD dwOptions;
	fileDlg->GetOptions(&dwOptions);
	fileDlg->SetOptions(dwOptions | FOS_ALLOWMULTISELECT);
	hr = fileDlg->Show(NULL);
	if (SUCCEEDED(hr)) {
		IShellItemArray *pRets;
		hr = fileDlg->GetResults(&pRets);
		if (SUCCEEDED(hr)) {
			DWORD count;
			pRets->GetCount(&count);
			for (DWORD i = 0; i < count; i++) {
				IShellItem *pRet;
				LPWSTR nameBuffer;
				pRets->GetItemAt(i, &pRet);
				pRet->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &nameBuffer);
				filePaths.push_back(std::wstring(nameBuffer));
				pRet->Release();
				CoTaskMemFree(nameBuffer);
			}
			pRets->Release();
		}
	}
	return filePaths;
}
    // Processes a single argument which identifies the library to operate on; passes any remaining arguments to the derived class.
    HRESULT v_ProcessArguments(PCWSTR *ppszArgs, int cArgs)
    {
        PCWSTR pszLibPath = CONSUME_NEXT_ARG(ppszArgs, cArgs);
        HRESULT hr = pszLibPath ? S_OK : E_INVALIDARG;
        if (SUCCEEDED(hr))
        {
            if (_fCreate)
            {
                // When creating a new library, interpret the argument as the file system path to save the library to.
                WCHAR szAbsPath[MAX_PATH];
                hr = SHStrDupW(_wfullpath(szAbsPath, pszLibPath, ARRAYSIZE(szAbsPath)), &_pszSavePath);
            }
            else
            {
                // Check for the 'FOLDERID_' prefix, which indicates that the argument should be interpreted as a KNOWNFOLDERID.
                const WCHAR szPrefix[] = L"FOLDERID_";
                const UINT cchPrefix = ARRAYSIZE(szPrefix) - 1;
                if (StrCmpNCW(pszLibPath, szPrefix, cchPrefix) == 0)
                {
                    IKnownFolderManager *pkfm;
                    hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm));
                    if (SUCCEEDED(hr))
                    {
                        // KNOWNFOLDERIDs are GUIDs, but they have a corresponding canonical name which is a string.
                        // By convention, the canonical name is the same as the name of the KNOWNFOLDERID #define.
                        // That is, FOLDERID_DocumentsLibrary => "DocumentsLibrary".  So, skip the prefix and pass
                        // the remainder to GetFolderByName to retrieve the known folder.
                        IKnownFolder *pkf;
                        hr = pkfm->GetFolderByName(pszLibPath + cchPrefix, &pkf);
                        if (SUCCEEDED(hr))
                        {
                            hr = pkf->GetShellItem(KF_FLAG_INIT, IID_PPV_ARGS(&_psiLibrary));
                            pkf->Release();
                        }
                        pkfm->Release();
                    }
                }
                else
                {
                    // Default - interpret the argument as a file system path, and create a shell item for it.
                    WCHAR szAbsPath[MAX_PATH];
                    hr = SHCreateItemFromParsingName(_wfullpath(szAbsPath, pszLibPath, ARRAYSIZE(szAbsPath)), NULL, IID_PPV_ARGS(&_psiLibrary));
                }
            }
        }
        else
        {
            ParseError(L"Missing library path argument.\n");
        }

        if (SUCCEEDED(hr))
        {
            // Allow derived command to process any remaining arguments.
            hr = v_ProcessLibArguments(ppszArgs, cArgs);
        }
        return hr;
    }
void EnumAndDumpKnownFolders(DWORD *pdwKFCount, PCWSTR pszNameSrchStr, REFKNOWNFOLDERID kfidSearch)
{
    *pdwKFCount = 0;
    IKnownFolderManager *pkfm = NULL;
    HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm));
    if (SUCCEEDED(hr))
    {
        KNOWNFOLDERID *rgKFIDs = NULL;
        UINT cKFIDs = 0;
        hr = pkfm->GetFolderIds(&rgKFIDs, &cKFIDs);
        if (SUCCEEDED(hr))
        {
            WCHAR szKFIDGuid[GUID_SIZE];
            IKnownFolder *pkfCurrent = NULL;
            for (UINT i = 0; i < cKFIDs; ++i)
            {
                // if we are searching for a specific GUID, make sure we match before going
                // any further.  GUID_NULL means "show all."
                if (kfidSearch == GUID_NULL || kfidSearch == rgKFIDs[i])
                {
                    StringFromGUID2(rgKFIDs[i], szKFIDGuid, ARRAYSIZE(szKFIDGuid));
                    hr = pkfm->GetFolder(rgKFIDs[i], &pkfCurrent);
                    if (SUCCEEDED(hr))
                    {
                        KNOWNFOLDERID kfid;
                        hr = pkfCurrent->GetId(&kfid);
                        if (FAILED(hr))
                        {
                            wprintf(L"IKnownFolder::GetId() failed for %s!  hr=0x%x\n", szKFIDGuid, hr);
                        }

                        KNOWNFOLDER_DEFINITION kfd;
                        hr = pkfCurrent->GetFolderDefinition(&kfd);
                        if (FAILED(hr))
                        {
                            wprintf(L"IKnownFolderManager::GetFolderDefinition() failed hr=0x%x KNOWNFOLDERID=%s", hr, szKFIDGuid);
                        }
                        else
                        {
                            BOOL fDumpThisFolder = TRUE;
                            if (pszNameSrchStr)
                            {
                                if (NULL == wcsstr(kfd.pszName, pszNameSrchStr))
                                {
                                    fDumpThisFolder = FALSE;
                                }
                            }

                            if (fDumpThisFolder)
                            {
                                ++*pdwKFCount;
                                DumpKnownFolderDef(kfid, kfd);
                                DumpKnownFolderInfo(pkfCurrent);
                            }

                            FreeKnownFolderDefinitionFields(&kfd);
                        }

                        pkfCurrent->Release();
                    }
                    else
                    {
                        wprintf(L"IKnownFolderManager::GetFolder() failed for %s  hr=0x%x\n", szKFIDGuid, hr);
                    }
                }
            }
            CoTaskMemFree(rgKFIDs);
        }
        pkfm->Release();
    }
}