PRB: Range::GetApplication Method Returns an Incorrect Interface Pointer (278949)
The information in this article applies to:
- Microsoft Excel 2000 Service Release 1 (SR-1)
- Microsoft Excel 2000
- Microsoft Excel 97 for Windows
This article was previously published under Q278949 SYMPTOMS
When a developer uses the #import functionality of Microsoft Visual C++ to generate smart pointers for Excel and then uses those smart pointers to access the pointer returned by the Range::GetApplication property, an access violation occurs.
This problem does not occur in Microsoft Visual Basic applications, but can be observed in Component Object Model (COM) code that does not use #import.
CAUSE
The type library for Excel indicates that the CoClass "Application" is returned by the Range::GetApplication method. COM methods return only interface pointers, not CoClass pointers. Because of this, Microsoft Visual C++ concludes that the interface pointer returned must be for the default interface of the Application CoClass, _Application.
The _Application interface is dual, meaning that it can be called either through the IDispatch interface, or directly through the vtable. However, the interface returned from Range::GetApplication does not support vtable binding. When the wrappers generated by #import attempt to make a vtable call, an access violation results.
This problem is not seen elsewhere in Excel.
RESOLUTION
To work around the problem, call IUnknown::QueryInterface() on the interface pointer returned from Range::GetApplication() before using it. The following code demonstrates the workaround:
_ApplicationPtr appPtr2;
IUnknown* pUnk = NULL;
HRESULT hr = xlRange->GetApplication()->QueryInterface( __uuidof(_Application), (void**)&pUnk );
if (SUCCEEDED(hr)){
appPtr2 = pUnk;
appPtr2->PutVisible(0, TRUE );
}
Modification Type: | Major | Last Reviewed: | 12/12/2003 |
---|
Keywords: | kbAutomation kbprb kbProgramming KB278949 |
---|
|