Here is my C# server method:
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);
}
My C++ client is setting up and calling this method. This works fine, but the status and output variables don't appear to be chaining. It's as though they're being passed by value instead of by reference.
Here's my client code:
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 on setting up the SAFEARRAY arguments so that the 'ref' params are copied back out?