Charlie
Charlie

Reputation: 794

VB6 and COM: cannot use user-defined type parameter in COM function

I am using a COM object in VB6. The COM object has a function Foo(Long, Long, Rect). Rect is a struct defined in the COM object implementation. My VB6 code (a button on a form) is like below:

Private Sub btnTestCom_Click()
    Set ComObj = CreateObject("ObjectName")
    Dim rect As DISPLAY_RECT

    rect.Left = 20
    rect.Top = 20

    ComObj.Foo(101, 0, rect) ' Error here
End Sub

At the last line it is giving me this compilation error: "Only user-defined types defined in public object modules can be coerced to or from a variant or passed to late-bound functions".

Other COM functions that do not have user-defined type parameters are working fine.

How do I solve this problem?

Thanks.

Upvotes: 0

Views: 1038

Answers (2)

MarkJ
MarkJ

Reputation: 30398

The function call is late-bound because your variable ComObj is not typed. You could try declaring it, something like

Dim ComObj As SomeObjectDefinedInComImplementation 

EDIT

I would also check that you are actually using the DISPLAY_RECT from the COM library. Open up the object browser (press F2) and search all libraries for DISPLAY_RECT.

  • If you only see one result, check whether it's from the correct COM library.
    • If it's from the correct COM library, there must be another problem.
    • If it's from the wrong COM library: check whether you actually have the correct library referenced (in project references). If you definitely do already have it referenced, double-check whether you really are supposed to pass a DISPLAY_RECT
  • If you see multiple results, VB6 may be picking up the wrong library. Try explicitly qualifying the DISPLAY_RECT with the name of the library Dim rect As TheCorrectLibraryName.DISPLAY_RECT

Upvotes: 1

RS Conley
RS Conley

Reputation: 7196

Assuming DISPLY_RECT is a type, you can't pass types into a public COM method or return a type from a public COM Function in VB6. You will have to make a class duplicating the type, and a helper function that takes the class as a parameter and returns the type.

Upvotes: 0

Related Questions