The event SomeEvent() is raised and handled in VB6 fine. Using this exact
same code and using VBScript with the CreateObject call causes an error in
the c# code.
When using VBScript, I get a 'Object reference not set to an instance of an
object.' on the line SomeEvent(). I am positive the m_nSomeProperty =
nValue; line executes.
How can I get this working from VBScript?
Here's the code:
using System;
using System.Runtime.InteropServices;
namespace ComInterop
{
public delegate void SomeEventHandler();
[ Guid("F8E81A5A-BB37-49CC-9681-7D4FE52936B3"),
InterfaceType(ComInterfaceType.InterfaceIsIDispatch) ]
public interface ICComInteropEvents
{
[DispId(1)] void SomeEvent();
}
[ Guid("97C8501B-35DE-48CA-BECC-B980D08226F4"),
InterfaceType(ComInterfaceType.InterfaceIsDual) ]
public interface ICComInterop
{
[DispId(2)] void SomeMethod( int nValue );
[DispId(3)] int SomeProperty{ get; set; }
}
[ Guid("10EBC493-D2FD-4723-9F8B-F593586ED9B7"),
ProgId("ComInterop.CComInterop"),
ClassInterface(ClassInterfaceType.None),
ComSourceInterfaces(typeof(ICComInteropEvents)) ]
public class CComInterop : ICComInterop
{
public CComInterop() : base()
{
}
private int m_nSomeProperty = 0;
public event SomeEventHandler SomeEvent;
public void SomeMethod( int nValue )
{
m_nSomeProperty = nValue;
///////////////////////////////////////////////////////////////////////////////////////////
//
// object reference not set to an object error occurs here
//
//////////////////////////////////////////////////////////////////////////////////////////
SomeEvent();
}
public int SomeProperty
{
get{ return m_nSomeProperty; }
set{ m_nSomeProperty = value; }
}
}
}
And the VBScript code:
Option Explicit
' ==========================================================================
' Implementation
'
' ==========================================================================
Dim objReceive
Dim bDone
On error Resume Next
bDone = false
WScript.Echo "CreateObject"
Set objReceive = CreateObject("ComInterop.CComInterop")
if Err.Number <> 0 then
WScript.Echo "Error: CreateObject: " & Err.Description
else
objReceive.SomeMethod(7)
if Err.Number <> 0 then
WScript.Echo "Error: Setting SomeMethod: " & Err.Description
else
WScript.Sleep 3000
end if
end if
objReceive = nothing
WScript.Echo "Program Over"
' ==========================================================================
Sub objReceive_SomeEvent()
WScript.Echo "Message: " & objReceive.SomeProperty
bDone = true
End Sub
--
Dan Sikorsky, MSCS BSCE BAB