在COM方法中用VARIANT类型传递数组数据
2008-03-18 10:15
381 查看
在COM方法中可用VARIANT类型传递数组数据,在VC++中用SAFEARRAY处理。下面给出输入和输出数组的例子
1。输入数组到COM中
STDMETHODIMP CTestCom1::vb2vc(VARIANT buffer)
{
long dim=SafeArrayGetDim(buffer.parray);
long ubound;
long lbound;
SafeArrayGetUBound(buffer.parray,dim,&ubound);
SafeArrayGetLBound(buffer.parray,dim,&lbound);
BSTR* buf;
BSTR pd[2];
SafeArrayAccessData(buffer.parray,(void**)&buf);
for (int i=lbound;i <ubound;i++)
pd[i]=buf[i];
}
return S_OK;
}
buffer为一维数组,存放字符串,在vb中的代码为
Dim oo As ARRAYTESTLib.TestCom1
Set oo = New ARRAYTESTLib.TestCom1
Dim buf(2) As String
buf(0) = "65 "
buf(1) = "anss "
oo.vb2vc buf
2。COM返回数组数据到vb
STDMETHODIMP CTestCom1::retarray(VARIANT *buffer)
{
//返回数组
SAFEARRAY FAR* psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound=0;
rgsabound[0].cElements=2;
psa=SafeArrayCreate(VT_I4,1,rgsabound);
long idx;
long setdt;
idx=0;
setdt=12;
SafeArrayPutElement(psa,&idx,&setdt);
idx=1;
setdt=342;
SafeArrayPutElement(psa,&idx,&setdt);
V_VT(buffer) = VT_ARRAY | VT_I4;
V_ARRAY(buffer)=psa;
return S_OK;
}
vb中的代码为:
Dim oo As ARRAYTESTLib.TestCom1
Set oo = New ARRAYTESTLib.TestCom1
Dim rarr As Variant
oo.retarray rarr
MsgBox rarr(0) & rarr(1)
1。输入数组到COM中
STDMETHODIMP CTestCom1::vb2vc(VARIANT buffer)
{
long dim=SafeArrayGetDim(buffer.parray);
long ubound;
long lbound;
SafeArrayGetUBound(buffer.parray,dim,&ubound);
SafeArrayGetLBound(buffer.parray,dim,&lbound);
BSTR* buf;
BSTR pd[2];
SafeArrayAccessData(buffer.parray,(void**)&buf);
for (int i=lbound;i <ubound;i++)
pd[i]=buf[i];
}
return S_OK;
}
buffer为一维数组,存放字符串,在vb中的代码为
Dim oo As ARRAYTESTLib.TestCom1
Set oo = New ARRAYTESTLib.TestCom1
Dim buf(2) As String
buf(0) = "65 "
buf(1) = "anss "
oo.vb2vc buf
2。COM返回数组数据到vb
STDMETHODIMP CTestCom1::retarray(VARIANT *buffer)
{
//返回数组
SAFEARRAY FAR* psa;
SAFEARRAYBOUND rgsabound[1];
rgsabound[0].lLbound=0;
rgsabound[0].cElements=2;
psa=SafeArrayCreate(VT_I4,1,rgsabound);
long idx;
long setdt;
idx=0;
setdt=12;
SafeArrayPutElement(psa,&idx,&setdt);
idx=1;
setdt=342;
SafeArrayPutElement(psa,&idx,&setdt);
V_VT(buffer) = VT_ARRAY | VT_I4;
V_ARRAY(buffer)=psa;
return S_OK;
}
vb中的代码为:
Dim oo As ARRAYTESTLib.TestCom1
Set oo = New ARRAYTESTLib.TestCom1
Dim rarr As Variant
oo.retarray rarr
MsgBox rarr(0) & rarr(1)
相关文章推荐
- JNI中基本类型数组的传递方法(无需拷贝数据!!!)
- Intent 传递简单类型, 数组, Object对象,与ArrayList<T> 类型数据
- 使用ASP.NET AJAX异步调用Web Service和页面中的类方法(8):服务器端和客户端数据类型的自动转换:数组类型
- 【语言基础】c++ 基本数据类型与字节数组(string,char [] )之间的转化方法
- VUE中解决不能检测数组中基本数据类型的方法及$set的使用方法
- 我要从com向应用程序返回一个结构,结构中包括二进制数据,请问用什么方式传替比较好呢?BSTR,VARIANT还是其他什么类型比较好呢
- js中对arry数组的各种操作小结 瀑布流AJAX无刷新加载数据列表--当页面滚动到Id时再继续加载数据 web前端url传递值 js加密解密 HTML中让表单input等文本框为只读不可编辑的方法 js监听用户的键盘敲击事件,兼容各大主流浏览器 HTML特殊字符
- 数据类型int跟byte[]数组互转的方法!
- 第64+65讲 函数基本数据类型和数组方式 成员方法
- 采用扩展方法实现数组中数据类型转换(c#)
- COM的数据类型BSTR,Variant
- js 将json字符串转换为json对象的方法解析 作者: 字体:[增加 减小] 类型:转载 将json字符串转换为json对象的方法。在数据传输过程中,json是以文本,即字符串的形式传递的,而J
- Javascript小知识点(七):数据类型赋值,浅拷贝,深拷贝(数组值传递还是引用传递)
- [零基础学JAVA]Java SE基础部分-03.标识符、数据类型,数组,方法
- 17开年第一篇Activity之间传递List<T> 以及T对象类型数据的方法
- 删除数据Jquery post 传递数组方法 asp.net mvc
- 在COM/DCOM中如何将数组作为函数返回的类型传递
- 使用python向C语言的链接库传递数组、结构体、指针类型的数据
- Java/JSP中传递JSON数据类型给Ajax回调函数方法与注意的
- java数据类型、存储以及作为方法参数传递