Solved

Consuming COM class ;ibrary with unmanged "C" application

Posted on 2012-03-31
6
332 Views
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()
        MyBase.New()
    End Sub

    Public ReadOnly Property MyName() As String
        Get
            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.
0
Comment
Question by:Sid Price
  • 4
  • 2
6 Comments
 
LVL 19

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:
http://msdn.microsoft.com/en-us/library/ms973802.aspx

Here is an example of calling a COM object from C++ code:
http://www.codeproject.com/Articles/3906/Unmanaged-to-Managed-Calls/

Here is the Stack Overflow answer that includes the link from above:
http://stackoverflow.com/questions/530244/calling-c-sharp-from-c-com-add-in
0
 

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.

Sid.
0
 
LVL 19

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.
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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"))
XMLReader;
    // interface _Object
    // [ default ] interface _XMLReader

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

    __declspec(property(get=GetMyName))
    _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 =
    {0xa0143e31,0xcdd1,0x491d,{0x9c,0x5b,0x32,0xbf,0x56,0x10,0xa0,0xcf}};
extern "C" const GUID __declspec(selectany) CLSID_XMLReader =
    {0x8fec2c9f,0xa761,0x4fec,{0xb9,0x64,0x52,0x71,0x83,0xde,0x05,0x28}};
extern "C" const GUID __declspec(selectany) IID__XMLReader =
    {0xb5f352b3,0xb93d,0x4091,{0x8f,0x8f,0xb5,0xb6,0x69,0x8c,0xa4,0x15}};

//
// 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.
0
 

Accepted Solution

by:
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,
Sid.
0
 

Author Closing Comment

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

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

What my article will show is if you ever had to do processing to a listbox without being able to just select all the items in it. My software Visual Studio 2008 crystal report v11 My issue was I wanted to add crystal report to a form and show…
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…
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. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

708 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now