Link to home
Start Free TrialLog in
Avatar of Gus012498
Gus012498

asked on

Matlab and Automation and C++

Hi! I have got the following poerl code to execute data with Matlab.

# First create two SAFE_ARRAYs (Can only pass SAFE_ARRAY?)
my $MReal = Variant(VT_ARRAY|VT_R8, [1,2], 2);
my $MImag = Variant(VT_ARRAY|VT_R8, [1,2], 2);
# Set the values
$MReal->Put(1,0,1);
$MReal->Put(1,1,2);
$MReal->Put(2,0,3);
$MReal->Put(2,1,4);

# Put the matrix into MATLAB
$ML->PutFullMatrix('B', 'base', $MReal, $MImag);

I have tried to reproduce this code in C++:
HRESULT hr = S_OK;
VARIANTARG vargs[4];
VARIANT vret;
DISPPARAMS DispParams;
EXCEPINFO ExcepInfo;
SAFEARRAY* vArrayPr;
SAFEARRAY* vArrayPi;

CString command;
command.Format("B");

SAFEARRAYBOUND saPr[1];
SAFEARRAYBOUND saPi[1];

saPr[0].cElements = 10;
saPr[0].lLbound = 0;
vArrayPr = SafeArrayCreate(VT_R8 | VT_BYREF | VT_ARRAY, 1, saPr);

saPi[0].cElements = 0;
saPi[0].lLbound = 0;
vArrayPi = SafeArrayCreate(VT_R8 | VT_BYREF | VT_ARRAY, 0, saPi);

// initialize the variant args
VariantInit (&vargs[0]);
V_VT(&vargs[0]) = VT_BSTR;
V_BSTR(&vargs[0]) = command.AllocSysString();
 
VariantInit (&vargs[1]);
V_VT(&vargs[1]) = VT_BSTR;
V_BSTR(&vargs[1]) = L"base";

VariantInit (&vargs[2]);
V_VT(&vargs[2]) = VT_ARRAY | VT_R8 | VT_BYREF;
V_ARRAYREF(&vargs[2]) = &vArrayPr;

VariantInit (&vargs[3]);
V_VT(&vargs[3]) = VT_ARRAY | VT_R8 | VT_BYREF;
V_ARRAYREF(&vargs[3]) = &vArrayPi;

DispParams.cArgs = 4;
DispParams.rgvarg = vargs;
DispParams.cNamedArgs = 0;
DispParams.rgdispidNamedArgs = NULL;

// prepare for exceptions and initialize the return variante
memset (&ExcepInfo, sizeof (EXCEPINFO), '\0');
VariantInit (&vret);

// call Invoke
hr = m_lpDispatch->Invoke (m_DispGetFullMatrixID, IID_NULL, LOCALE_USER_DEFAULT, DISPATCH_METHOD,
&DispParams, &vret, &ExcepInfo, NULL);


But the C++ Invoke call returns: Type mismatch.

Can anybody help me?

Thanks in Advance

Gus
ASKER CERTIFIED SOLUTION
Avatar of rfuller031898
rfuller031898

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Gus012498
Gus012498

ASKER

Thank you very much for your answer. The problem to connect to Matlab from C++ is not already solved now, (I got another error message regarding the passed parameters) but Matlab does no longer respond with an Invalid_Type error.

Best regards

Gus