VARIANT life cycle in DLL RRS feed

  • Question

  • Hi,

    I'm writing an extension DLL for VBA in C. I'm using VARIANTs to pass function arguments, so as those prototypes:

    __stdcall VARIANT SomeFunc(VARIANT byvalVariant) {
    VARIANT result;
    if (result.bstrVal=SysAllocString(L"0123456789")) result.vt=VT_BSTR;
    return result;

    Declare Function SomeFunc Lib "Some.dll" (ByVal byvalVariant as Variant) As Variant

    Sub Test()
    Dim localVar
    localVar = "Some String"
    Debug.Print SomeFunc(localVar), localVar
    End Sub

    Calling Test in Excel 2000 will cause fatal error, or, corrupt localVar.

    As it works between VBA function calls, I was expecting that VBA make a copy of BSTR data, put it in a new VARIANT and send this copy to DLL. So on, I thought I should release VARIANT in C function.

    So, how does VBA really passes Variant to functions ByVal?
    Does VBA passes Variant ByVal just like a C function, sending just a VARIANT structure copy?

    • Edited by Luis Simao Thursday, July 26, 2012 11:02 AM
    • Moved by Youen Zen Friday, July 27, 2012 7:15 AM To provide better support (From:Visual Basic Interop and Upgrade)
    Thursday, July 26, 2012 7:57 AM

All replies

  • Hi Luis,

    This issue is related to VBA. In order to provide better support, I will move this thread to VBA forum.

    Thanks for understanding!

    Shanks Zen
    MSDN Community Support | Feedback to us

    Friday, July 27, 2012 7:15 AM