Improve company productivity with a Business Account.Sign Up


Consuming COM class ;ibrary with unmanged "C" application

Posted on 2012-03-31
Medium Priority
Last Modified: 2012-04-06
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,
Question by:Sid Price
  • 4
  • 2
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 37791922
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:

Author Comment

by:Sid Price
ID: 37792088
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.

LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 37793032
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.
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions


Author Comment

by:Sid Price
ID: 37793353
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,

Accepted Solution

Sid Price earned 0 total points
ID: 37793689
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,

Author Closing Comment

by:Sid Price
ID: 37815439
It allowed me to resolve my issue completely.

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
Watch the video to know the process of migration of Exchange or Office 365 mailboxes in absence of MS Outlook. It is an eminent tool which can easily migrate Public, Archive user mailboxes from one another Exchange server and Office 365. Kernel Migr…

595 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question