You need to be more specific. There are two ways to interpret your question.
One is that you have a COM component (e.g. some library), and you want to programmatically enumerate interfaces/properties/methods of that. Usually (but not always), COM components come with a type library that contains full metainformation about all this - it's either embedded into the COM .exe or .dll, or is a separate .tlb file. In any case, if the COM component is properly registered in the system (e.g. using regsvr32), then COM provides a standard API to retrieve that type information, centered around ITypeLib and ITypeInfo interfaces.
Another interpretation is that you get a reference to a particular COM object, and you need to enumerate all methods/properties on that and/or invoke them by computed name, like Java or C# reflection, or Delphi RTTI. If so, then it is only possible if the COM object implements IDispatch interface, which allows you to do all of the above.
It's much more automatic than the other answers here are suggesting. All the boilerplate code is written for you by Visual Studio.
You're lucky you have the .idl, it's by far the most conveninent, I think.
You could paste the contents of the .idl file into your ATL COM project's existing .idl file, which would give you access to the declarations in it. For example, something like this could be pasted into an IDL file:
[
object,
uuid(ecaac0b8-08e6-45e8-a075-c6349bc2d0ac),
dual,
nonextensible,
helpstring("IJim Interface"),
pointer_default(unique)
]
interface IJim : IDispatch
{
[id(1), helpstring("method SpliceMainbrace")] HRESULT SpliceMainbrace(BSTR* avast);
};
Then in Class View, right click your class and choose Add | Implement Interface
.
Notice that in this dialog, you can actually browse for a .tlb file, but I think it's better to have plain-text source for these things, for version control and the like.
Pick IJim
from the list, press the >
button to add it to the list to be implemented. Press Finish.
Visual Studio will add this to your class (along with a bunch of other crap to make it work):
// IJim Methods
public:
STDMETHOD(SpliceMainbrace)(BSTR * avast)
{
// Add your function implementation here.
return E_NOTIMPL;
}
Best Answer
You can enumerate the methods an
IDispatch
exposes through the type info. There are two ways to get the type info:IDispatch::GetTypeInfo
.Unfortunately, an
IDispatch
implementation is not obligated to provide type info about the methods and properties it implements.If it does, however, the basic enumerating involves calling
ITypeInfo::GetTypeAttr
to get theTYPEATTR
for the interface and looking at the number of implemented methods (cFuncs
) and variables (cVars
) and looping over these and callingITypeInfo::GetFuncDesc()
orITypeInfo::GetVarDesc()
. Of course, there are lot more details you will have to deal with as I can list here, but this should be a good starting point for your exploration.Here's a nice article explaining the process in more details with code in VB.Net.