0

DISCLAIMER: Before posting this question I read the following ones and their answers:

  • 2791996 - C# COM objects with VB6/asp error
  • 4793844 - Passing strongly typed arguments in .NET COM interop

I wrote a wrapper of the .NET Mail Class and another one for SmtpClient to make them usable in ASP Classic and VB6.

Below the SMTPSender COM-visible class which wraps SmtpClient and its interface (Dual to keep early binding):

<Guid("f588b9e7-7107-4f3a-83a9-5e0e8f5297fd")>
<ClassInterface(ClassInterfaceType.None)>
Public Class SMTPSender
    Implements ISMTPSender
    '
    Public Sub sendMail(ByVal mail As MailUtils.Mail) _
        Implements ISMTPSender.sendMail
        ' [...]
    End Sub

<Guid("cfe5e99b-c66a-4afb-8770-c0330994a823")>
<InterfaceType(ComInterfaceType.InterfaceIsDual)> _
Public Interface ISMTPSender
    '[...]
    Sub sendMail(ByVal mail As MailUtils.Mail)
    '[...]
End Interface

Below the Mail COM-visible object which wraps MailMessage and its interface (Dual too):

<Guid("d9252161-0731-4e6c-9964-66e4f56b97fa")>
<ClassInterface(ClassInterfaceType.None)>
Public Class Mail
    Implements IMail
    ' [...]
End Class

<Guid("05a149ff-36e4-4905-9193-2fb8d31f5bd8")>
<InterfaceType(ComInterfaceType.InterfaceIsDual)>
Public Interface IMail
    ' [...]
End Interface

I cannot use ISmtpSender.sendMail in VBScript as well as in VB6 if I use non-typed variables. Below the block in which occurs the issue:

Dim smtpsender: Set smtpsender = CreateObject("Mailing.SMTPSender")
' [...] SMTP configuration...

Dim mail As mail: Set mail = CreateObject("Mailing.Mail") ' Typed variable
Dim mail2: Set mail2 = New MailUtils.mail                 ' Variant
' [...] mail and mail2 filling...

smtpsender.sendMail mail  ' Success
smtpsender.sendMail mail2 ' Fails "Invalid procedure call or argument"

I explicitely assigned a GUID for each class and interface, and prevented Interface autogeneration for each class (ClassInterfaceType.None). I still get this error message.

However, surrounding the argument with parenthesis (based on an answer of the question 4793844) removes the error:

smtpsender.sendMail (mail2)

I don't understand why, since mail2 is passed by value (the value is a reference to the object content as well as in other languages IIRC).

The last solution isn't the cleaner way.

I cannot use typed variable since these COM-visible objects will be used in ASP Classic VBScript pages, which work only with untyped variables. What did I missed to make SMTPSender working?

2
  • You missed nothing. VBScript only knows VARIANT variables, so it has to convert everything to VARIANT internally. SendMail's argument is still a reference (even if you marked it as byval, it's a reference to an object). You have two options: pass your object as VARIANT (object in VB.NET) - ADO COM does that -, or use parens. For information, there's nothing wrong with using parens. Here is an example from Microsoft that uses arguments with parens: docs.microsoft.com/en-us/windows/desktop/imapi/burning-a-disc (ChooseImageDefaults is defined the same way as your sendMail method) Commented May 21, 2019 at 22:23
  • @Simon Mourier, thank you. I don't understand why it works with parenthesis (which should protect the value of the reference variable itself, not the pointed content). Is there some documentation about this point? Commented May 22, 2019 at 8:49

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.