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?