Solved

Consuming COM class ;ibrary with unmanged "C" application

Posted on 2012-03-31
6
335 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 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:
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 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.
0
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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…

830 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