使用多维safearray时值得注意的地方

今天中午给safearray弄得晕乎乎的,最后发现msdn的safearray概述里明确提到了下面这段东西,让俺体会到了不先看文档的后果


The base type of the array is indicated by VT_ tag | VT_ARRAY. The data referenced by an array descriptor is stored in column-major order, which is the same ordering scheme used by Visual Basic and FORTRAN, but different from C and Pascal. Column-major order is when the left-most dimension (as specified in a programming language syntax) changes first.


比如,vb里定义了如下的数组
ReDim r(1 To 2, 1 To 3) As Long
r(1, 1) = 11
r(1, 2) = 12
r(1, 3) = 13


内存的排列其实是

11
0
12
0
13
0

以c的观点来看,是这样一个矩阵
110
120
130

用SafeArrayGetElement取值时,分别用索引 {1,1}  {1,2}  {1,3} 来获得r (1,1), r(1,2), r(1,3)。
msdn对索引的描述如下:

rgIndices
Pointer to a vector of indexes for each dimension of the array. The right-most (least significant) dimension is rgIndices[0]. The left-most dimension is stored at rgIndices[ psa-> cDims – 1].


这句话太模糊了,俺最初就是给它搞昏的,现在看来其中所谓的right-most是以c数组的观点来看的。如果用safearraylock/unlock直接指针存取数值时,设上下标分别为LBound1,LBound2,UBound1,UBound2,大小为Dim1, Dim2,索引为Index1, Index2,则用
*(pData + (Index2 - LBound2) * Dim1 + (Index1 - LBound1));
来取 arr(Index1, Index2) 的值。

最后贴一下构建开头那个数组的vc代码
 SAFEARRAYBOUND saBounds[2] = { { 2, 1 }, { 3,1 } };
 SAFEARRAY *psa = SafeArrayCreate(VT_I4, 2, saBounds);
 long lIndices[2] = { 1, 1 };
 long nValue = 11;
 SafeArrayPutElement(psa, lIndices, &nValue);
 lIndices[1] = 2;
 nValue = 12;
 SafeArrayPutElement(psa, lIndices, &nValue);
 lIndices[1] = 3;
 nValue = 13;
 SafeArrayPutElement(psa, lIndices, &nValue);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
vlax-safearray-get-element 是一种 AutoLISP 函数,它用于从 Visual LISP 中的 SafeArray 对象中获取元素。 SafeArray 是一种用于在 Visual Basic、Visual C++ 和 Visual Basic for Applications (VBA) 中存储和传输数组的数据类型。在 AutoLISP 中,可以使用 vlax-make-safearray 函数来创建 SafeArray 对象。一旦创建了 SafeArray 对象,可以使用 vlax-safearray-get-element 函数来获取其中的元素。 函数的语法如下: (vlax-safearray-get-element safe-array indices) 其中,safe-arraySafeArray 对象,indices 是包含要获取元素的索引的列表。对于一维数组,indices 是一个整数,对于二维数组,indices 是一个包含两个整数的列表,以此类推。 下面是一个示例,演示如何使用 vlax-safearray-get-element 函数从 SafeArray 对象中获取元素: ``` (setq sa (vlax-make-safearray vlax-vbDouble '(0 3) '((1.0 2.0 3.0 4.0)))) ; 创建一个包含四个元素的一维数组 (setq elem (vlax-safearray-get-element sa 2)) ; 获取第三个元素,即 3.0 (setq sa2 (vlax-make-safearray vlax-vbDouble '(0 1 0 1) '(((1.0 2.0) (3.0 4.0)) ((5.0 6.0) (7.0 8.0))))) ; 创建一个包含四个元素的二维数组 (setq elem2 (vlax-safearray-get-element sa2 '(0 1))) ; 获取第一行第二列的元素,即 4.0 ``` 注意:vlax-safearray-get-element 函数只能用于具有正确类型的 SafeArray 对象。如果 SafeArray 对象的类型与要获取的元素类型不匹配,函数将返回 nil。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值