Home All Groups Group Topic Archive Search About

VB Net/Com Interoperability/Late Binding

Author
26 Jun 2009 3:26 PM
Mike
Hi All,

We are in the process of upgrading some VB6 code to .Net  Specifically, we are re-writing some class definitions in the .Net environment to still be used in VB6.  I think we have most of the stumbling blocks figured out, however, there is one design requirement due to poor programming that we need to implement.  This requirement is to be able to have access to all child properties from its parent.  Let's do a simple example that may illustrate this.

VB.Net Code

Public Class myBaseClass

    Public myBaseClassProp As String

End Class

Public Class myDerivedClass
    Inherits myBaseClass

    Public myDerivedClassProp As String

End Class

From within VB6 I need to be able to access myBaseClss.myDerivedClassProp through code like

Dim md As New myDerivedClass
Dim mb as myBaseClass

Set mb=md

mb.myDerivedClassProp="test"

I did find the following blurb from the below website to slightly lead me in the right direction.  However, it didn't give any examples or indications of actually how to go about doing this.  I know if I can somehow override the Interfaces below, we can write our own implementation and use Reflections to get the property that we are looking for.  I would also like to be able to do a mixture of early and late binding(which I don't know if it is possible) where all the regular properties are made available through intellisense and so that the application performs best.

The website/blurb is as follows:

http://msdn.microsoft.com/en-us/library/111chfb8(VS.80).aspx

In addition to exposing the interfaces that are explicitly implemented by a class in the managed environment, the .NET Framework supplies implementations of the COM interfaces listed in the following table on behalf of the object. A .NET class can override the default behavior by providing its own implementation of these interfaces. However, the runtime always provides the implementation for the IUnknown and IDispatch interfaces.

      Interface  Description 
      Idispatch
     Provides a mechanism for late binding to type.

      IerrorInfo
     Provides a textual description of the error, its source, a Help file, Help context, and the GUID of the interface that defined the error (always GUID_NULL for .NET classes).

      IprovideClassInfo
     Enables COM clients to gain access to the ITypeInfo interface implemented by a managed class.

      IsupportErrorInfo
     Enables a COM client to determine whether the managed object supports the IErrorInfo interface. If so, enables the client to obtain a pointer to the latest exception object. All managed types support the IErrorInfo interface.

      ItypeInfo
     Provides type information for a class that is exactly the same as the type information produced by Tlbexp.exe.

      Iunknown
     Provides the standard implementation of the IUnknown interface with which the COM client manages the lifetime of the CCW and provides type coercion.





--
Thanks,

Mike

Author
26 Jun 2009 9:34 PM
Family Tree Mike
"Mike" <mwiles***@email.phoenix.edu> wrote in message news:%237KdKKn9JHA.3964@TK2MSFTNGP04.phx.gbl...
  Hi All,

  We are in the process of upgrading some VB6 code to .Net  Specifically, we are re-writing some class definitions in the .Net environment to still be used in VB6.  I think we have most of the stumbling blocks figured out, however, there is one design requirement due to poor programming that we need to implement.  This requirement is to be able to have access to all child properties from its parent.  Let's do a simple example that may illustrate this.

  VB.Net Code

  Public Class myBaseClass

      Public myBaseClassProp As String

  End Class

  Public Class myDerivedClass
      Inherits myBaseClass

      Public myDerivedClassProp As String

  End Class

  From within VB6 I need to be able to access myBaseClss.myDerivedClassProp through code like

  Dim md As New myDerivedClass
  Dim mb as myBaseClass

  Set mb=md

  mb.myDerivedClassProp="test"

  I did find the following blurb from the below website to slightly lead me in the right direction.  However, it didn't give any examples or indications of actually how to go about doing this.  I know if I can somehow override the Interfaces below, we can write our own implementation and use Reflections to get the property that we are looking for.  I would also like to be able to do a mixture of early and late binding(which I don't know if it is possible) where all the regular properties are made available through intellisense and so that the application performs best.

  The website/blurb is as follows:

  http://msdn.microsoft.com/en-us/library/111chfb8(VS.80).aspx

  In addition to exposing the interfaces that are explicitly implemented by a class in the managed environment, the .NET Framework supplies implementations of the COM interfaces listed in the following table on behalf of the object. A .NET class can override the default behavior by providing its own implementation of these interfaces. However, the runtime always provides the implementation for the IUnknown and IDispatch interfaces.

        Interface  Description 
        Idispatch
       Provides a mechanism for late binding to type.

        IerrorInfo
       Provides a textual description of the error, its source, a Help file, Help context, and the GUID of the interface that defined the error (always GUID_NULL for .NET classes).

        IprovideClassInfo
       Enables COM clients to gain access to the ITypeInfo interface implemented by a managed class.

        IsupportErrorInfo
       Enables a COM client to determine whether the managed object supports the IErrorInfo interface. If so, enables the client to obtain a pointer to the latest exception object. All managed types support the IErrorInfo interface.

        ItypeInfo
       Provides type information for a class that is exactly the same as the type information produced by Tlbexp.exe.

        Iunknown
       Provides the standard implementation of the IUnknown interface with which the COM client manages the lifetime of the CCW and provides type coercion.





  --
  Thanks,

  Mike


In general, this would not make sense, because of the following example:

public class vehical
end class

public class car
  inherits vehical
  public string MufflerType
end class

public class airplane
  inherits vehical
  public string WingType
end class

Given your scenario, you would be trying (potentially) to get the wingtype of a car, or the mufflertype of an airplane.

It's been years since I looked at VB 6 code, but I cannot think you could to that in VB 6 either.

--
Mike
Author
27 Jun 2009 12:08 AM
Mike
Thanks for the reply Mike..

Sadly, I know it is somehow possible.  The third party program that we are replacing allowed for it.  I do not know what language the program was written in(quite possibly c or c++).  However, to minimize the amount of code changes it would be nice to be able to duplicate this functionality.  I'm fairly certain that the third party program we were utilizing did this somehow through late binding as none of the properties like that show up through VB6's intelli sense.

The backup plan that I have proposed to the group is to create a Properties method on each class that utilizes reflection to find that function and then either sets or gets the correct value.

--
Thanks,

Mike Wiles
  "Family Tree Mike" <FamilyTreeM***@ThisOldHouse.com> wrote in message news:F93E3C8C-ADCF-4A4D-9D75-2C14EA5EDA8F@microsoft.com...
    "Mike" <mwiles***@email.phoenix.edu> wrote in message news:%237KdKKn9JHA.3964@TK2MSFTNGP04.phx.gbl...
    Hi All,

    We are in the process of upgrading some VB6 code to .Net  Specifically, we are re-writing some class definitions in the .Net environment to still be used in VB6.  I think we have most of the stumbling blocks figured out, however, there is one design requirement due to poor programming that we need to implement.  This requirement is to be able to have access to all child properties from its parent.  Let's do a simple example that may illustrate this.

    VB.Net Code

    Public Class myBaseClass

        Public myBaseClassProp As String

    End Class

    Public Class myDerivedClass
        Inherits myBaseClass

        Public myDerivedClassProp As String

    End Class

    From within VB6 I need to be able to access myBaseClss.myDerivedClassProp through code like

    Dim md As New myDerivedClass
    Dim mb as myBaseClass

    Set mb=md

    mb.myDerivedClassProp="test"

    I did find the following blurb from the below website to slightly lead me in the right direction.  However, it didn't give any examples or indications of actually how to go about doing this.  I know if I can somehow override the Interfaces below, we can write our own implementation and use Reflections to get the property that we are looking for.  I would also like to be able to do a mixture of early and late binding(which I don't know if it is possible) where all the regular properties are made available through intellisense and so that the application performs best.

    The website/blurb is as follows:

    http://msdn.microsoft.com/en-us/library/111chfb8(VS.80).aspx

    In addition to exposing the interfaces that are explicitly implemented by a class in the managed environment, the .NET Framework supplies implementations of the COM interfaces listed in the following table on behalf of the object. A .NET class can override the default behavior by providing its own implementation of these interfaces. However, the runtime always provides the implementation for the IUnknown and IDispatch interfaces.

          Interface  Description 
          Idispatch
         Provides a mechanism for late binding to type.

          IerrorInfo
         Provides a textual description of the error, its source, a Help file, Help context, and the GUID of the interface that defined the error (always GUID_NULL for .NET classes).

          IprovideClassInfo
         Enables COM clients to gain access to the ITypeInfo interface implemented by a managed class.

          IsupportErrorInfo
         Enables a COM client to determine whether the managed object supports the IErrorInfo interface. If so, enables the client to obtain a pointer to the latest exception object. All managed types support the IErrorInfo interface.

          ItypeInfo
         Provides type information for a class that is exactly the same as the type information produced by Tlbexp.exe.

          Iunknown
         Provides the standard implementation of the IUnknown interface with which the COM client manages the lifetime of the CCW and provides type coercion.





    --
    Thanks,

    Mike


  In general, this would not make sense, because of the following example:

  public class vehical
  end class

  public class car
    inherits vehical
    public string MufflerType
  end class

  public class airplane
    inherits vehical
    public string WingType
  end class

  Given your scenario, you would be trying (potentially) to get the wingtype of a car, or the mufflertype of an airplane.

  It's been years since I looked at VB 6 code, but I cannot think you could to that in VB 6 either.

  --
  Mike
Author
29 Jun 2009 11:22 AM
Patrice
Hello,

The proper way to do that from a mb variable would be precisely to do something such as :
Set md=mb ' Will fails if mb doesn't hold a reference to MyDerivedClass
md.myDerivedClassProp="Test"

That is exactly what you are trying to escape from...

IMO the easiest way to do this using VB6 would be likely to use late binding at least for this particular object/call :

Dim md As New myDerivedClass
Dim mb as Object
Set mb=md
mb.myDerivedClassProp="test"


I understand you are trying to minimize code changes (but you the existing source code is in some other language  than VB 6 ??? and I don't see how it could have worked in VB6 until now) but I would still double check if it's not better to design correctly now rather than to reproduce an awkward design that will keep bothering you forever...

--
Patrice

  "Mike" <mwiles***@email.phoenix.edu> a écrit dans le message de groupe de discussion : %237KdKKn9JHA.3***@TK2MSFTNGP04.phx.gbl...
  Hi All,

  We are in the process of upgrading some VB6 code to .Net  Specifically, we are re-writing some class definitions in the .Net environment to still be used in VB6.  I think we have most of the stumbling blocks figured out, however, there is one design requirement due to poor programming that we need to implement.  This requirement is to be able to have access to all child properties from its parent.  Let's do a simple example that may illustrate this.

  VB.Net Code

  Public Class myBaseClass

      Public myBaseClassProp As String

  End Class

  Public Class myDerivedClass
      Inherits myBaseClass

      Public myDerivedClassProp As String

  End Class

  From within VB6 I need to be able to access myBaseClss.myDerivedClassProp through code like

  Dim md As New myDerivedClass
  Dim mb as myBaseClass

  Set mb=md

  mb.myDerivedClassProp="test"

  I did find the following blurb from the below website to slightly lead me in the right direction.  However, it didn't give any examples or indications of actually how to go about doing this.  I know if I can somehow override the Interfaces below, we can write our own implementation and use Reflections to get the property that we are looking for.  I would also like to be able to do a mixture of early and late binding(which I don't know if it is possible) where all the regular properties are made available through intellisense and so that the application performs best.

  The website/blurb is as follows:

  http://msdn.microsoft.com/en-us/library/111chfb8(VS.80).aspx

  In addition to exposing the interfaces that are explicitly implemented by a class in the managed environment, the .NET Framework supplies implementations of the COM interfaces listed in the following table on behalf of the object. A .NET class can override the default behavior by providing its own implementation of these interfaces. However, the runtime always provides the implementation for the IUnknown and IDispatch interfaces.

        Interface  Description 
        Idispatch
       Provides a mechanism for late binding to type.

        IerrorInfo
       Provides a textual description of the error, its source, a Help file, Help context, and the GUID of the interface that defined the error (always GUID_NULL for .NET classes).

        IprovideClassInfo
       Enables COM clients to gain access to the ITypeInfo interface implemented by a managed class.

        IsupportErrorInfo
       Enables a COM client to determine whether the managed object supports the IErrorInfo interface. If so, enables the client to obtain a pointer to the latest exception object. All managed types support the IErrorInfo interface.

        ItypeInfo
       Provides type information for a class that is exactly the same as the type information produced by Tlbexp.exe.

        Iunknown
       Provides the standard implementation of the IUnknown interface with which the COM client manages the lifetime of the CCW and provides type coercion.





  --
  Thanks,

  Mike
Author
30 Jun 2009 11:06 AM
Mike
Thanks for the info.  I'll have to see if that will work.  Do you know what .Net COM attributes I need to set above my class in order to make that work?  Does that approach mix early and late binding?  Late binding is an expensive operation, so it would be nice to early bind the known properties and then late bind the unknown properties.  This should allow our intellisense to work for the early bound properties as well.

Reworking the code is definitely a possibility that we are considering.  I do have a solution using Reflections that I can simulate the same thing using a function call.  We just have a very short window of time to write the code.  I basically wrote two .Net properties as a work around, an obj.Properties("myDerivedProp") and and obj.setProperty("myDerivedProp", value).  The obj.Properties("myDerivedProp") works for returning values and setting non number or string values, but works for class objects.  This is due to the way things get exported to COM from .Net. obj.setProperty("myDerivedProp", value) works with all types of data. 

Our application is in VB6.  However, our object manager is written in by a 3rd party that we don't have access to.  If I had to take a guess, I'm guessing that it was written in C++ because what I've read shows that in C++ you write your own IUnknown and IDispatch events, which I think is how I think they got away with it.

--
Thanks,

Mike Wiles
  "Patrice" <http://scribe-en.blogspot.com/> wrote in message news:OagHhvK%23JHA.1492@TK2MSFTNGP03.phx.gbl...
  Hello,

  The proper way to do that from a mb variable would be precisely to do something such as :
  Set md=mb ' Will fails if mb doesn't hold a reference to MyDerivedClass
  md.myDerivedClassProp="Test"

  That is exactly what you are trying to escape from...

  IMO the easiest way to do this using VB6 would be likely to use late binding at least for this particular object/call :

  Dim md As New myDerivedClass
  Dim mb as Object
   Set mb=md
   mb.myDerivedClassProp="test"


  I understand you are trying to minimize code changes (but you the existing source code is in some other language  than VB 6 ??? and I don't see how it could have worked in VB6 until now) but I would still double check if it's not better to design correctly now rather than to reproduce an awkward design that will keep bothering you forever...

  --
  Patrice

    "Mike" <mwiles***@email.phoenix.edu> a écrit dans le message de groupe de discussion : %237KdKKn9JHA.3***@TK2MSFTNGP04.phx.gbl...
    Hi All,

    We are in the process of upgrading some VB6 code to .Net  Specifically, we are re-writing some class definitions in the .Net environment to still be used in VB6.  I think we have most of the stumbling blocks figured out, however, there is one design requirement due to poor programming that we need to implement.  This requirement is to be able to have access to all child properties from its parent.  Let's do a simple example that may illustrate this.

    VB.Net Code

    Public Class myBaseClass

        Public myBaseClassProp As String

    End Class

    Public Class myDerivedClass
        Inherits myBaseClass

        Public myDerivedClassProp As String

    End Class

    From within VB6 I need to be able to access myBaseClss.myDerivedClassProp through code like

    Dim md As New myDerivedClass
    Dim mb as myBaseClass

    Set mb=md

    mb.myDerivedClassProp="test"

    I did find the following blurb from the below website to slightly lead me in the right direction.  However, it didn't give any examples or indications of actually how to go about doing this.  I know if I can somehow override the Interfaces below, we can write our own implementation and use Reflections to get the property that we are looking for.  I would also like to be able to do a mixture of early and late binding(which I don't know if it is possible) where all the regular properties are made available through intellisense and so that the application performs best.

    The website/blurb is as follows:

    http://msdn.microsoft.com/en-us/library/111chfb8(VS.80).aspx

    In addition to exposing the interfaces that are explicitly implemented by a class in the managed environment, the .NET Framework supplies implementations of the COM interfaces listed in the following table on behalf of the object. A .NET class can override the default behavior by providing its own implementation of these interfaces. However, the runtime always provides the implementation for the IUnknown and IDispatch interfaces.

          Interface  Description 
          Idispatch
         Provides a mechanism for late binding to type.

          IerrorInfo
         Provides a textual description of the error, its source, a Help file, Help context, and the GUID of the interface that defined the error (always GUID_NULL for .NET classes).

          IprovideClassInfo
         Enables COM clients to gain access to the ITypeInfo interface implemented by a managed class.

          IsupportErrorInfo
         Enables a COM client to determine whether the managed object supports the IErrorInfo interface. If so, enables the client to obtain a pointer to the latest exception object. All managed types support the IErrorInfo interface.

          ItypeInfo
         Provides type information for a class that is exactly the same as the type information produced by Tlbexp.exe.

          Iunknown
         Provides the standard implementation of the IUnknown interface with which the COM client manages the lifetime of the CCW and provides type coercion.





    --
    Thanks,

    Mike
Author
30 Jun 2009 1:24 PM
Patrice
AFAIK none. When your VB6 code uses a strongly typed variable it will use just use early binding, when you'll call a method through an object variable (that points to the same class) it will use late binding.
Basically the idea is to have two variables pointing to the same object using one or the other depending wether or not you want to use early or late binding. Not tried but IMO it should work.

Also it makes me think that VB6 had also CallByName. Then you could perhaps use the same strongly typed variable and use CallByName when you need to go through IDispatch. Try :
http://www.devx.com/tips/Tip/15422

Not sure but doing a quick test should be quite simple.

--
Patrice

  "Mike" <mwiles***@email.phoenix.edu> a écrit dans le message de groupe de discussion : euj6GLX%23JHA.1***@TK2MSFTNGP04.phx.gbl...
  Thanks for the info.  I'll have to see if that will work.  Do you know what .Net COM attributes I need to set above my class in order to make that work?  Does that approach mix early and late binding?  Late binding is an expensive operation, so it would be nice to early bind the known properties and then late bind the unknown properties.  This should allow our intellisense to work for the early bound properties as well.

  Reworking the code is definitely a possibility that we are considering.  I do have a solution using Reflections that I can simulate the same thing using a function call.  We just have a very short window of time to write the code.  I basically wrote two .Net properties as a work around, an obj.Properties("myDerivedProp") and and obj.setProperty("myDerivedProp", value).  The obj.Properties("myDerivedProp") works for returning values and setting non number or string values, but works for class objects.  This is due to the way things get exported to COM from .Net. obj.setProperty("myDerivedProp", value) works with all types of data. 

  Our application is in VB6.  However, our object manager is written in by a 3rd party that we don't have access to.  If I had to take a guess, I'm guessing that it was written in C++ because what I've read shows that in C++ you write your own IUnknown and IDispatch events, which I think is how I think they got away with it.

  --
  Thanks,

  Mike Wiles
    "Patrice" <http://scribe-en.blogspot.com/> wrote in message news:OagHhvK%23JHA.1492@TK2MSFTNGP03.phx.gbl...
    Hello,

    The proper way to do that from a mb variable would be precisely to do something such as :
    Set md=mb ' Will fails if mb doesn't hold a reference to MyDerivedClass
    md.myDerivedClassProp="Test"

    That is exactly what you are trying to escape from...

    IMO the easiest way to do this using VB6 would be likely to use late binding at least for this particular object/call :

    Dim md As New myDerivedClass
    Dim mb as Object
     Set mb=md
     mb.myDerivedClassProp="test"


    I understand you are trying to minimize code changes (but you the existing source code is in some other language  than VB 6 ??? and I don't see how it could have worked in VB6 until now) but I would still double check if it's not better to design correctly now rather than to reproduce an awkward design that will keep bothering you forever...

    --
    Patrice

      "Mike" <mwiles***@email.phoenix.edu> a écrit dans le message de groupe de discussion : %237KdKKn9JHA.3***@TK2MSFTNGP04.phx.gbl...
      Hi All,

      We are in the process of upgrading some VB6 code to .Net  Specifically, we are re-writing some class definitions in the .Net environment to still be used in VB6.  I think we have most of the stumbling blocks figured out, however, there is one design requirement due to poor programming that we need to implement.  This requirement is to be able to have access to all child properties from its parent.  Let's do a simple example that may illustrate this.

      VB.Net Code

      Public Class myBaseClass

          Public myBaseClassProp As String

      End Class

      Public Class myDerivedClass
          Inherits myBaseClass

          Public myDerivedClassProp As String

      End Class

      From within VB6 I need to be able to access myBaseClss.myDerivedClassProp through code like

      Dim md As New myDerivedClass
      Dim mb as myBaseClass

      Set mb=md

      mb.myDerivedClassProp="test"

      I did find the following blurb from the below website to slightly lead me in the right direction.  However, it didn't give any examples or indications of actually how to go about doing this.  I know if I can somehow override the Interfaces below, we can write our own implementation and use Reflections to get the property that we are looking for.  I would also like to be able to do a mixture of early and late binding(which I don't know if it is possible) where all the regular properties are made available through intellisense and so that the application performs best.

      The website/blurb is as follows:

      http://msdn.microsoft.com/en-us/library/111chfb8(VS.80).aspx

      In addition to exposing the interfaces that are explicitly implemented by a class in the managed environment, the .NET Framework supplies implementations of the COM interfaces listed in the following table on behalf of the object. A .NET class can override the default behavior by providing its own implementation of these interfaces. However, the runtime always provides the implementation for the IUnknown and IDispatch interfaces.

            Interface  Description 
            Idispatch
           Provides a mechanism for late binding to type.

            IerrorInfo
           Provides a textual description of the error, its source, a Help file, Help context, and the GUID of the interface that defined the error (always GUID_NULL for .NET classes).

            IprovideClassInfo
           Enables COM clients to gain access to the ITypeInfo interface implemented by a managed class.

            IsupportErrorInfo
           Enables a COM client to determine whether the managed object supports the IErrorInfo interface. If so, enables the client to obtain a pointer to the latest exception object. All managed types support the IErrorInfo interface.

            ItypeInfo
           Provides type information for a class that is exactly the same as the type information produced by Tlbexp.exe.

            Iunknown
           Provides the standard implementation of the IUnknown interface with which the COM client manages the lifetime of the CCW and provides type coercion.





      --
      Thanks,

      Mike