The Design of Software (CLOSED)

A public forum for discussing the design of software, from the user interface to the code architecture. Now closed.

The "Design of Software" discussion group has been merged with the main Joel on Software discussion group.

The archives will remain online indefinitely.

Calling ActiveX from "Plain C"

Hello All,

We sell a device for which we supply an ActiveX driver.  Most customers use the ActiveX in "normal" Windows apps to set up the device and get data from it.

A potential customer wants to add our device to an existing "plain C" app (MS C 5.1? but they run on Windows).  They only need access to one method of the ActiveX and a handful of properties.  They're willing to do device setup, etc., using our utilities outside of their app but want to collect data using their app.

Our research so far has only found suggestions of solutions that require nitty-gritty understanding of the COM / ActiveX interface details, but our shop lacks that (the ActiveX is written in VB 6) and it looks like a potential swamp.  So we're looking for a solution a little higher up the abstraction ladder.

Any thoughts?  We've considered:
- wrap the ActiveX in a DLL, and let them call the DLL. 
- set up a "server" app using using our code, and communicate using the file system, Winsock, etc.
- give them the communications protocols and let them write the interface
- get lucky on JoS

rkj Send private email
Thursday, December 01, 2005
This assumes when you say 'plain c' you are talking Visual Studio 6 or later (for method 1) or probably any for method 2.

The trick is you need to get the com interface defined in your c-code.  Couple possible ways.

1) (easiest) use the #import statement in their c-code.

#import will generate a MyDll.tlh/MyDll.tli file that you can hunt thru to find your interface names.

They would do something like the following in their code:
    #import "MyDll.dll" no_namespace

To get the interface:
  CoInitialize();  // make sure COM is initialized

  IMyInterface  *pIMyInterface;
  CLSID    clsid = {0};
  HRESULT  hr;

  // if you dont know the clsid you can grab it with the progid
  CLSIDFromProgID( L"MyCompany.InterfaceName", &clsid );

  // create the instance
  hr = CoCreateInstance( clsid, NULL, CLSCTX_ALL, __uuidof(IMyInterface), (void **) &pIMyInterface );

  //  should be able to call the function

(2) If they cant use the #import statement you can get the idl/tlb file by using the OLEVIew tool. 
Run OleView, Choose File, View TypeLib.  Browse to your DLL.  In ole view you will get a representation of the idl file. 

Save out the idl file. 

run the midl compiler against it to generate the header files.  You should be able to use these as described above by including the generate header file instead of importing the .dll directly.

3) Get the book Essential COM and code up the interface by hand and give them the header file and do above.
MikeG Send private email
Thursday, December 01, 2005
MikeG, you've got the right idea, but everything you've written is C++, not C.

You can't do pInterface->MyMethod() in C. The call looks more like pInterface->vtbl->MyMethod().

If you have an IDL file, the MIDL compiler will spit out a header file that can be used to call a COM interface from C. COM was defined so that you can call it directly from C, but it's kind of annoying.

If they have COM expert on staff (doubtful) they should be able to use your ActiveX component with ease. It sounds like you don't have such an expert on staff either, so you're in trouble on this one.

The plain DLL approach sounds reasonable, but without more details on what the COM interface looks like, it's hard to make a recommendation.
Chris Tavares Send private email
Thursday, December 01, 2005
There's an old program from Microsoft called "b2c". Don't know if it's still available, but it takes VB code that does COM and writes the equivalent plain-c version.

It doesn't produce clean code, but it does produce working code, and it is _mostly_ readable. It's a good starting point if you want to understand how to do COM from C.
Ori Berger
Thursday, December 01, 2005

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics
Powered by FogBugz