I am new to COM, and I looked around on the internet to find how to create a COM class in C++ (to be used by C#). I've seen that in the .idl file, I must put something like this:
[
object,
uuid(a93164ee-e6d4-44be-aa27-f00ce6972719),
helpstring("interface ITopologyQuery")
]
interface ITopologyQuery : IUnknown
{
HRESULT LoadFile(BSTR completeFileName);
HRESULT SetParameters(int above, int percentage);
}
[
uuid(a958f2af-8b55-43c4-9fc3-c39d83fc1376)
]
library TopologyQueryLib
{
[
uuid(cc814992-31ec-4a1f-a41e-111ade27bdfe),
helpstring("TopologyQuery class")
]
coclass CTopologyQuery
{
[default] interface ITopologyQuery;
};
}
Now my question is, where do you define the CTopologyQuery class? If I define it as a regular c++ class in another file, will the compiler link my class correctly with the interface? The c++ class code looks like this (it is implemented in a .cpp file):
class CTopologyQuery : public ITopologyQuery
{
public:
__stdcall CTopologyQuery();
//IUnknown interface
HRESULT __stdcall QueryInterface(REFIID riid, void **ppObj);
ULONG __stdcall AddRef();
ULONG __stdcall Release();
//ITopologyQuery interface
HRESULT __stdcall LoadTopologyFile(BSTR completeFileName);
HRESULT __stdcall SetParameters(int above, int percentage);
private:
};
For now it compiles either if I put the library section in the .idl file or if I don't. I am a little lost as what is the good thing to do here? I understand that the coclass definition is supposed to give a default implementation to the interface, but for me it looks like an empty class with no methods...
A COM class ("coclass") is a concrete implementation of one or more interfaces, and closely resembles classes in object-oriented programming languages. Classes are created based on their class ID (CLSID) or based on their programmatic identifier string (ProgID).
An implementation file is used in C++ programming when creating a class definition to split the interface from the implementation. The header file would declare all the member functions (methods) and data methods (fields) that the class has.
The IDL (Interface Definition Language) part defines the binary interface in a language-independent way that just looks a lot like C++.
It's generally used to build a binary type library, which is a DLL with info about your COM class. You can use Microsoft's COM/OLE Viewer to browse existing type libraries.
For C++ consumption of your non-IDispatch
COM class you don't need that info about your class, since all that the C++ compiler needs to know (as minimum) is in your C++ header file.
But the IDL description and type library generated from that helps to generate the glue code for using your COM class from C# and other languages. As I recall it can also help to generate the Windows registry keys and values needed to use your COM class via CoCreateInstance
and friends. So it's a bit of infra-structure machinery, so to speak, essentially supporting a language-independent view and usage of your COM class.
Cheers & hth.,
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With