Questions on Usage of a .NET Component from VB6 and VBScript

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Igor Ladnik
    New Member
    • Oct 2008
    • 1

    Questions on Usage of a .NET Component from VB6 and VBScript

    We are dealing with .NET server component and COM clients written with VB6 and VBScript. And there are some problems we faced (please see code below).

    1. Managed type System Drawing.Color is automatically marshaled to VB type OLE_COLOR only in case of early binding. (see DealWithColor() method).

    2. For class Component public variable DateTime dt can be used in both early and late binding cases, whereas public variable Color clr causes failure in late binding and works fine in early binding.

    3. Method public void Info(MyPointCla ss pt) works fine with VB6 for both early and late binding but fails in VBScript.

    [code=cpp]
    [ComVisible(true )]
    public struct MYPOINT
    {
    // These are now private!
    private int xPos;
    private int yPos;

    [MarshalAs(Unman agedType.BStr)]
    private string name;

    // Add some members to the MYPOINT struct.
    public void SetPoint(int x, int y) { xPos = x; yPos = y; }
    public void DisplayPoint() { MessageBox.Show (string.Format( "X: {0} Y: {1}", xPos, yPos), name); }
    }

    [ComVisible(true )]
    [ClassInterface( ClassInterfaceT ype.AutoDual)]
    public class MyPointClass
    {
    private int xPos;
    private int yPos;
    private string name;

    public void SetPoint(string name, int x, int y) { this.name = name; xPos = x; yPos = y; }
    public void DisplayPoint() { MessageBox.Show (string.Format( "X: {0} Y: {1}", xPos, yPos), name); }
    }

    [ComVisible(true )]
    [ClassInterface( ClassInterfaceT ype.AutoDual)]
    public class Component
    {
    // Success in Early Binding. Failure in Late Binding (Both VB6 and VBScript)
    public Color clr;

    // Success in Early Binding and Late Binding (Both VB6 and VBScript)
    public DateTime dt;

    // Success in Early Binding. Failure in Late Binding (Both VB6 and VBScript)
    public Color DealWithColor(C olor col)
    {
    MessageBox.Show (col.ToString() , "DealWithColor( ) Method");
    return col;
    }

    // Success in Early Binding. TODO Late
    public void Info(ref MYPOINT pt)
    {
    pt.DisplayPoint ();
    }

    // Success in Early Binding and Late Binding (Both VB6 and VBScript)
    public void Info (object pt)
    {
    (pt as MyPointClass).D isplayPoint();
    }

    // Success in Early Binding and in Late Binding VB6. Failure in VBScript.
    public void Info (MyPointClass pt)
    {
    pt.DisplayPoint ();
    }
    }

    [/code]

    [code=vbnet]
    'Late Binding via VB6
    ''''''''''''''' ''''''''''''''' '''''
    Dim ptc As CallNetFromVB6. MyPointClass
    Dim col1 As OLE_COLOR
    Dim objLate As Object
    Set objLate = CreateObject("C allNetFromVB6.C omponent")

    objLate.dt = Now 'Success

    Set ptc = New CallNetFromVB6. MyPointClass
    ptc.SetPoint "From Class", 11, 22
    objLate.Info_2 ptc 'Success

    objLate.clr = vbRed 'Fail
    col1 = objLate.DealWit hColor(col) 'Fail


    'Late Binding via VBScript
    ''''''''''''''' ''''''''''''''' ''''
    Set objLate = CreateObject("C allNetFromVB6.C omponent")
    Set ptLate = CreateObject("C allNetFromVB6.M yPointClass")

    objLate.dt = Now 'Success

    Set ptc = New CallNetFromVB6. MyPointClass
    ptc.SetPoint "From Class", 11, 22
    objLate.Info_2 ptc 'Fail

    objLate.clr = vbRed 'Fail
    col1 = objLate.DealWit hColor(col) 'Fail

    [/code]

    Thanks.

    Igor Ladnik
    Barak Cohen
    Last edited by Frinavale; Oct 15 '08, 08:16 PM. Reason: added code tags
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Now that you've determined how .NET's Interop is not properly Marshaling your VB6 and VBScript components, consider Marshaling your VB6 and VBScript components yourself:

    Write a wrapper class around the VB and VBScript components that will handle the Marshaling instead of leaving this up to .NET to do.

    -Frinny

    Comment

    Working...