获取异常0x80020008 - “错误的变量类型。”。 [英] Getting exception 0x80020008 -"Bad variable type.".
问题描述
您好,
我正在使用C ++代码中的InvokeHelper()调用C#中的方法,如下所示。
I'm invoking a method in C# using InvokeHelper() from C++ code as below.
VARIANT Caller()
{
VARIANT result;
InvokeHelper(<some dispid>, DISPATCH_METHOD, VT_VARIANT, (void*)&result, NULL);
return result;
}
这会调用C#函数,它看起来像
This would call the C# function which looks like
public object Callee()
{
int a= 0;
int b= 0;
//....
//....someother code to assign a and b.
//.......
int[] res= new int[] { a, b};
return res;
}
返回类型是'object',因为我们想要返回泛型类型,这是我们的要求。
The return type is 'object' since we want to return a generic type and it is our requirement.
从调用者()返回的结果被分配给_variant_t变量,在此期间我得到异常0x80020008 - "错误的变量类型。"。(抛出异常的确切函数是:: VariantCopy(),在此期间调用赋值
到_variant_t变量。。
The result returned from caller() is assigned to _variant_t variable during which I'm getting exception 0x80020008 -"Bad variable type.".( the exact function which is throwing exception is ::VariantCopy() which is called during assignment to _variant_t variable ).
请告诉我可能的根本原因是什么?
Kindly let me know what could be the root cause?
我的InvokeHelper()函数调用了吗?是否正确?我需要在InvokeHelper()中使用VT_ARRAY而不是VT_VARIANT吗?
Is my InvokeHelper() function call is correct?Do I need to use VT_ARRAY instead of VT_VARIANT or both in InvokeHelper()?
推荐答案
由于您使用的是InvokeHelper,我假设您已使用MFC从COM对象类型库为C#COM服务器创建一个类,该服务器实现了Callee方法。
Since you are using InvokeHelper I assume that you have used MFC to create a class from a COM object type library for a C# COM server which implements the Callee method.
我将C#Callee方法复制到现有的C#COM服务器中并使用MFC从类型创建一个类。文库NBSP; MFC从类型库创建的函数看起来与发布代码中的调用函数完全相同。
I copied the C# Callee method into an existing C# COM server and used MFC to create a class from the type library. The function that MFC created from the type library looks exactly like the Caller function in the posted code.
VARIANT Callee()
{
VARIANT result;
InvokeHelper(0x60020006, DISPATCH_METHOD, VT_VARIANT, (void*)&result, NULL);
return result;
}
当测试MFC应用程序创建托管在C#COM服务器中的COM对象并调用Callee()函数时InvokeHelper成功,结果VARIANT包含SAFEARRAY ..
When the test MFC app created the COM object hosted in the C# COM Server and called the Callee() function InvokeHelper succeeded and the result VARIANT contained a SAFEARRAY..
然后代码将结果成功分配给_variant_t变量。
The code then assigned the result to a _variant_t variable successfully.
这篇关于获取异常0x80020008 - “错误的变量类型。”。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!