The C++ project must somewhere include the following line, in order to pull in the CoClass and Interface definitions for the wrapper object:
#import "IDLexFoo.tlb" no_namespace no_implementation raw_interfaces_only named_guids
For details about the object parameters, see Sample IDL Object.
Initiation Without Parameters in C++
Use the following code to initialize the object with no parameters.
CComPtr<IIDLexFoo> spFoo
if ( FAILED(spFoo.CoCreateInstance( uuidof(IDLexFoo)) || !spFoo)) return E_FAIL
CComVariant vtNULL(0)
HRESULT hr = spFoo->CreateObject(0,vtNULL,vtNULL)
if ( FAILED(hr) )
{
CComBSTR bstrErr
spFoo->GetLastError(&bstrErr)
return E_FAIL
}
Initiation with Parameters in C++
Use the following code to initialize the object with its three parameters (a string, a 32-bit long value, and an array that has two rows and three columns, containing 32- bit long values).
CComPtr<IIDLexFoo> spFoo
if ( FAILED(spFoo.CoCreateInstance( uuidof(IDLexFoo)) || !spFoo)) return E_FAIL
CComSafeArrayBound bound[2]
bound[0].SetLowerBound(0)
bound[0].SetCount(2)
bound[1].SetLowerBound(0)
bound[1].SetCount(3)
CComSafeArray<VARIANT> parmArr(bound,2)
long ndx[2]
long lData[2][3] = { {10, 11, 12}, {20, 21, 22} }
for ( int i = 0
{
for ( int j = 0
{
ndx[0] = i
parmArr.MultiDimSetAt(ndx, CComVariant(lData[i][j]))
}
}
CComBSTR parmStr = "I am a string parameter"
CComVariant parmVal = (long)24
CComSafeArray<VARIANT> argval(3)
CComSafeArray<long> argpal(3)
argval[0] = parmStr
argpal[0] = IDLBML_PARMFLAG_CONST
argval[1] = parmVal
argpal[1] = IDLBML_PARMFLAG_CONST
argval[2] = parmArr
argpal[2] = IDLBML_PARMFLAG_CONST | IDLBML_PARMFLAG_CONVMAJORITY
long argc = 3
CComVariant vargval = argval
CComVariant vargpal = argpal
HRESULT hr = spFoo->CreateObject(argc,vargval,vargpal)
if ( FAILED(hr) )
{
CComBSTR bstrErr
spFoo->GetLastError(&bstrErr)
return E_FAIL
}