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; }
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; }
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; }
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; }
// @pymethod <o PyIKnownFolder>|PyIKnownFolderManager|GetFolder|Returns a folder by its id. PyObject *PyIKnownFolderManager::GetFolder(PyObject *self, PyObject *args) { IKnownFolderManager *pIKFM = GetI(self); if ( pIKFM == NULL ) return NULL; IID kfid; IKnownFolder *ret; // @pyparm <o PyIID>|id||A known folder id (shell.FOLDERID_*) if ( !PyArg_ParseTuple(args, "O&:GetFolder", PyWinObject_AsIID, &kfid)) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pIKFM->GetFolder(kfid, &ret); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return PyCom_BuildPyException(hr, pIKFM, IID_IKnownFolderManager ); return PyCom_PyObjectFromIUnknown(ret, IID_IKnownFolder, FALSE); }
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(); } }