SafeArrayUnaccessData(*pNode); pSimuRecordInfo->Release(); pRecordInfo->Release(); return S_OK; } 下面是VB中的测试函数,在里面动态分配了数组大小, Dim aSimu() As RECON_ATLLib.rcSimu Dim i, j, iSum As Integer Dim objRcTest As RECON_ATLLib.RcTest Set objRcTest = New RECON_ATLLib.RcTest Dim aNodes() As RECON_ATLLib.rcNode objRcTest.GetStruct aNodes i = UBound(aNodes) MsgBox "在VC里分配的数组大小为" + CStr(i+1) MsgBox aNodes(i).fResult i = UBound(aNodes(0).aRcSimu) For j = 0 To i MsgBox "节点1的aRcSimu(" + CStr(j) + ").iSimuNo=" + CStr(aNodes(0).aRcSimu(j).iSimuNo) Next
long 的在VC和VB中都用long 就行或者VC中用long VB中int 试试
VB的字符串是BSTR型的,所以VC写的接口函数接口参数如果给VB用,不能用char *,应该用BSTR一般来说,如果接口函数参数为 float *,int *
VB中都只需要直接Dim fParam as float
dim iParam as integer
接口函数.(fParam,iParam) 就可以了数组的调用你可以看下面代码,我也是COM初学,写了个试了下,还能正常传递:结构定义及函数代码(COM组件中的--ATL工程 )如下:
IDL:
typedef [uuid(5536B87A-57D7-45e3-8A06-9B21BE40F5D2)] struct rcSimu
{
int iSimuNo;
BSTR strSimuName;
}CRcSimu;
typedef [uuid(425091B5-1E6F-4cfb-995C-18AFA20AFA84)] struct rcNode
{
int iUniqueNo;
float fResult;
BSTR strNodeName;
SAFEARRAY(CRcSimu) aRcSimu;
}CNode;
函数:/*自定义结构的ID*/
const GUID IID_STRUCT_SIMU = { 0x5536b87a, 0x57d7, 0x45e3, { 0x8a, 0x6, 0x9b, 0x21, 0xbe, 0x40, 0xf5, 0xd2 } };
const IID IID_STRUCT_NODE = { 0x425091b5, 0x1e6f, 0x4cfb, { 0x99, 0x5c, 0x18, 0xaf, 0xa2, 0xa, 0xfa, 0x84 } };STDMETHODIMP CRcTest::GetStruct(SAFEARRAY **pNode)
{
IRecordInfo* pRecordInfo=NULL;
IRecordInfo *pSimuRecordInfo=NULL; HRESULT hR=GetRecordInfoFromGuids(LIBID_RECON_ATLLib,
1,0,GetUserDefaultLCID(),IID_STRUCT_SIMU
,&pSimuRecordInfo);
if(!SUCCEEDED(hR))
{
return FALSE;
}
hR=GetRecordInfoFromGuids(LIBID_RECON_ATLLib
,1,0,GetUserDefaultLCID(),IID_STRUCT_NODE
,&pRecordInfo);
if(!SUCCEEDED(hR))
{
return FALSE;
} /*创建大小为5个元素的自定义结构的数组--该结构内还有成员为数组*/
*pNode=SafeArrayCreateVectorEx(VT_RECORD,0,5
,(void *)pRecordInfo);
CNode *pData=0;
hR=SafeArrayAccessData(*pNode,(void **)&pData);
if(!SUCCEEDED(hR))
{
return FALSE;
}
CRcSimu *pSimu=NULL;
for(int i=0;i<5;i++)
{
pData[i].iUniqueNo=i*100*(i+1);
pData[i].fResult=2.2343+i;
/*(CNode 结构的aRcSimu为数组类型) 动态创建aRcSimu的大小*/
pData[i].aRcSimu=SafeArrayCreateVectorEx(VT_RECORD,0,3,(void *)pSimuRecordInfo);
hR=SafeArrayAccessData(pData[i].aRcSimu,(void **)&pSimu);
for(int j=0;j<3;j++)
{
pSimu[j].iSimuNo=(i+1)*10+(j+1);
}
SafeArrayUnaccessData(pData[i].aRcSimu);
}
SafeArrayUnaccessData(*pNode);
pSimuRecordInfo->Release();
pRecordInfo->Release();
return S_OK;
}
下面是VB中的测试函数,在里面动态分配了数组大小,
Dim aSimu() As RECON_ATLLib.rcSimu
Dim i, j, iSum As Integer
Dim objRcTest As RECON_ATLLib.RcTest
Set objRcTest = New RECON_ATLLib.RcTest Dim aNodes() As RECON_ATLLib.rcNode
objRcTest.GetStruct aNodes
i = UBound(aNodes)
MsgBox "在VC里分配的数组大小为" + CStr(i+1)
MsgBox aNodes(i).fResult
i = UBound(aNodes(0).aRcSimu)
For j = 0 To i
MsgBox "节点1的aRcSimu(" + CStr(j) + ").iSimuNo="
+ CStr(aNodes(0).aRcSimu(j).iSimuNo)
Next
弹出对话框的内容一次为:
“数组大小为5”
“节点1的aRcSimu(0).iSimuNo=11"
“节点1的aRcSimu(0).iSimuNo=12"
“节点1的aRcSimu(0).iSimuNo=13"
符合设想的功能。