Solved

Calling a COM written in VB from a C program

Posted on 2000-03-12
10
191 Views
Last Modified: 2010-04-15
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 =-

0
Comment
Question by:lgadi
  • 3
  • 3
  • 2
  • +1
10 Comments
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2611346
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.
0
 

Author Comment

by:lgadi
ID: 2611519
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!
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2611579
Have you ever used COM before? I just ask to find out what level of detail you need!
0
ScreenConnect 6.0 Free Trial

Want empowering updates? You're in the right place! Discover new features in ScreenConnect 6.0, based on partner feedback, to keep you business operating smoothly and optimally (the way it should be). Explore all of the extras and enhancements for yourself!

 
LVL 32

Accepted Solution

by:
jhance earned 100 total points
ID: 2612808
Basically the process is as follows:

1) Call OleInitialize() or CoInitialize() from your program.

2) Use CLSIDFromProgID() (usually) to get the CLSID for the object from its "friendly" name.

3) Use CoCreateInstance() to get the object's IUnknown interface.

4) Use IUnknown::QueryInterface() using the pointer returned in #3 to get a pointer to the object's IDispatch interface.  This _IS_ the AUTOMATION interface pointer and is the key to the rest of this.

5) Use IDispatch::GetIDsOfNames() to get the dispIDs for all of the methods and properties on the Automation interface you'll be using from your C program.

6)Use IDispatch::Invoke() to actually call methods or properties on the interface.

7) when you're done, don't forget to call IDispatch::Release(), IUnknown::Release(), and CoUninitialize().

 
0
 

Author Comment

by:lgadi
ID: 2613644
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.
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2615979
That easy in C ?
0
 
LVL 5

Expert Comment

by:Wyn
ID: 2615982
The class stuff and so on maybe hard to around or create it manually by struct.
0
 

Author Comment

by:lgadi
ID: 2619033
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.
0
 
LVL 12

Expert Comment

by:pjknibbs
ID: 2619059
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.
0
 
LVL 32

Expert Comment

by:jhance
ID: 2619134
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
}
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
voltage to force translation ? 8 99
Understanding the meaning of Portability in Embedded System Programming 8 141
convert char array to number in c 5 85
What is sub-make ? 2 58
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
The goal of this video is to provide viewers with basic examples to understand recursion in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and reading files in the C programming language.

786 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