M
Mousam
Hi,
I am looking to develop a COM add-in using C++. For this I tried to
debug/understand the sample code provided by MS on http://support.microsoft.com/kb/230689.
I came across a strange behavior while debugging this sample code. It
would be great if some body can please explain me this behavior.
Let me try to explain the problem in detail.
The add-in provided in above sample is basically a COM add-in using C+
+. This add-in adds a command bar named "MyBar" to the office
application and adds a single button (caption is "My Button") to the
"MyBar" in the "OnStartupComplete" method. It also handles the click
event of "My Button" button and in the handler of click event it just
pops up a message box. This all works just fine.
Now for debugging purpose I made some modification to this code. In
the "OnStartupComplete" routine, just after adding new button (caption
"My Button"), I used "Item" property of CommandBarControls class to
get the dispatch pointer to newly added button. This propert
successfully returns me the IDispatch * but what I found is the
returned IDispatch* is different from what I get as the return value
of "Add" method. I want to know why I am getting two different
IDispatch * for same CommandBarButton object? For ease of
understanding I am also providing the piece of code:
STDMETHODIMP CMyAddin::OnStartupComplete(LPSAFEARRAY* custom)
{
//All the required variables are declared and initialized
properly here.
//m_pOurCmdBar is the IDispatch * for newly added commandbar
("MyBar")
hr = GetProperty(m_pOurCmdBar, L"Controls", &vtCtrls);
if (FAILED(hr)) goto cleanup;
//Add the button to the "MyBar" command bar
vtParam.vt = VT_I4; vtParam.lVal = 1;
hr = CallMethod(vtCtrls.pdispVal, L"Add", &vtButton, 1,
&vtParam);
if (FAILED(hr)) goto cleanup;
//Set Caption of button to the "My Button"
vtParam.vt = VT_BSTR; vtParam.bstrVal
= ::SysAllocString(L"My Button");
hr = PutProperty(vtButton.pdispVal, L"Caption", &vtParam);
VariantClear(&vtParam);
if (FAILED(hr)) goto cleanup;
//Set style property to msoButtonCaption
vtParam.vt = VT_I4; vtParam.lVal = 2;
hr = PutProperty(vtButton.pdispVal, L"Style", &vtParam);
if (FAILED(hr)) goto cleanup;
//Set visible property to TRUE.
vtParam.vt = VT_I4; vtParam.lVal = 1;
hr = PutProperty(vtButton.pdispVal, L"Visible", &vtParam);
if (FAILED(hr)) goto cleanup;
//Set Tag property
vtParam.vt = VT_BSTR; vtParam.bstrVal = ::SysAllocString(L"Unique
My Button Tag");
hr = PutProperty(vtButton.pdispVal, L"Tag", &vtParam);
if (FAILED(hr)) goto cleanup;
//Find our new button that we just added.
vtParam.vt = VT_BSTR; vtParam.bstrVal = ::SysAllocString(L"My
Button");
hr = CallMethod(vtCtrls.pdispVal, L"Item", &vtButton1, 1,
&vtParam);
VariantClear(&vtParam);
if (FAILED(hr)) goto cleanup;
if(vtButton1.pdispVal == vtButton.pdispVal)
{
::MessageBox(NULL, "Found interface and created interface
are same.", NULL, MB_OK);
}
else
{
::MessageBox(NULL, "Found interface and created interface
are different.", NULL,
MB_OK);
}
//Rest of the code goes here
} //End of CMyAddin::OnStartupComplete
I am getting the message as "Found interface and created interface are
different.", why it is happening?
Any help or pointer would be greatly appreciated.
Thanks & Regards,
Mousam Dubey
I am looking to develop a COM add-in using C++. For this I tried to
debug/understand the sample code provided by MS on http://support.microsoft.com/kb/230689.
I came across a strange behavior while debugging this sample code. It
would be great if some body can please explain me this behavior.
Let me try to explain the problem in detail.
The add-in provided in above sample is basically a COM add-in using C+
+. This add-in adds a command bar named "MyBar" to the office
application and adds a single button (caption is "My Button") to the
"MyBar" in the "OnStartupComplete" method. It also handles the click
event of "My Button" button and in the handler of click event it just
pops up a message box. This all works just fine.
Now for debugging purpose I made some modification to this code. In
the "OnStartupComplete" routine, just after adding new button (caption
"My Button"), I used "Item" property of CommandBarControls class to
get the dispatch pointer to newly added button. This propert
successfully returns me the IDispatch * but what I found is the
returned IDispatch* is different from what I get as the return value
of "Add" method. I want to know why I am getting two different
IDispatch * for same CommandBarButton object? For ease of
understanding I am also providing the piece of code:
STDMETHODIMP CMyAddin::OnStartupComplete(LPSAFEARRAY* custom)
{
//All the required variables are declared and initialized
properly here.
//m_pOurCmdBar is the IDispatch * for newly added commandbar
("MyBar")
hr = GetProperty(m_pOurCmdBar, L"Controls", &vtCtrls);
if (FAILED(hr)) goto cleanup;
//Add the button to the "MyBar" command bar
vtParam.vt = VT_I4; vtParam.lVal = 1;
hr = CallMethod(vtCtrls.pdispVal, L"Add", &vtButton, 1,
&vtParam);
if (FAILED(hr)) goto cleanup;
//Set Caption of button to the "My Button"
vtParam.vt = VT_BSTR; vtParam.bstrVal
= ::SysAllocString(L"My Button");
hr = PutProperty(vtButton.pdispVal, L"Caption", &vtParam);
VariantClear(&vtParam);
if (FAILED(hr)) goto cleanup;
//Set style property to msoButtonCaption
vtParam.vt = VT_I4; vtParam.lVal = 2;
hr = PutProperty(vtButton.pdispVal, L"Style", &vtParam);
if (FAILED(hr)) goto cleanup;
//Set visible property to TRUE.
vtParam.vt = VT_I4; vtParam.lVal = 1;
hr = PutProperty(vtButton.pdispVal, L"Visible", &vtParam);
if (FAILED(hr)) goto cleanup;
//Set Tag property
vtParam.vt = VT_BSTR; vtParam.bstrVal = ::SysAllocString(L"Unique
My Button Tag");
hr = PutProperty(vtButton.pdispVal, L"Tag", &vtParam);
if (FAILED(hr)) goto cleanup;
//Find our new button that we just added.
vtParam.vt = VT_BSTR; vtParam.bstrVal = ::SysAllocString(L"My
Button");
hr = CallMethod(vtCtrls.pdispVal, L"Item", &vtButton1, 1,
&vtParam);
VariantClear(&vtParam);
if (FAILED(hr)) goto cleanup;
if(vtButton1.pdispVal == vtButton.pdispVal)
{
::MessageBox(NULL, "Found interface and created interface
are same.", NULL, MB_OK);
}
else
{
::MessageBox(NULL, "Found interface and created interface
are different.", NULL,
MB_OK);
}
//Rest of the code goes here
} //End of CMyAddin::OnStartupComplete
I am getting the message as "Found interface and created interface are
different.", why it is happening?
Any help or pointer would be greatly appreciated.
Thanks & Regards,
Mousam Dubey