|
-
October 28th, 1999, 05:43 AM
#1
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
-
October 28th, 1999, 06:57 AM
#2
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."
-
October 28th, 1999, 02:08 PM
#3
I have done it
I found the Solution the IDespatch way
Here is the code
#include "stdafx.h"
#include "objbase.h"
#include "oleauto.h"
int main(int argc, char* argv[])
{
HRESULT hresult;
IUnknown * punk;
IDispatch * pdisp;
OLECHAR FAR* szMember = L"Add";
//This is the name of the function I am going to call
DISPID dispid;//This will hold the dispatch ID of the function
static VARIANTARG vArg[2];
DISPPARAMS dispparams;//={NULL, NULL, 0, 0}; //For holding parameters
VARIANT vResult;
EXCEPINFO excepinfo;//Information about Exception thrown
UINT nArgErr;//Are there errors in the argument
CLSID clsid;
hresult = OleInitialize(NULL);
// OLE function CoCreateInstance starts application using GUID.
CLSIDFromProgID(L"Comtest.Math",&clsid);
hresult = CoCreateInstance(clsid, NULL, CLSCTX_SERVER, IID_IUnknown, (void FAR* FAR*)&punk);
//Call QueryInterface to see if object supports IDispatch.
hresult = punk->QueryInterface(IID_IDispatch,(void**)&pdisp);
hresult = pdisp-> GetIDsOfNames(IID_NULL ,&szMember,1,LOCALE_USER_DEFAULT,&dispid);
//In the following call to Invoke,
//the DISPID indicates the property or method to invoke.
//To invoke a property or method that requires parameters, supply the
//parameters in the DISPPARAMS structure.
VariantInit(&vArg[0]);
VariantInit(&vArg[1]);
//Just In case u want to pass string to the VB Class
//vArg[0].vt=VT_BSTR;
//vArg[0].bstrVal =SysAllocString(L"Vijay");
//vArg[1].vt=VT_BSTR;
//vArg[1].bstrVal =SysAllocString(L"Vijay");
vArg[0].vt=VT_I4;
vArg[0].lVal =10;//Last Parameter
vArg[1].vt=VT_I4;
vArg[1].bstrVal =20//Second LAst Parameter
dispparams.rgvarg = vArg;
dispparams.cArgs = 2;
dispparams.cNamedArgs = 0;
// Invoke the method. Use defaults where possible.
hresult = pdisp->Invoke(dispid,IID_NULL,LOCALE_SYSTEM_DEFAULT,DISPATCH_METHOD,&dispparams,&vResult,&excepinfo,&nArgErr);
printf("\n The return Value is : %ld",vResult.lVal);
pdisp->Release();
printf("Hello World!\n");
return 0;
}
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|