2
votes

I'm having trouble with a .NET Assembly that is com visible, and calling certain methods from VB6. What I have found is that if the parameters are well defined types, (e.g. string), calls work fine. If they are higher level objects, it raises a runtime error '438' suggesting that the property or method is not present. I suspect that this is a question of having the correct signature on the call, but I can't see how to do this correctly.

I believe that I've done everything correct on the .NET side (ComVisible, public interfaces, etc. and even have it down to a simple enough case).

Looking at the output from the typelib viewer, I have the following:

dispinterface ISimple {
properties:
methods:
    [id(0x60020000)]
    void Add([in] ISimpleMember* member);
    [id(0x60020001)]
    ISimpleMember* Create();
};

OK. So I have 2 methods in my ISimple interface. One takes an ISimpleMember (Add), whilst the other, returns an ISimpleMember.

The corresponding code in VB looks like this:

Dim item As ISimpleMember
Dim simple As simple
Set item = New SimpleMember
item.S1 = "Hello"
item.S2 = "World"
Set simple = New simple
simple.Add (item)           <---- This raised the run time error 438
Set item = simple.Create    <---- This works fine, returning me an ISimpleMember

I've tried a couple of things: 1. Dim item as SimpleMember (makes no difference) 2. simple.Add(ObjPtr(item)) - Syntax error 3. simple.Add(ByRef item) - Syntax error

Basically, The run time error is the same as if I had simple.AMethodThatIHaventWritten()

Also, If I browse References in the VB6 Environment, The Add method is well defined:

Sub Add(member As SimpleMember)

2

2 Answers

2
votes

I've found the answer I believe. It was very simple: When calling a SubRoutine, I shouldn't put the name in braces. the call should have been:

simple.add member

rather than

simple.add(member)

If I change it to a function (i.e. return a value rather than void) the braces are necessary

This seems to work

0
votes

(Probably) The top 3 VB6 coding mistakes made by devs who now mainly code in C#, Javascript etc. Are:-

  1. Placing ; at the end of lines. Its a syntax error very easily spotted and picked up the compiler.
  2. Not placing Then on the other side of an If condition expression. Again its a syntax error.
  3. Calling a method without retrieving a value and yet using ( ) to enclose the parameter list. With multiple parameters this is a syntax error and easily found. With only one parameter the use of ( ) is interpreted as an expression. Its the result of the ( ) expression which is passed as parameter. This causes problems when ByRef is expected by the callee.