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 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); }
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; }
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; }
// @pymethod |PyIKnownFolderManager|RegisterFolder|Defines a new known folder PyObject *PyIKnownFolderManager::RegisterFolder(PyObject *self, PyObject *args) { IKnownFolderManager *pIKFM = GetI(self); if ( pIKFM == NULL ) return NULL; IID kfid; PyObject *obdef; // @pyparm <o PyIID>|id||GUID used to identify the new known folder // @pyparm dict|Definition||Dictionary containing info to be placed in a KNOWNFOLDER_DEFINITION struct // @comm <om PyIKnownFolder.GetFolderDefinition> can be used to get a template dictionary KNOWNFOLDER_DEFINITION def; ZeroMemory(&def, sizeof(def)); if ( !PyArg_ParseTuple(args, "O&O:RegisterFolder", PyWinObject_AsIID, &kfid, &obdef)) return NULL; if (!PyWinObject_AsKNOWNFOLDER_DEFINITION(obdef, def)) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pIKFM->RegisterFolder(kfid, &def); PY_INTERFACE_POSTCALL; FreeKnownFolderDefinitionFields(&def); if ( FAILED(hr) ) return PyCom_BuildPyException(hr, pIKFM, IID_IKnownFolderManager); Py_INCREF(Py_None); return Py_None; }
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; }
// @pymethod (<o PyIID>,...)|PyIKnownFolderManager|GetFolderIds|Retrieves all known folder ids. PyObject *PyIKnownFolderManager::GetFolderIds(PyObject *self, PyObject *args) { IKnownFolderManager *pIKFM = GetI(self); if ( pIKFM == NULL ) return NULL; UINT count = 0; IID *kfids; if ( !PyArg_ParseTuple(args, ":GetFolderIds")) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pIKFM->GetFolderIds(&kfids, &count); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return PyCom_BuildPyException(hr, pIKFM, IID_IKnownFolderManager ); PyObject *ret = PyTuple_New(count); if (ret){ for (UINT i=0; i<count; i++){ PyObject *pyiid = PyWinObject_FromIID(kfids[i]); if (pyiid == NULL){ Py_DECREF(ret); ret = NULL; break; } PyTuple_SET_ITEM(ret, i, pyiid); } } CoTaskMemFree(kfids); return ret; }
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; }
HRESULT UnregisterFolder(REFKNOWNFOLDERID kfid) { IKnownFolderManager *pkfm = NULL; HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm)); if (SUCCEEDED(hr)) { hr = pkfm->UnregisterFolder(kfid); if (FAILED(hr)) { wprintf(L"IKnownFolder::UnregisterFolder() failed with hr = 0x%x\n", hr); } pkfm->Release(); } return hr; }
// @pymethod <o PyIID>|PyIKnownFolderManager|FolderIdFromCsidl|Returns the folder id that corresponds to a CSIDL PyObject *PyIKnownFolderManager::FolderIdFromCsidl(PyObject *self, PyObject *args) { IKnownFolderManager *pIKFM = GetI(self); if ( pIKFM == NULL ) return NULL; // @pyparm int|Csidl||The legacy CSIDL identifying a folder int csidl; IID kfid; if ( !PyArg_ParseTuple(args, "i:FolderIdFromCsidl", &csidl)) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pIKFM->FolderIdFromCsidl( csidl, &kfid ); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return PyCom_BuildPyException(hr, pIKFM, IID_IKnownFolderManager ); return PyWinObject_FromIID(kfid); }
// @pymethod <o PyIKnownFolder>|PyIKnownFolderManager|GetFolderByName|Returns a folder by canonical name PyObject *PyIKnownFolderManager::GetFolderByName(PyObject *self, PyObject *args) { IKnownFolderManager *pIKFM = GetI(self); if ( pIKFM == NULL ) return NULL; // @pyparm str|Name||The nonlocalized name of a known folder PyObject *obname; TmpWCHAR name; IKnownFolder *ret; if ( !PyArg_ParseTuple(args, "O:GetFolderByName", &obname)) return NULL; if (!PyWinObject_AsWCHAR(obname, &name, FALSE)) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pIKFM->GetFolderByName(name, &ret); PY_INTERFACE_POSTCALL; return PyCom_PyObjectFromIUnknown(ret, IID_IKnownFolder, FALSE); }
// @pymethod |PyIKnownFolderManager|UnregisterFolder|Removes the definition of a known folder PyObject *PyIKnownFolderManager::UnregisterFolder(PyObject *self, PyObject *args) { IKnownFolderManager *pIKFM = GetI(self); if ( pIKFM == NULL ) return NULL; IID kfid; // @pyparm <o PyIID>|id||GUID of a known folder to be unregistered if ( !PyArg_ParseTuple(args, "O&:UnregisterFolder", PyWinObject_AsIID, &kfid)) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pIKFM->UnregisterFolder(kfid); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return PyCom_BuildPyException(hr, pIKFM, IID_IKnownFolderManager ); Py_INCREF(Py_None); return Py_None; }
// @pymethod int|PyIKnownFolderManager|FolderIdToCsidl|Returns the CSIDL equivalent of a known folder PyObject *PyIKnownFolderManager::FolderIdToCsidl(PyObject *self, PyObject *args) { IKnownFolderManager *pIKFM = GetI(self); if ( pIKFM == NULL ) return NULL; IID kfid; // @pyparm <o PyIID>|id||A known folder id (shell.FOLDERID_*) int csidl; if ( !PyArg_ParseTuple(args, "O&:FolderIdToCsidl", PyWinObject_AsIID, &kfid)) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pIKFM->FolderIdToCsidl( kfid, &csidl ); PY_INTERFACE_POSTCALL; if ( FAILED(hr) ) return PyCom_BuildPyException(hr, pIKFM, IID_IKnownFolderManager ); return PyInt_FromLong(csidl); }
// @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); }
// @pymethod |PyIKnownFolderManager|Redirect|Redirects a known folder to an alternate location PyObject *PyIKnownFolderManager::Redirect(PyObject *self, PyObject *args) { IKnownFolderManager *pIKFM = GetI(self); if ( pIKFM == NULL ) return NULL; IID kfid; // @pyparm <o PyIID>|id||Id of the known folder to be redirected HWND hwnd; // @pyparm <o PyHANDLE>|hwnd||Handle of window to be used for user interaction KF_REDIRECT_FLAGS flags; // @pyparm int|flags||Combination of KF_REDIRECT_* flags TmpWCHAR TargetPath; PyObject *obTargetPath; // @pyparm str|TargetPath||Path to which the known folder will be redirected ULONG cExcludes; IID *Excludes = NULL; PyObject *obExcludes; // @pyparm (<o PyIID>,...)|Exclusion||Sequence of known folder ids of subfolders to be excluded from redirection WCHAR *Error = NULL; if ( !PyArg_ParseTuple(args, "O&O&iOO:Redirect", PyWinObject_AsIID, &kfid, PyWinObject_AsHANDLE, &hwnd, &flags, &obTargetPath, &obExcludes)) return NULL; if (!PyWinObject_AsWCHAR(obTargetPath, &TargetPath, FALSE)) return NULL; if (!SeqToVector(obExcludes, &Excludes, &cExcludes, PyWinObject_AsIID)) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pIKFM->Redirect( kfid, hwnd, flags, TargetPath, cExcludes, Excludes, &Error); PY_INTERFACE_POSTCALL; CoTaskMemFree(Excludes); if ( FAILED(hr) ){ // ??? Need to figure out how to return Error string with exception ??? PyCom_BuildPyException(hr, pIKFM, IID_IKnownFolderManager ); CoTaskMemFree(Error); return NULL; } Py_INCREF(Py_None); return Py_None; }
HRESULT RegisterKnownFolder(REFKNOWNFOLDERID kfid, KNOWNFOLDER_DEFINITION *pkfd) { IKnownFolderManager *pkfm = NULL; HRESULT hr = CoCreateInstance(CLSID_KnownFolderManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&pkfm)); if (SUCCEEDED(hr)) { hr = pkfm->RegisterFolder(kfid, pkfd); if (SUCCEEDED(hr)) { // to make it easy to clean up everything this sample does, we'll track each added kfid AddRegisteredFolderToHistory(kfid); } else { wprintf(L"IKnownFolder::RegisterFolder() failed with hr = 0x%x\nMake sure this tool is run as an administrator as that is necessary to regiser a known folder", hr); } pkfm->Release(); } return hr; }
// @pymethod <o PyIKnownFolder>|PyIKnownFolderManager|FindFolderFromIDList|Retrieves a known folder using its item id list. PyObject *PyIKnownFolderManager::FindFolderFromIDList(PyObject *self, PyObject *args) { IKnownFolderManager *pIKFM = GetI(self); if ( pIKFM == NULL ) return NULL; IKnownFolder *ret; PIDLIST_ABSOLUTE pidl; PyObject *obpidl; // @pyparm <o PyIDL>|pidl||Item id list of the folder if ( !PyArg_ParseTuple(args, "O:FindFolderFromIDList", &obpidl)) return NULL; if (!PyObject_AsPIDL(obpidl, &pidl, FALSE)) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pIKFM->FindFolderFromIDList(pidl, &ret); PY_INTERFACE_POSTCALL; PyObject_FreePIDL(pidl); if ( FAILED(hr) ) return PyCom_BuildPyException(hr, pIKFM, IID_IKnownFolderManager ); return PyCom_PyObjectFromIUnknown(ret, IID_IKnownFolder, FALSE); }
// @pymethod <o PyIKnownFolder>|PyIKnownFolderManager|FindFolderFromPath|Retrieves a known folder by path PyObject *PyIKnownFolderManager::FindFolderFromPath(PyObject *self, PyObject *args) { IKnownFolderManager *pIKFM = GetI(self); if ( pIKFM == NULL ) return NULL; // @pyparm str|Path||Path of a folder // @pyparm int|Mode||FFFP_EXACTMATCH or FFFP_NEARESTPARENTMATCH TmpWCHAR path; PyObject *obpath; FFFP_MODE mode; IKnownFolder *ret; if ( !PyArg_ParseTuple(args, "Oi:FindFolderFromPath", &obpath, &mode)) return NULL; if (!PyWinObject_AsWCHAR(obpath, &path, FALSE)) return NULL; HRESULT hr; PY_INTERFACE_PRECALL; hr = pIKFM->FindFolderFromPath(path, mode, &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(); } }