Link to home
Start Free TrialLog in
Avatar of lgadi
lgadi

asked on

Calling a COM written in VB from a C program

Hi
 
   I have a COM object in VB, and I need
   to call it from a C program.
   Does anyone know how to do that?
     -= Gadi =-

Avatar of pjknibbs
pjknibbs

If the COM object was written to use Automation standards (likely, given it's a VB COM object), you can call it the same as you would any other COM interface.
Avatar of lgadi

ASKER

Okay, but that's what I can't seem to get done. I'm not sure which APIs to
use (I was told about the CComPtr MFC object, but I need to use only C).
A short example of how it might be done could be great!
Have you ever used COM before? I just ask to find out what level of detail you need!
ASKER CERTIFIED SOLUTION
Avatar of jhance
jhance

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lgadi

ASKER

jhance: I haven't tried your solution yet, as soon as I will, I'll let you know the results.
pjknibbs: Well, I've created a COM object using C++, but I've only invoked it using VB, and the whole thing is pretty much encapsulated there...So, I never did go through the entire process in C.
That easy in C ?
The class stuff and so on maybe hard to around or create it manually by struct.
Avatar of lgadi

ASKER

jhance: Okay, I started following your directions, but as I got to step 3, I typed CoCreateInstance, and the 3rd parameter there is REFIID, and the compiler says it must be initialized. The problem is I don't know how to initialize it... I think a wokring example (if possible) will prevent alot of problems, if none is available, I will happily settle for an explenation.
Actually the REFIID is the FOURTH parameter of CoCreateInstance, and it's the GUID that uniquely identifies your Visual BASIC COM object. To clarify, all COM objects must be identified by a Globally Unique Identifier, a 128-bit number assigned when the control was created in the first place. You need to know this value to instantiate a COM object. If you don't already know this value for your COM object I'm not sure how you'd find it out without searching the registry.
lgadi,

In almost all cases, refiid for CoCreateInstance will be IID_IUnknown.

Use it like this:

CLSID myClsid;
LPUNKNOWN pUnk;

if(!SUCCEEDED(CLSIDFromProgID(L"ObjectName.ControlName.1", &myClsid))){

 // Error
}

if(CoCreateInstance(myClsid, NULL, CLSCTX_ALL, IID_IUnknown,  (LPVOID *)&pUnk) != S_OK){

  // Error
}
else{
  // OK
}