SAFEARRAY使用方法示例

From: http://blog.csdn.net/absurd/article/details/786822

 

SAFEARRAY使用方法示例

 

SAFEARRAY不是很好用,一些函数不太直观,一不小心就着了它的道。上次学习编写Google桌面插件时就遇到了麻烦,本来应该是vsa.vt = VT_ARRAY | VT_BSTR,结果写成了vsa.vt = VT_ARRAY,让我查了好久才找到原因。这里整理一下,作为备忘。

 

l         创建

                            long i = 0;

                            VARIANT va = {0};

                            va.vt = VT_BSTR;

                           

                            SAFEARRAYBOUND bounds[1] = {0};

                            bounds[0].cElements = 5;

                            SAFEARRAY* psa = SafeArrayCreate(VT_VARIANT, 1, bounds);

 

l         存入元素

                            for(i = 0; i < 5; i++)

                            {

                                     va.bstrVal = SysAllocString(L"test");

                                     SafeArrayPutElement(psa, &i, &va);

                            }

 

l         获取元素

                            for(i = 0; i < 5; i++)

                            {

                                     va.bstrVal = SysAllocString(L"test");

                                     SafeArrayGetElement(psa, &i, &va);

                                     SysFreeString(va.bstrVal);

                            }

 

l         销毁

                            SafeArrayDestroy(psa);

 

l         生成VARIANT变量

                            VARIANT vsa = {0};

                            vsa.vt = VT_ARRAY | VT_BSTR;

                            vsa.parray = psa;

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值