Using a VB-COM Object in C++
I have a Com Component called "COMTEST"
It has a Class called "MATH"
I want to instantanite an object of the type COMTEST.MATH, in my Plain Vanilla C++ Program,and use the method ADD of the MATH object which takes two long values and returns a long Can someone please write some sample code for this
URGENT PROJECT on hold for this
I tried with CoCreateInstance
The last parameter of CoCreateInstance is an [out] parameter in which a refernce to the object is returned
What should I declare this variable as?
Since this COM DLL is made in VB there is no Header file
Can't I do somrething like CreateObject( that i do in VB) here ?
Regards Vijay
Re: Using a VB-COM Object in C++
Vijay,
You can try the following:
#pragma warning (disable:4146)
#import "<path>\ComTest.dll" rename_namespace("ComTest")
using namespace ComTest;
The above will create 2 new files in your Release/Debug directory
depending on which target you're compiling to:
ComTest.tlh - ComTest classes
ComTest.tli - ComTest automation interface implementation.
For each class in ComTest you will find in the .tlh file something
like this:
struct __declspec(uuid("5fdc716a-8c81-11d3-8e8b-0000f6cdcd22"))
/* dual interface */ _clsMath;
struct /* coclass */ clsMath;
You can then define a variable in your own class:
_clsMathPtr m_clsMath;
You then implement it:
HRESULT hr = m_clsMath.CreateInstance( __uuidof( clsMath ));
if ( HRESULT_SEVERITY(hr) != 0 )
{
// ... Handle error
}
// ... Your code, etc.
long lLong1 = 1, lLong2 = 2;
long m_clsMath->ADD( &lLong1, &lLong2 );
NOTE: _clsMathPtr. In order for you to use the _clsMath, you need to to add the Ptr to the class name to use it.
NOTE: In the CreateInstance line that the __uuidof keyword uses
"clsMath" and not "_clsMath."
Hope this helps!
W Dicks
"Man is certainly stark mad: he cannot make a flea,
yet he makes gods by the dozens."