开发者

How to marshal Null objects from .NET to COM as an object type?

In the middle of a large project that is using COM interop while migrating from VB6 to .NET, I found the need to write code that receives an object from an interop method and then pass that object into an interop form when then object is not null.

I discovered on MSDN that null objects are returned as an Empty variant. This causes a problem with VB6 code like this, where validator is the interop class and inputKey is irrelevant to the issue.

Set validationObject = validator.GetValidationList(inputKey)

The Set statement cannot be used when the variant is Empty. Here's a sample of what the function was doing.

    Dim validationList = GetValidationList(inputKey)
    If validationList IsNot Nothing AndAl开发者_JAVA百科so validationList.Count > 0 Then
        Return validationList
    Else
        Return Nothing
    End If

Is there a best practice for how to get the Null value returned to COM as VT_OBJECT variant? Is the following code a good idea? It seems to work, but is it the "right" way to do this?

    Dim validationList = GetValidationList(inputKey)
    If validationList IsNot Nothing AndAlso validationList.Count > 0 Then
        Return validationList
    Else
        Return New System.Runtime.InteropServices.UnknownWrapper(Nothing)
    End If


The list from MSDN you posted also seem to reveal that System.DBNull will match to VT_NULL.

The MSDN page of System.DBNull seems to support this further:

Additionally, COM interop uses the DBNull class to distinguish between a VT_NULL variant, which indicates a nonexistent value, and a VT_EMPTY variant, which indicates an unspecified value.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜