CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    May 1999
    Posts
    9

    How to deal with binary data?


    I need to build a control which has a function that returns a binary data array. The function will be called in VB. What data type should I use in VC++?

    Note, the binary data array size is dynamic. If I dynamically allocates memory, where should I delete the memory?

    Any smaple codes are greatly appreciated.

    Thanks.



  2. #2
    Join Date
    May 1999
    Location
    Indiana
    Posts
    21

    Re: How to deal with binary data?

    I use a variant safearray when I need to pass an array of bytes.


    // initialize variant
    VARIANT vData;
    VariantInit(&vData);

    // create safearray
    SAFEARRAY* psa = NULL;
    SAFEARRAYBOUND rgsabound;
    rgsabound.lLbound = 0;
    rgsabound.cElements = lNumBytes;
    psa = SafeArrayCreate(VT_UI1, 1, &rgsabound);

    // access safearray
    BYTE HUGEP* pArrayData;
    SafeArrayAccessData(psa, (void HUGEP**) &pArrayData);
    // fill array
    memcpy(pArrayData, pData, lNumBytes);
    // release
    SafeArrayUnaccessData(psa);

    // return
    vData.vt = VT_UI1 | VT_ARRAY;
    vData.parray = psa;




    The caller (VB) is responsible to release memory allocated.


  3. #3
    Join Date
    May 1999
    Posts
    9

    Re: How to deal with binary data?

    Thank you.
    I still have a question.
    Should the function prototype be:
    STDMETHOD(Flush)(/*[out, retval]*/ VARIANT* buf);
    or
    STDMETHOD(Flush)(/*[out, retval]*/ SAFEARRAY* buf); ?

    Can you recommend a good book on this?

    Thanks.



  4. #4
    Join Date
    May 1999
    Location
    Indiana
    Posts
    21

    Re: How to deal with binary data?

    Use the VARIANT* parameter type.


    I have found the following books very helpful.

    Beginning ATL COM Programming by Dr. Richard Grimes and others from Wrox Press

    Professional ATL COM Programming by Dr. Richard Grimes from Wrox Press

    Essential COM by Don Box from The Addison-Wesley Object Technology Series


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured