C++ Client Code Sample

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); // two rows

bound[1].SetLowerBound(0);

bound[1].SetCount(3); // three cols

 

CComSafeArray<VARIANT> parmArr(bound,2);

 

long ndx[2];

long lData[2][3] = { {10, 11, 12}, {20, 21, 22} };

for ( int i = 0; i < 2; i++ ) // row

{

for ( int j = 0; j < 3; j++ ) // col

{

ndx[0] = i; ndx[1] = j;

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;

}