Windows: File Object Id to File Name - windows

I am developing the explorer hook for monitoring data to portable devices through MTP protocol.
I hooked Delete() function from interface IPortableDeviceContent. This function gives me the list of file object IDs which needs to be deleted.
I want here to know what is the name of file for which delete request is sent.
If I can get file name from the received file object then it will resolve my issue.
Please let me know if anyone has any solution to this.

HookDelete() is the function which hooked for IPortableDeviceContent::Delete().
Below is the answer to the above question. Thought it does not give me full path, it gives a file name at least.
HRESULT STDMETHODCALLTYPE PortableDeviceInternalHook::HookDelete(
IPortableDeviceContent *pTHIS,
const DWORD dwOptions,
IPortableDevicePropVariantCollection *pObjectIDs,
IPortableDevicePropVariantCollection **ppResults)
{
DWORD count;
if(S_OK == pObjectIDs->GetCount(&count))
{
HRESULT result = S_FALSE;
bool blockOperation;
CComPtr<IPortableDeviceProperties> spProperties;
result = pTHIS->Properties(&spProperties);
if(S_OK == result)
{
for (unsigned int i = 0; i < count; i++)
{
HRESULT propertyValueResult;
PROPVARIANT propertyValue = {0};
propertyValueResult = pObjectIDs->GetAt(i, &propertyValue);
CComPtr<IPortableDeviceValues> spDeviceValues;
result = S_FALSE;
if(S_OK == propertyValueResult)
{
result = spProperties->GetValues(propertyValue.pwszVal, NULL, &spDeviceValues);
PropVariantClear(&propertyValue);
}
if(S_OK == result)
{
HRESULT hResult;
LPWSTR pFileName = NULL;
hResult = spDeviceValues->GetStringValue(WPD_OBJECT_ORIGINAL_FILE_NAME, &pFileName);
}
}
}
}
return m_pFnTrueDelete(pTHIS, dwOptions, pObjectIDs, ppResults);
}

Related

How to get extended file-attributes in Windows in Python/Go/C/Batch?

I have searched a lot and have got some findings on how to get extended FA, but they are in C# using the language's own built-in APIs. I am trying to find the author name for a file in Windows, but my requirement is in Go/Python/C/Batch (order of priority).
In python, the third party packages (exifread and hachoir_metadata) are not working (not giving any result for a sample doc/xlsx file. Maybe the package I am installing via pip-install is erroneous).
Is there any other way or any user-level MSDN API available?
Please let me know if any experience on this. Thanks.
In C, C++ or other language, you get file properties with IPropertyStore interface
For example, for a .jpg file (test on Windows 10, VS 2015) =>
I get for Author :
System.Author(Auteurs) = Test Auteur
PIDLIST_ABSOLUTE pidl = ILCreateFromPath(L"E:\\icon_rose.jpg");
if (pidl != NULL)
{
IPropertyStore *pps;
HRESULT hr = SHGetPropertyStoreFromIDList(pidl, GPS_DEFAULT, IID_PPV_ARGS(&pps));
if (SUCCEEDED(hr))
{
DWORD dwCount;
hr = pps->GetCount(&dwCount);
PROPERTYKEY propKey;
for (DWORD i = 0; i < dwCount; ++i)
{
hr = pps->GetAt(i, &propKey);
if (SUCCEEDED(hr))
{
PWSTR pszCanonicalName = NULL;
hr = PSGetNameFromPropertyKey(propKey, &pszCanonicalName);
PWSTR pszDescriptionName = NULL;
IPropertyDescription *ppd;
hr = PSGetPropertyDescription(propKey, IID_PPV_ARGS(&ppd));
if (SUCCEEDED(hr))
{
hr = ppd->GetDisplayName(&pszDescriptionName);
ppd->Release();
}
PROPVARIANT propvarValue = { 0 };
HRESULT hr = pps->GetValue(propKey, &propvarValue);
if (SUCCEEDED(hr))
{
PWSTR pszDisplayValue = NULL;
hr = PSFormatForDisplayAlloc(propKey, propvarValue, PDFF_DEFAULT, &pszDisplayValue);
if (SUCCEEDED(hr))
{
WCHAR wsBuffer[255];
wsprintf(wsBuffer, L"%s(%s) = %s\n", pszCanonicalName, (pszDescriptionName==NULL?L"Unknown":pszDescriptionName), pszDisplayValue);
OutputDebugString(wsBuffer);
CoTaskMemFree(pszDisplayValue);
}
PropVariantClear(&propvarValue);
}
if (pszCanonicalName != NULL)
CoTaskMemFree(pszCanonicalName);
if (pszDescriptionName != NULL)
CoTaskMemFree(pszDescriptionName);;
}
}
pps->Release();
}
ILFree(pidl);
}

Finding if the end point is default end point in windows 8

I have a code where I can enumerate all the audio-endpoints on my system, but I don't know how to find which end point is the default end point.
int _tmain(int argc, _TCHAR* argv[])
{
HRESULT hr = S_OK;
IMMDeviceEnumerator *pEnumerator = NULL;
IMMDeviceCollection *pCollection = NULL;
IMMDevice *pEndpoint = NULL;
IPropertyStore *pProps = NULL;
LPWSTR pwszID = NULL;
system("pause");
CoInitialize(NULL);
hr = CoCreateInstance(
CLSID_MMDeviceEnumerator, NULL,
CLSCTX_ALL, IID_IMMDeviceEnumerator,
(void**) &pEnumerator);
EXIT_ON_ERROR(hr)
hr = pEnumerator->EnumAudioEndpoints(
eRender, DEVICE_STATE_ACTIVE,
&pCollection);
EXIT_ON_ERROR(hr)
UINT count;
hr = pCollection->GetCount(&count);
EXIT_ON_ERROR(hr)
if (count == 0)
{
printf("No endpoints found.\n");
}
// Each loop prints the name of an endpoint device.
for (ULONG i = 0; i < count; i++)
{
// Get pointer to endpoint number i.
hr = pCollection->Item(i, &pEndpoint);
EXIT_ON_ERROR(hr)
// Get the endpoint ID string.
hr = pEndpoint->GetId(&pwszID);
EXIT_ON_ERROR(hr)
hr = pEndpoint->OpenPropertyStore(
STGM_READ, &pProps);
EXIT_ON_ERROR(hr)
PROPVARIANT varName;
// Initialize container for property value.
PropVariantInit(&varName);
// Get the endpoint's friendly-name property.
hr = pProps->GetValue(
PKEY_Device_FriendlyName, &varName);
EXIT_ON_ERROR(hr)
hr = pProps->GetValue(PKEY_Device_DeviceDesc, &varName );
EXIT_ON_ERROR(hr)
// Print endpoint friendly name and endpoint ID.
printf("Endpoint %d: \"%S\" (%S)\n",i, varName.pwszVal, pwszID);
//printf("Endpoint %d: \"%S\" (%S)\n",i, varName.pwszVal, pwszID);
CoTaskMemFree(pwszID);
pwszID = NULL;
PropVariantClear(&varName);
SAFE_RELEASE(pProps)
SAFE_RELEASE(pEndpoint)
}
SAFE_RELEASE(pEnumerator)
SAFE_RELEASE(pCollection)
system("pause");
return 0;
Exit:
printf("Error!\n");
CoTaskMemFree(pwszID);
SAFE_RELEASE(pEnumerator)
SAFE_RELEASE(pCollection)
SAFE_RELEASE(pEndpoint)
SAFE_RELEASE(pProps)
}
First of all, you should get the default driver:
using C#
const uint DRVM_MAPPER_PREFERRED_GET = 0x2015;
const uint DRVM_MAPPER_PREFERRED_SET = 0x2016;
uint ret = waveOutMessage(
WAVE_MAPPER,
DRVM_MAPPER_PREFERRED_GET,
ref defaultDeviceId,
ref param1);
So the defaultDeviceId variable will contain the correct index, and then you should query the defaultdriverName with waveOutGetDevCaps from system. The LPWAVEOUTCAPS will contain a valid szPname member.
And finally, you should compare this string(szPname) with value of PKEY_Device_FriendlyName property:
using C++
PROPVARIANT friendlyName;
PropVariantInit(&friendlyName);
hr = propertyStore->GetValue(PKEY_Device_FriendlyName, &friendlyName);
bstr_friendlyName = ::SysAllocString(friendlyName.pwszVal);
if (NULL != wcsstr(friendlyName.pwszVal, woc.szPname))//WAVEOUTCAPS szPname
//def device
uint ret = waveOutMessage(WAVE_MAPPER, DRVM_MAPPER_PREFERRED_GET, ref defaultDeviceId, ref param1);
if (ret == MMSYSERR_NOERROR)
{
int waveOutDevicesCount = waveOutGetNumDevs(); //get total
if (waveOutDevicesCount > defaultDeviceId)
{
WaveOutCaps waveOutCaps = new WaveOutCaps();
waveOutGetDevCapsA((int)defaultDeviceId, ref waveOutCaps,
Marshal.SizeOf(typeof(WaveOutCaps)));
string defaultDeviceName = (new string(waveOutCaps.szPname).Remove(
new string(waveOutCaps.szPname).IndexOf('\0')).Trim());

Getting the Windows Control Panel's virtual folder

Why would GetPath always return E_FAIL when querying FOLDERID_ControlPanelFolder? Other FOLDERIDs actually do work:
HRESULT hr = S_OK;
*path = '\0';
LPWSTR pwcPath = NULL;
CoInitialize(NULL);
IKnownFolderManager *pFolderManager = NULL;
if ((hr = CoCreateInstance(__uuidof(KnownFolderManager), NULL, CLSCTX_INPROC_SERVER, __uuidof(IKnownFolderManager), (LPVOID *)&pFolderManager)) == S_OK)
{
IKnownFolder *pControlPanelFolder = NULL;
if ((hr = pFolderManager->GetFolder(FOLDERID_ControlPanelFolder, &pControlPanelFolder)) == S_OK)
{
hr = pControlPanelFolder->GetPath(0, &pwcPath);
if (hr == S_OK && pwcPath)
{
int nSize = wcslen(pwcPath);
WideCharToMultiByte(CP_ACP, 0, pwcPath, nSize, path, nSize+2, NULL, NULL);
path[nSize] = '\0';
CoTaskMemFree(pwcPath);
}
pControlPanelFolder->Release();
pControlPanelFolder = NULL;
}
pFolderManager->Release();
pFolderManager = NULL;
}
CoUninitialize();
(Yes, I stumbled upon this question but I don't have need for all that enumeration stuff.)
The Control Panel has no directory path because it does not exist on the disc. You can get its PIDL, and even a Desktop Absolute Parsing "display name" (via GetShellItem and GetDisplayName), but not a directory path.
Reason why I needed the path was that I wanted to open the controp panel with a ShellExecute "open". I now execute the control panel program directly, with the benefit of being able to select the desired applet right away (in this case "Sound"). I hope it's not too pretentious that I post this as answer:
char controlpanelpath[2000];
UINT controlpanelpathbuffersize = sizeof(controlpanelpath);
int actualcontrolpanelpathsize;
if (actualcontrolpanelpathsize = GetSystemDirectory(controlpanelpath, controlpanelpathbuffersize))
{
char *parameters = "\\control.exe mmsys.cpl,,0";
if (actualcontrolpanelpathsize + strlen(parameters) < controlpanelpathbuffersize)
{
strcat(controlpanelpath, parameters);
WinExec(controlpanelpath, SW_NORMAL);
}
}

IsSearchSynchronous fails in Outlook

I am using Outlook automation to find items in the Calendar. For that I use IsSearchSynchronous() method to see if I have to wait for the AdvancedSearchComplete event. BTW, is it ever synchronous???
Anyway, if I have Outlook running, I have no problems with this call. But if it doesn't run - the call fails with
HRESULT: 0x80020009 Exception occurred
The EXCEPINFO contains:
Source: "Microsoft Outlook"
Description: "The operation failed."
scode: 0x80004005
Any suggestions?
Here is my test case:
#include <atlstr.h>
int _tmain()
{
IDispatch* pApp;
HRESULT hr;
CoInitialize(NULL);
CLSID clsid;
hr = CLSIDFromProgID(L"Outlook.Application", &clsid);
if(SUCCEEDED(hr))
{
hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&pApp);
if(SUCCEEDED(hr))
{
// Get DISPID for "IsSearchSynchronous"
OLECHAR Name[] = {L"IsSearchSynchronous"};
LPOLESTR lp = Name;
DISPID dispID;
hr = pApp->GetIDsOfNames(IID_NULL, &lp, 1, LOCALE_USER_DEFAULT, &dispID);
if(SUCCEEDED(hr))
{
// The path name of the folders that the search will search through.
VARIANT path;
path.vt = VT_BSTR;
path.bstrVal = SysAllocString(L"'Calendar'");
// Build DISPPARAMS
DISPPARAMS dp = { NULL, NULL, 0, 0 };
dp.cArgs = 1;
dp.rgvarg = &path;
// get IsSearchSynchronous
VARIANT result;
VariantInit(&result);
EXCEPINFO ei = {0};
hr = pApp->Invoke(dispID, IID_NULL, LOCALE_SYSTEM_DEFAULT, DISPATCH_PROPERTYGET, &dp, &result, &ei, NULL);
VARIANT_BOOL vbIsSearchSynchronous = result.boolVal;
}
}
}
CoUninitialize();
return 0;
}
In case anybody else is interested, this is resolved with a help from Microsoft.
The “scope” parameter to a IsSearchSynchronous() call should be a complete path to the calendar folder, like:
"\\Mailbox - <user_name>\\Calendar"

SHBrowseForFolder and shortcuts

In my C++ Windows application, I have a function that is supposed to allow the end-user select a folder. I'm using SHBrowseForFolder and it is working fine except that folder shortcuts are not being displayed in the dialog.
Does anyone know if it is possible to configure SHBrowseForFolder so that the end-users will be able to navigate folder shortcuts?
Edit: May 24th, 2010, 1:10 EST:
Okay, I'm going to show the code of what I have so far. I have tried using the suggestion of putting a BFFM_IUNKNOWN callback in my callback procedure, but have been struggling to figure out how to provide a IFolderFilter descendent that works.
1. The code that gets called:
Error CFolderChooserDialog::RunDialog()
{
Error runResult = kError_NotInitialized;
if (VERIFYN(kLyndsey, m_ReferenceCount > 0)) {
runResult = kError_Unexpected;
m_AllFoldersFilter = new TAllFoldersFilter();
if (VERIFYN(kLyndsey, m_AllFoldersFilter))
{
char selectedDirectoryBuffer[MAX_PATH];
m_DirectoryPath.CopyInto(selectedDirectoryBuffer);
BROWSEINFO bi;
memset(&bi, 0, sizeof(bi));
bi.hwndOwner = MyGetMainHWND(m_CBP);
bi.pidlRoot = NULL;
bi.pszDisplayName = selectedDirectoryBuffer;
bi.lpszTitle = (const char*)m_Description;
bi.ulFlags |= BIF_RETURNONLYFSDIRS;
bi.ulFlags |= BIF_BROWSEINCLUDEFILES;
bi.lpfn = SHBrowseForFolderCallbackProc;
bi.lParam = (LPARAM)this;
bi.iImage = 0;
LPITEMIDLIST resultInfo = SHBrowseForFolder(&bi);
if (resultInfo) {
runResult = kError_NoError;
if (SHGetPathFromIDList(resultInfo, selectedDirectoryBuffer)) {
m_DirectoryPath = selectedDirectoryBuffer;
}
}
else {
runResult = kError_Failed;
}
delete m_AllFoldersFilter;
m_AllFoldersFilter = nil;
CoTaskMemFree(resultInfo);
}
}
return runResult;
}
2. The callback that gets called from SHBrowseForFolder:
int CALLBACK CFolderChooserDialog::SHBrowseForFolderCallbackProc(HWND window, UINT message, LPARAM messageValue, LPARAM clientData)
{
CFolderChooserDialog* thisPtr = (CFolderChooserDialog*)clientData;
if (VERIFYN(kLyndsey, thisPtr)) {
switch (message) {
case BFFM_INITIALIZED: {
if (!thisPtr->m_DialogTitle.IsEmpty()) {
::SetWindowText(window, (const char*) thisPtr->m_DialogTitle);
}
if (!thisPtr->m_DirectoryPath.IsEmpty()) {
LPCTSTR startDirectory = thisPtr->m_DirectoryPath;
::SendMessage(window, BFFM_SETSELECTION, TRUE, (LPARAM)startDirectory);
}
break;
}
case BFFM_IUNKNOWN:
{
IUnknown* theInterface = (IUnknown*)messageValue;
if (VERIFYN(kLyndsey, theInterface))
{
IFolderFilterSite* filter = NULL;
theInterface->QueryInterface(IID_IFolderFilterSite, (void**)&filter);
if (VERIFYN(kLyndsey, filter))
{
filter->SetFilter((IUnknown*)thisPtr->m_AllFoldersFilter);
filter->Release();
}
}
break;
}
default:
break;
}
}
return 0;
}
3. The IFolderFilter that should get called for each item for filtering it in or out of the dialog:
class TAllFoldersFilter : public IFolderFilter
{
public:
TAllFoldersFilter() { refCount = 0;}
HRESULT STDMETHODCALLTYPE QueryInterface(const IID& iid, void** obj)
{
if (!obj)
return E_INVALIDARG;
*obj = NULL;
if (iid == IID_IUnknown || iid == IID_IFolderFilter)
{
*obj = (void*)this;
AddRef();
return NOERROR;
}
return E_NOINTERFACE;
}
ULONG STDMETHODCALLTYPE AddRef()
{
refCount++;
return refCount;
}
ULONG STDMETHODCALLTYPE Release()
{
refCount--;
return refCount;
}
HRESULT STDMETHODCALLTYPE GetEnumFlags(IShellFolder* sf, LPCITEMIDLIST pidlFolder, HWND* window, DWORD* flags)
{
return 0;
}
HRESULT STDMETHODCALLTYPE ShouldShow(IShellFolder* sf, LPCITEMIDLIST pidlFolder, LPCITEMIDLIST pidlItem)
{
HRESULT resultCode = S_OK;
ULONG attributes = 0UL;
if (SUCCEEDED(sf->GetAttributesOf(1, &pidlItem, &attributes)))
{
if (attributes & SFGAO_FOLDER)
{
resultCode = S_OK; // Yes, I see the folders
}
else if (attributes & SFGAO_LINK)
{
resultCode = S_OK; // Yes, this shows the folder shortcut links, but I cannot explore them. When I "expand" them (click on the plus-sign-box), nothing happens.
}
}
return resultCode;
}
protected:
ULONG refCount;
};
So, where am I? Well, I can show the folders, I can show folder links, but I'm uncertain about the following:
How do I easily determine if the item I have is a shortcut link to a folder? The code I wrote is definitely not looking at that and is showing any shortcut link.
How do I easily allow the end-user delve into the folder represented by the shortcut link?
Is this code correct and as simple/clean as it can be?
Thanks for all of your help!
Edit: June 1st, 2010: 2:14 EST:
The answer was technically provided, so I'll mark that and I'm going to ask another question to help me fix this code.
I guess you could add the BIF_BROWSEINCLUDEFILES style and then filter the items to only display folders and .lnk's to folders (This is what you are after right?)
To filter the items, you need to add a callback func to BROWSEINFO, catch BFFM_IUNKNOWN and query for IFolderFilterSite and set a filter
The better answer is to use IFileOpenDialog with FOS_PICKFOLDERS as the option for Windows Vista and later.

Resources