Consuming COM class ;ibrary with unmanged "C" application

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.

VB.NET code:

<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"
#End Region

    ' 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()
    End Sub

    Public ReadOnly Property MyName() As String
            Return "XMLReader"
        End Get
    End Property
End Class

And then in my 'C" app I have gthe following lines:

#import "..\XMLReader_LINQ\bin\debug\XMLReader_LINQ.tlb" named_guids

IDispatch      *pMyInterface ;

CoInitialize(NULL) ;
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.

Many thanks,
Sid PriceSoftware Systems Architect/DesignerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Daniel Van Der WerkenIndependent ConsultantCommented:
Here's a pointer to creating and using a COM Callable Wrapper which is what I think you want to work with:

Here is an example of calling a COM object from C++ code:

Here is the Stack Overflow answer that includes the link from above:
Sid PriceSoftware Systems Architect/DesignerAuthor Commented:
Thanks for the quick reply, however I don't think these links help me.

I have a "C" application and a COM enabled class library. I thought my code was fairly close. Are you saying that I am way off track? Do I have to call the COM DLL in order to have it call the .NET code? That seems like a load of work.

Daniel Van Der WerkenIndependent ConsultantCommented:
I thought you were writing a .NET assembly and calling it from C. If this is just straight COM/C then, yes, I gave you information you probably don't need. I'm not sure where you're defining the IID values that are being reported as undefined. I am pretty sure you need to set those values to the proper GUIDs before you make the call. Also, make sure the COM object is properly registered with regsvr32. There is a regasm program for assemblies, but I guess you're not doing that.
C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Sid PriceSoftware Systems Architect/DesignerAuthor Commented:
You will see in my original post that the type library (TLB file) is imported into the application. This process generates two files, one of them is a TLH file and it is from this file that one is supposed to discover the IDs required. This is the TLH file generated when I compile my application:

// Created by Microsoft (R) C/C++ Compiler Version 15.00.30729.01 (efdf8a21).
// c:\data_root\projects\idb\idb\idb_engine\debug\xmlreader_linq.tlh
// C++ source equivalent of Win32 type library ..\XMLReader_LINQ\bin\debug\XMLReader_LINQ.tlb
// compiler-generated file created 04/01/12 at 09:56:21 - DO NOT EDIT!

#pragma once
#pragma pack(push, 8)

#include <comdef.h>

namespace XMLReader_LINQ {

// Forward references and typedefs

struct __declspec(uuid("a0143e31-cdd1-491d-9c5b-32bf5610a0cf"))
/* LIBID */ __XMLReader_LINQ;
struct /* coclass */ XMLReader;
struct __declspec(uuid("b5f352b3-b93d-4091-8f8f-b5b6698ca415"))
/* dual interface */ _XMLReader;

// Smart pointer typedef declarations

_COM_SMARTPTR_TYPEDEF(_XMLReader, __uuidof(_XMLReader));

// Type library items

struct __declspec(uuid("8fec2c9f-a761-4fec-b964-527183de0528"))
    // interface _Object
    // [ default ] interface _XMLReader

struct __declspec(uuid("b5f352b3-b93d-4091-8f8f-b5b6698ca415"))
_XMLReader : IDispatch
    // Property data

    _bstr_t MyName;

    // Wrapper methods for error-handling

    _bstr_t GetMyName ( );

    // Raw methods provided by interface

      virtual HRESULT __stdcall get_MyName (
        /*[out,retval]*/ BSTR * pRetVal ) = 0;

// Named GUID constants initializations

extern "C" const GUID __declspec(selectany) LIBID_XMLReader_LINQ =
extern "C" const GUID __declspec(selectany) CLSID_XMLReader =
extern "C" const GUID __declspec(selectany) IID__XMLReader =

// Wrapper method implementations

#include "c:\data_root\projects\idb\idb\idb_engine\debug\xmlreader_linq.tli"

} // namespace XMLReader_LINQ

#pragma pack(pop)

I just noticed that the file comment says it is generated for "C++", is this possibly my issue, some kind of name mangling? If so, how do I get the importer to generate a "C" compatible header file?

The registration of the COM assembly is a runtime requirement and does not affect the application compilation, so I think that is not an issue here.

It seems that this should have been a common requirement in the past and I am suprised that no simple examples are available.

Once again, any pointers would be much apprecited and thank you for taking the time to try and help,
Sid PriceSoftware Systems Architect/DesignerAuthor Commented:
Well I have discovered the "magic incantation" that not only allows my code to compile but it also works!

As I suspected from my extensive research the code I first posted was indeed close to the solution, all it took to get it to compile was to add the following line:

using namespace XMLReader_LINQ ;

This resolved the two parameters that were undefined.

The other issue to get the code to run and allow access to the property defined in the COM library was to change the interface pointer to the correct type, again I had previously had the correct type:

_XMLReader      *pMyInterface ;

Hopefully this post will be of some assistance should someone else be in a similar situation.

Thanks Dan7el for you efforts to help,

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Sid PriceSoftware Systems Architect/DesignerAuthor Commented:
It allowed me to resolve my issue completely.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.