COM using CoMarshalInterThreadInterfaceInStream to marshal interface - c++

I have a method that register functions and interface in C++, and I am using CoMarshalInterThreadInterfaceInStream to marshal an interface pointer to a method in another thread.
In RegisterFunction:
HRESULT hr = CoMarshalInterThreadInterfaceInStream(IID_Function, function, &stream);
Releasing it in GetFunction:
HRESULT hr = CoGetInterfaceAndReleaseStream(stream, IID_Function, reinterpret_cast<void**>(&function));
Since I will be calling GetFunction more than once and CoGetInterfaceAndReleaseStream only releases a stream once, how do I save the stream to use it again?
I tried using IGlobalInterfaceTable, but I can't get it to work.
I registered the Inteface successfully in RegisterFunction:
DWORD dwCookie = 0;
int a = 0;
if (pGIT == NULL) {
HRESULT hr = CoCreateInstance(CLSID_StdGlobalInterfaceTable,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void **)&pGIT);
if (hr != S_OK) {
throw _com_error(hr);
}
}
if (dwCookie == 0) {
HRESULT hr = pGIT->RegisterInterfaceInGlobal(function, IID_Function, &dwCookie);
if (hr != S_OK) {
throw _com_error(hr);
}
}
But when I tried to retrieve it in GetFunction:
IGlobalInterfaceTable* GIT = NULL;
if (GIT == NULL) {
hr = CoCreateInstance(CLSID_StdGlobalInterfaceTable,
NULL,
CLSCTX_INPROC_SERVER,
IID_IGlobalInterfaceTable,
(void **)&GIT);
if (FAILED(hr)) {
exit(0);
}
}
hr = pGIT->GetInterfaceFromGlobal(dwCookie, IID_Function, (void**)&function);
if (FAILED(hr)) {
exit(0);
}
I get an HR error of invalidArg when I tried to RegisterInterfaceInGlobal (even though it uses the same parameter as CoMarshalInterThreadInterfaceInStream except for the cookie)

As SoronelHaetir stated, you can't do this with CoMarshalInterThreadInterfaceInStream. It can only (un)marshal an interface one time.
Look at using IGlobalInterfaceTable instead. You can register your COM object in the table one time, and then retrieve the object from the table in any thread/apartment as many times as you want, until you revoke the object from the table.

You can't do it with CoMarshalInterThreadInterfaceInStream, it is a convenience method for interface pointers that will be extracted just once.
Instead use CoMarshalInterface/CoUnmarshalInterface and use a stream created with ShCreateMemStream.

Related

IGlobalInterfaceTable::RegisterInterfaceInGlobal returns E_NOTIMPL

Does anyone know why IGlobalInterfaceTable::RegisterInterfaceInGlobal might return E_NOTIMPL?
I'm using ATL's CComGITPtr and simply assigning it to a COM pointer. The assignment, in turn, calls the following ATL code:
HRESULT Attach(_In_ T* p) throw()
{
if (p)
{
CComPtr<IGlobalInterfaceTable> spGIT;
HRESULT hr = E_FAIL;
hr = AtlGetGITPtr(&spGIT);
ATLASSERT(spGIT != NULL);
ATLASSERT(SUCCEEDED(hr));
if (FAILED(hr))
return hr;
if (m_dwCookie != 0)
hr = spGIT->RevokeInterfaceFromGlobal(m_dwCookie);
if (FAILED(hr))
return hr;
return spGIT->RegisterInterfaceInGlobal(p, __uuidof(T), &m_dwCookie);
}
else
{
return Revoke();
}
}
Apparently that last call to spGIT->RegisterInterfaceInGlobal results in E_NOTIMPL.
I'm performing the assignment in the context of an in-proc Explorer extension, and I'm doing it on the main UI thread.
Also, I'm not sure if this is related, but I get the same error when trying to use RoGetAgileReference (on Windows 8.1), doing something like this (with an IShellItemArray):
HRESULT hr;
CComPtr<IAgileReference> par;
hr = RoGetAgileReference(AGILEREFERENCE_DEFAULT, IID_IShellItemArray, psia, &par);
Any ideas what might be going wrong?

How to create shortcut for virtual folder in C++ on windows 7?

The platform I am using is windows 7. I need to create shortcut for the virtual folder on windows 7.
I use windows 7 SDK sample to create a virtual folder under Computer:
The sample project name is called ExplorerDataProvider, it defines the CLSID for the IShellFolder class:
// add classes supported by this module here
const CLASS_OBJECT_INIT c_rgClassObjectInit[] =
{
{ &CLSID_FolderViewImpl, CFolderViewImplFolder_CreateInstance },
{ &CLSID_FolderViewImplContextMenu,CFolderViewImplContextMenu_CreateInstance },
};
The definition for CFolderViewImplFolder_CreateInstance is:
HRESULT CFolderViewImplFolder_CreateInstance(REFIID riid, void **ppv)
{
*ppv = NULL;
CFolderViewImplFolder* pFolderViewImplShellFolder = new (std::nothrow) CFolderViewImplFolder(0);
HRESULT hr = pFolderViewImplShellFolder ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
hr = pFolderViewImplShellFolder->QueryInterface(riid, ppv);
pFolderViewImplShellFolder->Release();
}
return hr;
}
And the CFolderViewImplFolder implement IShellFolder2 amd IPersistFolder2.
I found a similar code which is used to create shortcut for printer here:
https://www.codeproject.com/Articles/596642/Creating-a-shortcut-programmatically-in-Cplusplus
and in
https://msdn.microsoft.com/en-us/library/aa969393.aspx#Shellink_Item_Identifiers
Once you have the class identifier for IShellFolder, you can call the CoCreateInstance function to retrieve the address of the interface. Then you can call the interface to enumerate the objects in the folder and retrieve the address of the item identifier for the object that you are searching for. Finally, you can use the address in a call to the IShellLink::SetIDList member function to create a shortcut to the object.
I revised
hr = SHGetMalloc(&pMalloc);
hr = SHGetDesktopFolder( &pDesktopFolder );
hr = SHGetSpecialFolderLocation( NULL, CSIDL_PRINTERS, &netItemIdLst );
hr = pDesktopFolder->BindToObject( netItemIdLst, NULL, IID_IShellFolder, (void **)&pPrinterFolder );
to
// testFolder is the CLSID for the virtual folder implementation
hr = CoCreateInstance(testFolder, NULL, CLSCTX_INPROC_SERVER, IID_IShellFolder, (LPVOID*)&pVirtualFolder);
or
hr = CoCreateInstance(testFolder, NULL, CLSCTX_INPROC_SERVER, IID_IShellFolder2, (LPVOID*)&pVirtualFolder);
But the pVirtualFolder is still NULL, and it prints that "cannot find the corresponding interface".
Is there anything wrong with CoCreateInstance when I use it ? Or I shouldn't use this solution ? Any sample code for it ?
To create a shortcut, you can use the official documentation. Here is a sample code that creates a shortcut for a children of "This PC" (aka: ComputerFolder)
int main()
{
CoInitialize(NULL);
// I've used my Apple's iCloud as an example (name is in french)
// it's a virtual folder, a shell namespace extension
HRESULT hr = CreateComputerChildShortCut(L"Photos iCloud", L"c:\\temp\\my icloud");
printf("hr:0x%08X\n", hr);
CoUninitialize();
return 0;
}
HRESULT CreateComputerChildShortCut(LPWSTR childDisplayName, LPWSTR path)
{
// get My Computer folder's ShellItem (there are other ways for this...)
CComPtr<IShellItem> folder;
HRESULT hr = SHCreateItemInKnownFolder(FOLDERID_ComputerFolder, 0, NULL, IID_PPV_ARGS(&folder));
if (FAILED(hr)) return hr;
// enumerate children
CComPtr<IEnumShellItems> items;
hr = folder->BindToHandler(NULL, BHID_EnumItems, IID_PPV_ARGS(&items));
if (FAILED(hr)) return hr;
for (CComPtr<IShellItem> item; items->Next(1, &item, NULL) == S_OK; item.Release())
{
// get parsing path (if's often useful)
CComHeapPtr<wchar_t> parsingPath;
item->GetDisplayName(SIGDN_DESKTOPABSOLUTEPARSING, &parsingPath);
wprintf(L"Path: %s\n", parsingPath);
// get display name
CComHeapPtr<wchar_t> displayName;
item->GetDisplayName(SIGDN_NORMALDISPLAY, &displayName);
wprintf(L" Name: %s\n", displayName);
if (!lstrcmpi(childDisplayName, displayName))
{
// get pidl
// it's the unambiguous way of referencing a shell thing
CComHeapPtr<ITEMIDLIST> pidl;
hr = SHGetIDListFromObject(item, &pidl);
if (FAILED(hr)) return hr;
// create an instance of the standard Shell's folder shortcut creator
CComPtr<IShellLink> link;
hr = link.CoCreateInstance(CLSID_FolderShortcut);
if (FAILED(hr)) return hr;
// just use the pidl
hr = link->SetIDList(pidl);
if (FAILED(hr)) return hr;
CComPtr<IPersistFile> file;
hr = link->QueryInterface(&file);
if (FAILED(hr)) return hr;
// save the shortcut (we could also change other IShellLink parameters)
hr = file->Save(path, FALSE);
if (FAILED(hr)) return hr;
break;
}
}
return S_OK;
}
Of course, if you know an absolute parsing path or an absolute pidl, you don't have to enumerate anything, this was just for demonstration purposes.

How do I implement `BeginRead` in the IMFByteStream interface

I have created ReadByteContainer to store current data and ReadByteAsyncCallback for the callback. Are there alternatives that would work better?
HRESULT MediaByteStream::BeginRead(
BYTE *pb,
ULONG cb,
IMFAsyncCallback *pCallback,
IUnknown *punkState)
{
HRESULT hr = S_OK;
// Create a new read byte container.
ReadByteContainer* readBytes = new ReadByteContainer(pb, cb);
ReadByteAsyncCallback* readCallback = new ReadByteAsyncCallback(this);
// If not created.
if (readBytes == NULL)
{
return E_OUTOFMEMORY;
}
// If not created.
if (readCallback == NULL)
{
return E_OUTOFMEMORY;
}
IMFAsyncResult *pResult = NULL;
readBytes->_readCallback = readCallback;
// Creates an asynchronous result object. Use this function if you are implementing an asynchronous method.
hr = MFCreateAsyncResult(readBytes, pCallback, punkState, &pResult);
if (SUCCEEDED(hr))
{
// Start a new work item thread.
hr = MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, readCallback, pResult);
pResult->Release();
}
// Return the result.
return hr;
}
If pb remains valid until EndRead occurs, you can avoid the copy (new ReadByteContainer), and just keep pb as is.
Normally your MediaByteStream implements IMFAsyncCallback so you should call MFPutWorkItem like this (avoid new ReadByteAsyncCallback) :
hr = MFPutWorkItem(MFASYNC_CALLBACK_QUEUE_STANDARD, this, pResult);
Also, i don't see some lock mechanism inside BeginRead. If you use it in a multithreading environment you should handle this. Close can be call when BeginRead has been called and did not finish, so you will face problems.

How to use property pages on unregistrated filter?

I use filter DS LAME for compressing audio. I loaded it from file "lame.ax" as follows:
// pPath - path to LAME "lame.ax"
HRESULT CMyFilter::CreateObjectFromPath(wchar_t *pPath, REFCLSID clsid, IUnknown **ppUnk)
{
// load the target DLL directly
if (!m_hLibFilter) m_hLibFilter = LoadLibrary(pPath);
if (!m_hLibFilter)
{
return HRESULT_FROM_WIN32(GetLastError());
}
// the entry point is an exported function
FN_DLLGETCLASSOBJECT fn = (FN_DLLGETCLASSOBJECT)GetProcAddress(m_hLibFilter, "DllGetClassObject");
if (fn == NULL)
{
return HRESULT_FROM_WIN32(GetLastError());
}
// create a class factory
IUnknownPtr pUnk;
HRESULT hr = fn(clsid, IID_IUnknown, (void**)(IUnknown**)&pUnk);
if (SUCCEEDED(hr))
{
IClassFactoryPtr pCF = pUnk;
if (pCF == NULL)
{
hr = E_NOINTERFACE;
}
else
{
// ask the class factory to create the object
hr = pCF->CreateInstance(NULL, IID_IUnknown, (void**)ppUnk);
}
}
return hr;
}
further
HRESULT hr = 0;
IUnknown *ppUnk = 0;
ULONG lRef = 0;
hr = CreateObjectFromPath(L"lame.ax", CLSID_LAMEDShowFilter, (IUnknown **)&ppUnk);
hr = ppUnk->QueryInterface(&m_pFilter);
lRef = ppUnk->Release();
It works perfectly. LAME encoding audio.
I want to show the filter settings - property page, but this code failed
bool ShowConfigWindow(HWND hParent)
{
ISpecifyPropertyPages *pProp;
HRESULT hr = m_pFilter->QueryInterface(IID_ISpecifyPropertyPages, (void **)&pProp);
if (SUCCEEDED(hr))
{
// Get the filter's name and IUnknown pointer.
FILTER_INFO FilterInfo;
hr = m_pFilter->QueryFilterInfo(&FilterInfo);
IUnknown *pFilterUnk;
m_pFilter->QueryInterface(IID_IUnknown, (void **)&pFilterUnk);
// Show the page.
CAUUID caGUID;
pProp->GetPages(&caGUID);
pProp->Release();
HRESULT hr = OleCreatePropertyFrame(
hParent, // Parent window
0, 0, // Reserved
FilterInfo.achName, // Caption for the dialog box
1, // Number of objects (just the filter)
&pFilterUnk, // Array of object pointers.
caGUID.cElems, // Number of property pages
caGUID.pElems, // Array of property page CLSIDs
0, // Locale identifier
0, NULL // Reserved
);
// Clean up.
pFilterUnk->Release();
FilterInfo.pGraph->Release();
CoTaskMemFree(caGUID.pElems);
}
return true;
}
I find https://groups.google.com/forum/#!topic/microsoft.public.win32.programmer.directx.video/jknSbMenWeM
I should call CoRegisterClassObject for each property page, but how to do it?
Or what the right way?
OleCreatePropertyFrame takes property page class identifiers (CLSIDs) so you need to find a way to make them "visible" for the API.
Use of CoRegisterClassObject is one of the ways to achieve the mentioned task (perhaps the easiest, another method would be reg-free COM). You need to retrieve IClassFactory pointers for property page CLSIDs the same way as you do it in the first snippet. Then instead of calling IClassFactory::CreateInstance you use the interface pointers as arguments for CoRegisterClassObject API. Make sure you do it on the same thread as the following OleCreatePropertyFrame call. CoRevokeClassObject will clean things up afterwards.

I want to monitor new and changed contacts in Outlook add-in

I've created an Outlook addin in C++ (VS6!) that monitors the InspectorsCollection to catch when a user opens or closes a contact item. Works great.
Now I want it to track when a user saves a new contact, or modifies an existing one. I figured I'd modify the CInspectorsCollectionEventHandler class to work with contact items. Everything seems to be ok, and FindConnectionPoint() and Advise() succeed, but my Invoke() never gets called.
Here's how I set up, when my dll starts up:
Outlook::MAPIFolderPtr pFolder = g_pNameSpace->GetDefaultFolder(Outlook::olFolderContacts);
if (pFolder != NULL) {
Outlook::_ItemsPtr pContactItems = pFolder->GetItems();
if (pContactItems != NULL)
m_pContactItemsEventHandler = new CItemsEventHandler(pContactItems);
}
My CItemsEventHandler constructor calls SinkEvents() (below), which executes correctly.
Here are the key parts of my CItemsEventHandler class:
STDMETHODIMP CItemsEventHandler::QueryInterface(REFIID riid, void** ppv)
{
if (NULL == ppv) return E_POINTER;
*ppv = NULL;
HRESULT hr = S_OK;
if ((__uuidof(Outlook::ItemsEvents) == riid) ||
(IID_IUnknown == riid) || (IID_IDispatch == riid))
*ppv = static_cast<IDispatch*>(this);
else
hr = E_NOINTERFACE;
if (NULL != *ppv)
reinterpret_cast<IUnknown*>(*ppv)->AddRef();
return hr;
}
void CItemsEventHandler::SinkEvents(LPDISPATCH pItems)
{
HRESULT hr;
// Get server's IConnectionPointContainer interface.
IConnectionPointContainer* pCPC;
hr = pItems->QueryInterface(IID_IConnectionPointContainer, (void **)&pCPC);
if (SUCCEEDED(hr)) {
// Find connection point for events we're interested in.
hr = pCPC->FindConnectionPoint(__uuidof(Outlook::ItemsEvents), &m_pConnection);
if (SUCCEEDED(hr))
{
AddRef();
hr = m_pConnection->Advise(static_cast<IDispatch*>(this), &m_dwCookie);
}
// Release the IConnectionPointContainer
pCPC->Release();
}
}
It looks like everything is executing fine, and the code is based on code that works (for Inspectors), but I just don't get any calls to my Invoke()! Any ideas?
Solved it. Hope this is useful to someone. Turns out that as soon as my pContactItems pointer went out of scope (right after it was used), no events were registered. I moved pContactItems to a global variable and it started working.