2 wentionchyq wentionchyq 于 2014.02.20 21:03 提问

vc调用matlab生成的com组件返回值不成功

大虾们好,谁能帮帮我。我在设置好vc和matlab环境之后,先通过简单的程序进行了测试,可以实现调用,eg:function z=add(x+y),不论输入参数是数组还是标量都能成功。但是在将复杂的matlab程序做成com组件之后,用vc调用。得不到正确的结果。下面我将贴出简单的程序和复杂的多输入程序给大虾们帮我分析。

function z=add(x+y)
‘inline code’
HRESULT hr;
hr=CoCreateInstance(CLSID_add1,NULL,CLSCTX_ALL,IID_Iadd1,(void **)&p_Test);
if(FAILED(hr))
{
AfxMessageBox("failed");
return;
}

VARIANT x,y,z;
SAFEARRAYBOUND xBound[1],yBound[1];
SAFEARRAY *xArrayPr,*yArrayPr,*zArrayPr;
double HUGEP *xdata,*ydata,*zdata;
xBound[0].cElements=3,xBound[0].lLbound=0;
xArrayPr=SafeArrayCreate(VT_R8,1,xBound);
yBound[0].cElements=3,yBound[0].lLbound=0;
yArrayPr=SafeArrayCreate(VT_R8,1,yBound);
HRESULT hr1,hr2;
hr1=SafeArrayAccessData(xArrayPr,(void HUGEP**)&xdata);
hr2=SafeArrayAccessData(yArrayPr,(void HUGEP**)&ydata);
for(int i=0;i<3;i++)
{
    xdata[i]=(double)i;ydata[i]=(double)i+10;

}
SafeArrayUnaccessData(xArrayPr);
SafeArrayUnaccessData(yArrayPr);
VariantInit(&x);
x.vt=VT_R8|VT_ARRAY;
x.parray=xArrayPr;
VariantInit(&y);
y.vt=VT_R8|VT_ARRAY;
y.parray=yArrayPr;
hr=p_Test->add(1,&z,x,y);

LONG zBoundL,zBoundU;
zArrayPr=z.parray;
SafeArrayGetLBound(zArrayPr,2,&zBoundL);
SafeArrayGetUBound(zArrayPr,2,&zBoundU);

double z0[3];
HRESULT hr3=SafeArrayAccessData(zArrayPr,(void** HUGEP)&zdata);
for(int t=0;t<3;t++)
{
    z0[t]=zdata[t];
}

m_1=z0[0];
m_2=z0[1];
m_3=z0[2];
UpdateData(false);
SafeArrayUnaccessData(zArrayPr);

SafeArrayDestroy(xArrayPr);
SafeArrayDestroy(yArrayPr);
SafeArrayDestroy(zArrayPr);

对这个com组件的调用能够成功。

下面是对我项目中的程序进行调用,我失败了,希望有人能够帮帮我。

‘inline code’
考虑到大侠们的感受,我这里只贴出部分参数的设定,

HRESULT hr;
hr=CoCreateInstance(CLSID_test01,NULL,CLSCTX_ALL,IID_Itest01,(void **)&m_pTest);
if(FAILED(hr))
{
    AfxMessageBox("failed");
    return;
}

VARIANT GST,GSTm,Heat,Heatm,Up_thR,Up_thRm,Up_elec,Up_elecm,Down_elec,Down_elecm,Precision,Fmin,Write01,R;
SAFEARRAYBOUND GSTBound[1],GSTmBound[1],HeatBound[1],HeatmBound[1],Up_thRBound[1],Up_thRmBound[1],Up_elecBound[1],Up_elecmBound[1];Down_elecBound[1],Down_elecmBound[1],PrecisionBound[1];
SAFEARRAY *GSTArrayPr,*GSTmArrayPr,*HeatArrayPr,*HeatmArrayPr,*Up_thRArrayPr,*Up_thRmArrayPr;
SAFEARRAY *Up_elecArrayPr,*Up_elecmArrayPr,*Down_elecArrayPr,*Down_elecmArrayPr,*PrecisionArrayPr;

double HUGEP GSTdata;
double gst[3]={300,300,120};
GSTBound[0].cElements=3,GSTBound[0].lLbound=0;
GSTArrayPr=SafeArrayCreate(VT_R8,1,GSTBound);
HRESULT hr1;
hr1=SafeArrayAccessData(GSTArrayPr,(void HUGEP
*)&GSTdata);
for(int i0=0;i0<3;i0++)
{
GSTdata[i0]=gst[i0];
}
SafeArrayUnaccessData(GSTArrayPr);
VariantInit(&GST);
GST.vt=VT_R8|VT_ARRAY;
GST.parray=GSTArrayPr;

VARIANT addrin,addout;
addrin.vt=VT_BSTR;
addrin.bstrVal=_com_util::ConvertStringToBSTR("'D:\MATLAB6.5\work3\pulse_rst.txt'");
addout.vt=VT_BSTR;
addout.bstrVal=_com_util::ConvertStringToBSTR("'D:\MATLAB6.5\work3'");
hr=m_pTest->elec_mini3(1,&R,GST,GSTm,Heat,Heatm,Up_elec,Up_elecm,Down_elec,Down_elecm,Up_thR,Up_thRm,Precision,Fmin,Write01,addrin,addout);
m_R=R.dblVal;
UpdateData(false);

运行结果得到的没有初始化的R.dblVal=-9.255……;调试的时候出现了User breakpoint called from code at 0X7C9120e的错误。
在下很急,希望有人看到这个后能帮帮我。

1个回答

wentionchyq
wentionchyq   2014.03.03 16:24

我自己已经搞定了,是字符串的传递出现了问题。版主可以删帖了。

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!