Consuming COM class ;ibrary with unmanged "C" application
Posted on 2012-03-31
I am working on a project that is a "C" language application to which I need to add some fairly complex XML parsing. I really like LINQ to XML and would like to build a COM class library to do the XML work so that I can use LINQ. THis means I need to access the class library from my unmanaged "C" application.
I understand that I need to call CoInitialize and then get a pointer to my DLL using CoCreateInstance and this is where I am so far failing. The code below is from the VB.NET COM enabled class library and also a snippet of the consuming application code. What I seem to be unable to do is to figure out the type of the pointer needed for the interface and also the CLSID and interface IID, this inspite of importing the TLB file into my source.
<ComClass(XMLReader.ClassId, XMLReader.InterfaceId, XMLReader.EventsId)> _
Public Class XMLReader
#Region "COM GUIDs"
' These GUIDs provide the COM identity for this class
' and its COM interfaces. If you change them, existing
' clients will no longer be able to access the class.
Public Const ClassId As String = "8fec2c9f-a761-4fec-b964-527183de0528"
Public Const InterfaceId As String = "b5f352b3-b93d-4091-8f8f-b5b6698ca415"
Public Const EventsId As String = "bc3078e2-b563-42b8-8c03-2a02e0bbeacf"
' A creatable COM class must have a Public Sub New()
' with no parameters, otherwise, the class will not be
' registered in the COM registry and cannot be created
' via CreateObject.
Public Sub New()
Public ReadOnly Property MyName() As String
And then in my 'C" app I have gthe following lines:
#import "..\XMLReader_LINQ\bin\debug\XMLReader_LINQ.tlb" named_guids
IDispatch *pMyInterface ;
hr = CoCreateInstance(CLSID_XMLReader,NULL,CLSCTX_INPROC_SERVER,IID__XMLReader,(void**)&pMyInterface) ;
I really am not sure that the interface type is "IDispatch", although the line does compile. Also the compiler reports that both "CLSID_XMLReader" and "IID__XMLReader" are undefined. Any pointers to how to move on from here would be much appreciated. This is such an old technique that finding any simple "howtos" on the Internet is proving impossible.