none
Is it possible to marshal ref parameters in SAFEARRAY RRS feed

  • Question

  • I'm trying to load/run the CLR from c++ then invoke a method on a managed type. The method I'm trying to invoke has a signature that thats ref parameters for in/out.
     
         public void Exec(out int status, string output)
          {
             status = 3;
             Console.WriteLine("Exec({0}, ...)", status);
    
             output = string.Format("Hello from .NET {0}", DateTime.Now);
             Console.WriteLine("Exec(..., {0})", output);         
          }
       }
    
     
    method  My c++ client is setting up and calling this method ok, but the status and output variables don't appear to be chaning. It's as though they're being passed by value instead reference.
    InitCLR();
    
       LONG index = 0;
    
       LONG i1 = 12; // just some number for testing
       BSTR s1 = SysAllocString(L"Hello world");
    
       SAFEARRAY* args = NULL;
       CHECK_HRESULT( SafeArrayAllocDescriptor(1, &args) );
    
       args->cbElements = sizeof(VARIANT);
       args->rgsabound[0].lLbound = 0;
       args->rgsabound[0].cElements = 2;
    
       CHECK_HRESULT( SafeArrayAllocData(args) );   
    
       // byref not working for in/out param   
       VARIANT arg1;
       VariantInit(&arg1);
       V_I4REF(&arg1) = &i1;
       V_VT(&arg1) = VT_I4 | VT_BYREF;   
    
       // byref not working
       VARIANT arg2;
       VariantInit(&arg2);  
       V_BSTR(&arg2) = SysAllocString(L"Hello world");
       V_VT(&arg2) = VT_BSTR;   
    
       index = 0;
       CHECK_HRESULT( SafeArrayPutElement(args, &index, &arg1) );
    
       index = 1;
       CHECK_HRESULT( SafeArrayPutElement(args, &index, &arg2) );
    
       int bindingFlags = mscorlib::BindingFlags_InvokeMethod |
          mscorlib::BindingFlags_Instance |
          mscorlib::BindingFlags_Public;
    
       VARIANT retval;
       VariantInit(&retval);
    
       bstr_t methodName("Exec");
       HRESULT hRes = Type->InvokeMember_3(
          methodName,
          static_cast<mscorlib::BindingFlags>(bindingFlags),
          NULL, // binder *
          Instance,
          args,
          &retval);
    
       _tprintf(TEXT("Exec() == 0x%x\n"), hRes);
    
       _tprintf(TEXT("i1=%d\n"), i1);
    
     
    Can someone provide assistance one setting up the SAFEARRAY arguments so that the 'ref' params are copied back out?
     
     
    Friday, November 4, 2011 3:54 PM

Answers

  • I think you need to declare the parameter to be ref:The ref keyword causes an argument to be passed by reference, not by value.

    The effect of passing by reference is that any change to the parameter in the method is reflected in the underlying argument variable in the calling method. The value of a reference parameter is always the same as the value of the underlying argument variable.

    C# Signature

    Unmanaged Signature

    ref int arg

    int *arg


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Wednesday, November 16, 2011 4:46 AM
    Monday, November 7, 2011 7:33 AM

All replies

  • I think you need to declare the parameter to be ref:The ref keyword causes an argument to be passed by reference, not by value.

    The effect of passing by reference is that any change to the parameter in the method is reflected in the underlying argument variable in the calling method. The value of a reference parameter is always the same as the value of the underlying argument variable.

    C# Signature

    Unmanaged Signature

    ref int arg

    int *arg


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by Paul Zhou Wednesday, November 16, 2011 4:46 AM
    Monday, November 7, 2011 7:33 AM
  •  

    Hi,

     

    Has your issue been resolved? Would you mind letting us know the result of the suggestions?

     

    Now I will mark an answer, you can mark others that you think to be so useful to your issue.

    If you still have any questions about this issue, please feel free to let me know. We will continue to work with you on this issue.

     

    Have a nice day!


    Paul Zhou [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, November 16, 2011 4:47 AM