查看了很多的文档都有这样的一段程序
BYTE buf[lIsRead];
 for(long index=0;index<lIsRead;index++)         
 {           
  ::SafeArrayGetElement(varChunk.parray,&index,buf+index);   
 }
可是始终没明白lIsRead应该是long类型,但是不知道初始值为多少。其实我得目的就是为了
从一个safearray中取出一个byte数组,求教方法。

解决方案 »

  1.   


     读取SafeArray中的数据的步骤:
    (1). 用SafeArrayGetElement一个一个地读
     BYTE buf[lIsRead];
     for(long index=0;index<lIsRead;index++)         
     {           
      ::SafeArrayGetElement(varChunk.parray,&index,buf+index);   
     }
     就读到缓冲区buf里了。
    方法二:
     使用SafeArrayAccessData直接读写SafeArray的缓冲区:
    (1). 读缓冲区:
     BYTE *buf;
     SafeArrayAccessData(varChunk.parray, (void **)&buf);
     f.Write(buf,lIsRead);
     SafeArrayUnaccessData(varChunk.parray);
    (2). 写缓冲区:
     BYTE *buf;
     ::SafeArrayAccessData(psa, (void **)&buf);
     for(long index=0;index<uIsRead;index++)          
     {
      buf[index]=bVal[index];  
     }
     ::SafeArrayUnaccessData(psa); varChunk.vt = VT_ARRAY|VT_UI1;
     varChunk.parray = psa;
    看了这段程序感觉莫名其妙,所有的疑惑都来自lIsRead这个参数,整篇文章没有参数申明的地方,可是到处用到这个参数,百思不得其解啊
      

  2.   

    你想知道safeArray的数组大小!
    看看下面代码:
    long lParCount = (varChunk.parray->rgsabound->cElements + 1) * varChunk.parray->rgsabound->cElements;
    myVariant.parray->rgsabound->cElements  表示数组的维数,0表示一维。。
    myVariant.parray->rgsabound->cElements另一个表示数组的个数
      

  3.   

    写错了
    应该是
    long lParCount = (myVariant.parray->rgsabound->lLbound+ 1) * varChunk.parray->rgsabound->cElements;
    myVariant.parray->rgsabound->lLbound  表示数组的维数,0表示一维。。
    myVariant.parray->rgsabound->cElements另一个表示数组的个数
      

  4.   

    早上半天没发出去,看看下面的函数吧。
    SafeArrayGetLBound
    SafeArrayGetUBound
      

  5.   

    I believe this piece of incomplete code is copied from another article named "Visual C++ ADO数据库编程入门(下)", which is accessible at CSDN document center (http://dev.csdn.net/article/65/65125.shtm)Furthor reading
    http://www.daniweb.com/techtalkforums/showthread.php?t=8306See alsoBinary.exe Transfers Binary Data Using OLE Automation
    http://support.microsoft.com/kb/131046How To Pass Binary Data Between an ActiveX Control and VB
    http://support.microsoft.com/kb/154172/SAMPLE: SAFEARAY: Use of Safe Arrays in Automation
    http://support.microsoft.com/kb/131086How to use safe arrays in MFC Automation
    http://support.microsoft.com/kb/140202
      

  6.   

    地址:http://www.vckbase.com/bbs/prime/viewprime.asp?id=65内容如下:
    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)
      

  7.   

    lIsRead是一个预先定义了大小的变量。这些文章的目的是告诉你原理,所写的代码并没有经过严格测试。