这是我的 C# 服务器方法:
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);
}
我的 C++ 客户端正在设置并调用此方法。这工作正常,但状态和输出变量似乎没有链接。就好像它们是按值而不是按引用传递的。
这是我的客户代码:
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);
有人可以在设置 SAFEARRAY 参数方面提供帮助,以便将“ref”参数复制回来吗?