首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > VB >

VB向VC++的COM中传递数组的有关问题

2012-03-14 
VB向VC++的COM中传递数组的问题//VB向COM传递数组//这段代码的意思是,传进来的数组元素是BSTR类型的,得到

VB向VC++的COM中传递数组的问题
//VB向COM传递数组
//这段代码的意思是,传进来的数组元素是BSTR类型的,得到每个元素,组成一个新的BSTR串,在返回给SetArr方法

C/C++ code
STDMETHODIMP CArr::SetArr(VARIANT *pIn, VARIANT *pRet){    AFX_MANAGE_STATE(AfxGetStaticModuleState())    SAFEARRAY *psa=*(pIn->pparray);//获取从外部传递进来的数组,要用pparray,区别在于此    CComBSTR tmp;    long lBound,uBound;    SafeArrayGetLBound(psa,1,&lBound);    SafeArrayGetUBound(psa,1,&uBound);    for(long i=lBound;i<=uBound;i++)    {        VARIANT var;        SafeArrayGetElement(psa,&i,&var);        if(tmp.Length()==0)tmp.AppendBSTR(var.bstrVal);        else        {            tmp.AppendBSTR(SysAllocString(L","));            tmp.AppendBSTR(var.bstrVal);        }    }    pRet->vt=VT_BSTR;    pRet->bstrVal=tmp.Copy();    return S_OK;}


在ASP中调用没有问题:

<%
'向COM中传递数组
dim newArr(6)
newArr(0)="1111111"
newArr(1)="2222222"
newArr(2)="3333333"
newArr(3)="4444444"
newArr(4)="5555555"
newArr(5)="6666666"
newArr(6)="7777777"

str=obj.setArr(newArr)
response.write str
%>

可在VB中死活不行:
VB code
Private Sub Command1_Click()Set obj = CreateObject("TestVar.Arr")Dim arr(0 To 3) As Stringarr(0) = CStr("aaa")arr(1) = CStr("bbb")arr(2) = CStr("ccc")Dim var As Variant'var = obj.SetArr(arr)'var = obj.SetArr(VarPtr(arr(0)))var = obj.SetArr(arr(0))MsgBox CStr(var)End Sub

是我哪里弄错了呢?
非常感谢


[解决办法]
Dim arr(0 To 3) As String
试试改为
Dim arr(0 To 3) As Variant
[解决办法]
试试这样:

Private Sub Command1_Click()

Set obj = CreateObject("TestVar.Arr")

Dim arr As Variant
arr =array(CStr("aaa"),CStr("bbb"),CStr("ccc"))

var = obj.SetArr(arr)

'MsgBox CStr(var)
End Sub



[解决办法]
试试这个:
VB code
Private Sub Command1_Click()    Dim obj    Set obj = CreateObject("TestCom.Test")        Dim arr(0 To 3) As Variant    arr(0) = "aaa"    arr(1) = "bbb"    arr(2) = "ccc"        Dim var As Variant    var = obj.SetArr(arr)    'var = obj.SetArr(VarPtr(arr(0)))    'var = obj.SetArr(arr(0))        MsgBox CStr(var)End Sub
[解决办法]
VB code
Dim arr() As StringRedim arr(0 to 2)arr(0) = CStr("aaa")arr(1) = CStr("bbb")arr(2) = CStr("ccc")Dim var As Variantvar = obj.SetArr(arr) 

热点排行