c++ MS Word - OleAutomation - c++

I am creating IDispatch like this:
IDispatch *m_pWApp = NULL;
CoInitialize(NULL);
CLSID clsid;
m_hr = CLSIDFromProgID(L"Word.Application", &clsid);
if(SUCCEEDED(m_hr))
{
m_hr = CoCreateInstance(clsid, NULL, CLSCTX_LOCAL_SERVER, IID_IDispatch, (void **)&m_pWApp);
if(FAILED(m_hr)) m_pWApp=NULL;
}
I am using this to automate word like in example
But now I need to use method from interface IOleCommandTarget::Exec for hide menu bar for opening word. I know that with this method it is possible.
My question is, how can I get IOleCommandTarget to use it for calling Exec and of course to have possibility to use IDispatch line in example.

Your code is correct, just add:
LPOLECOMMANDTARGET lpOleCommandTarget = NULL;
lpDispatch->QueryInterface(IID_IOleCommandTarget, (void**)&lpOleCommandTarget);
or
IOleCommandTarget* target;
disp->QueryInterface (IID_IOleCommandTarget, (void **) &target);
Then **lpOleCommandTarget->Exec(...);**
For example:
lpOleCommandTarget->Exec(NULL, OLECMDID_PRINT, 0, NULL,NULL);

Related

How to get an Instance of IWebBrowser2

I am a bit new to C++, please be gentle.
I am trying to automate Internet Explorer. I have a simple Win32 console application where I am trying to create an instance of IE using a local server.
However, my call to CoCreateInstance() doesn't return an object to initialize my IWebBrowser2 variable.
I could use some help to see what I am missing.
Here is my code:
HRESULT InstanciateIEResult;
HRESULT NavigateResult;
HRESULT ShowBrowserResult;
VARIANT * empty = new VARIANT();
BSTR URL = L"bing.com";
IWebBrowser2* pBrowser2;
InstanciateIEResult = CoCreateInstance(CLSID_InternetExplorer, NULL, CLSCTX_LOCAL_SERVER,
IID_IWebBrowser2, (void**)&pBrowser2);
if(pBrowser2)
{
//never reach here
NavigateResult = pBrowser2->Navigate(URL, empty, empty, empty, empty);
ShowBrowserResult = pBrowser2->put_Visible(VARIANT_TRUE);
}
I am also not sure how to decode what the HRESULT returns. If you know, that would be helpful as well.
I was looking at documentation on IWebBrowser2 interface and CoCreateInstance.
You need to call CoInitialize() before using COM objects.
Also, you need to use SysAllocString() to allocate the string.
Example:
#include <windows.h>
#include <MsHTML.h>
#include <Exdisp.h>
#include <ExDispid.h>
int WINAPI wWinMain(HINSTANCE, HINSTANCE, LPTSTR, int)
{
CoInitialize(NULL);
HRESULT InstanciateIEResult;
HRESULT NavigateResult;
HRESULT ShowBrowserResult;
VARIANT empty;
VariantInit(&empty);
IWebBrowser2* browser = NULL;
HRESULT hr = CoCreateInstance(CLSID_InternetExplorer, NULL,
CLSCTX_LOCAL_SERVER, IID_IWebBrowser2, (void**)&browser);
if (browser)
{
BSTR URL = SysAllocString(L"bing.com");
NavigateResult = browser->Navigate(URL, &empty, &empty, &empty, &empty);
SysFreeString(URL);
ShowBrowserResult = browser->put_Visible(VARIANT_TRUE);
browser->Release();
}
CoUninitialize();
return 0;
}

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.

Deleting WMI instance with C++

I have found some samples with C# and VBS for WMI instance deletion, however I need this implemented with C++.
My sample code:
CoInitialize(NULL);
HRESULT hRes;
//Obtain the initial locator to WMI
CComPtr<IWbemLocator> pLoc = NULL;
hRes = CoCreateInstance(CLSID_WbemLocator, NULL, CLSCTX_INPROC_SERVER, IID_IWbemLocator, (LPVOID*) &pLoc);
if(FAILED(hRes))
return 1;
//Connect to WMI through the IWbemLocator::ConnectServer method
CComPtr<IWbemServices> pSvc = NULL;
//Connect to the root namespace with the current user and obtain pointer pSvc to make IWbemServices calls.
hRes = pLoc->ConnectServer(L"ROOT\\SUBSCRIPTION", NULL, NULL, 0, NULL, 0, 0, &pSvc);
if(FAILED(hRes))
return 1;
hRes = pSvc->DeleteInstance(
L"CommandLineEventConsumer.Name='{709782F3-E860-488E-BD8A-89FBC8C1495C}'",
WBEM_FLAG_RETURN_IMMEDIATELY, NULL, NULL);
return 0;
According to that I've found here and here, my code should work. I surely have CommandLineEventConsumer named {709782F3-E860-488E-BD8A-89FBC8C1495C}
And my code fails on IWbemServices::DeleteInstance, error code 0x80041008 (One of the parameters to the call is not correct).
I would appreciate if someone spot mistake in my code. Or maybe some privileges are required to do that?
The first parameter to IWbemServices::DeleteInstance is a BSTR. A BSTR is different from a UTF-16 encoded C-style string in that it stores an explicit length argument. Even though BSTR is of type wchar_t*, you cannot pass a plain string literal in place of a BSTR.
To create a BSTR from a string literal you need to call SysAllocString:
BSTR objPath = ::SysAllocString(L"CommandLineEventConsumer.Name='{709782F3-E860-488E-BD8A-89FBC8C1495C}'");
hRes = pSvc->DeleteInstance(
objPath,
WBEM_FLAG_RETURN_IMMEDIATELY, NULL, NULL);
::SysFreeString(objPath);
Alternatively, since you are already using ATL for CComPtr you could use CComBSTR to make your life easier:
CComBSTR objPath(L"CommandLineEventConsumer.Name='{709782F3-E860-488E-BD8A-89FBC8C1495C}'");
hRes = pSvc->DeleteInstance(
objPath,
WBEM_FLAG_RETURN_IMMEDIATELY, NULL, NULL);
Note: IWbemLocator::ConnectServer also needs BSTRs as parameters. The sample provided on the documentation page does pass a plain C-style string, so maybe the IWbemLocator interface is more forgiving when presented invalid parameters.
I have found two solutions:
1.Remove WBEM_FLAG_RETURN_IMMEDIATELY flag.
_bstr_t objPath(L"CommandLineEventConsumer.Name='{709782F3-E860-488E-BD8A-89FBC8C1495C}'");
hRes = pSvc->DeleteInstance(objPath, 0, NULL, NULL);
2.Pass IWbemCallResult for result.
_bstr_t objPath(L"CommandLineEventConsumer.Name='{709782F3-E860-488E-BD8A-89FBC8C1495C}'");
CComPtr<IWbemCallResult> pRes = NULL;
hRes = pSvc->DeleteInstance(objPath, WBEM_FLAG_RETURN_IMMEDIATELY, NULL, &pRes);
Didn't investigate a lot, but it works both ways. Looks like specs aren't 100% correct.

Accessing raw wav data from a directshow filter graph

The following code will quite happily load in an mp3 and play it, but I need to retrieve the raw wav data, as float if possible. How can I do that? I kind of know how to do it using IAMMultiMediaStream but Microsoft suggests using a filter on the graph is the preferred way, I just can't find any documentation or examples. I would have thought playing the sound file is the hardest part and that accessing the raw data would be easy.
IGraphBuilder* pGraph;
// Use its member function CoCreateInstance to
//create the COM object and obtain the IGraphBuilder pointer.
hr = CoCreateInstance(CLSID_FilterGraph, NULL, CLSCTX_INPROC_SERVER, IID_IGraphBuilder, (void **)&pGraph);
if(FAILED(hr)){ /*... handle hr error*/ }
// Use the overloaded -> operator to call the interface methods.
std::wstring ws;
ws.assign(rInFilename.begin(), rInFilename.end());
hr = pGraph->RenderFile(ws.c_str(), NULL);
if(FAILED(hr)){ /*... handle hr error*/ }
// Declare a second smart pointer and use it to
// obtain another interface from the object.
IMediaControl* pControl;
IMediaEvent* pEvent;
hr = pGraph->QueryInterface(IID_IMediaControl, (void **)&pControl);
hr = pGraph->QueryInterface(IID_IMediaEvent, (void **)&pEvent);
// Use the second interface.
hr = pControl->Run();
if(FAILED(hr)){ /*... handle hr error*/ }
// Use the third interface.
long evCode = 0;
pEvent->WaitForCompletion(INFINITE, &evCode);
edit: I should link to microsoft documentation showing how to do this using streams
https://msdn.microsoft.com/en-us/library/windows/desktop/dd317593%28v=vs.85%29.aspx

Returning COM object to JScript

I am trying to pass a COM object from an ActiveX component to JScript. So far I have tried the following method of doing so:
STDMETHODIMP CHSNetwork::CreateIPPPacket(VARIANT ** ppv)
{
IIPPacket *iipp;
HRESULT hr = CoCreateInstance(CLSID_IPPacket, NULL, CLSCTX_ALL, IID_IIPPacket, (void **)&iipp);
if(SUCCEEDED(hr) && ppv)
{
CComVariant cvar((IUnknown *)iipp);
hr = cvar.Detach(*ppv);
}
return hr;
}
The following JScript causes the error Variable uses an Automation type not supported in JScript:
var hsn = new ActiveXObject("ZIENetwork.HSNetwork");
var ipp = hsn.CreateIPPPacket();
Any help would be greatly appreciated. Thanks.
Ah. I got it. I needed to pass a VARIANT * not a VARIANT **. I guess I still get confused by pointers-to-pointers as it relates to return values with COM.
Thus the correct code is:
STDMETHODIMP CHSNetwork::CreateIPPPacket(VARIANT * ppv)
{
// TODO: Add your implementation code here
IIPPacket *iipp;
HRESULT hr = CoCreateInstance(CLSID_IPPacket, NULL, CLSCTX_ALL, IID_IIPPacket, (void **)&iipp);
if(ppv)
{
CComVariant cvar((IUnknown *)iipp);
hr = cvar.Detach(ppv);
}
return hr;
}
Of course if you only ever pass back an interface you can pass back a IDispatch** or IUnknown**, you don't need the VARIANT, just a thought :)