Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COM

Status
Not open for further replies.

sunaj

Technical User
Feb 13, 2001
1,474
DK
Hi,
I need to write a c# dll which I can call from a VBScript.
It works, except that I also need to catch events from the dll.
Here is my C# code
[tt]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface IFormCloseEvents
{
void OnFormClose();
}

//declaring the event handler delegate
public delegate void SimpleEventHandler();

[ProgId("DHI.WFScriptWindow"),
ComSourceInterfaces("IFormCloseEvents"),
ClassInterface(ClassInterfaceType.AutoDual),
ComVisible(true)]
public class WFScriptWin
{
//declaring the event
public event SimpleEventHandler OnFormClose;

frmMain mFrm;
public WFScriptWin()
{
//init
mFrm = new frmMain();
mFrm.FormClosing += new FormClosingEventHandler(frmMain_FormClosing);
}

//expose the main form's closing event through COM
private void frmMain_FormClosing(object sender, FormClosingEventArgs e)
{
if (null != OnFormClose) OnFormClose();
}

public void ShowForm(string winTitle)
{
if (winTitle == "") winTitle = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
mFrm.Show();
}

[/tt]
Here is my script code
[tt]
Set obj = WScript.CreateObject("DHI.WFScriptWindow","_hej")
obj.ShowForm "min titel"
for i=0 to 10
wscript.sleep 2000
next
sub hej_OnFormClose
msgBox "end"
end sub
[/tt]
The C# dll is, strongnamed, registered in the GAC and coverted to a type lib by regasm.

Any help will be greatly appreciated.

Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
I got one step closer. It should be
[tt]ComSourceInterfaces(typeof(IFormCloseEvent))[/tt]

and there is a typo in the above, the prefix should have been "hej_":
[tt]Set obj = WScript.CreateObject("DHI.WFScriptWindow","hej_")[/tt]

The script now subscribes to the event (because OnFormClose!=null), but when the event raises, I get the following error:
[tt]
System.NotImplementedException: The method or operation is not implemented.
at System.RuntimeType.InvokeDispMethod(String name, BindingFlags invokeAttr, Object target, Object[] args, Boolean[] byrefModifiers, Int32 culture, String[] namedParameters)
at System.RuntimeType.InvokeMember(String name, BindingFlags bindingFlags, Binder binder, Object target, Object[] providedArgs, ParameterModifier[] modifiers, CultureInfo culture, String[] namedParams)
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData& msgData)
at DHI.WF.WFScriptWindow.IFormCloseEvent.OnFormClose()
at DHI.WF.WFScriptWindow.WFScriptWin.frmMain_FormClosing(Object sender, FormClosingEventArgs e)
at System.Windows.Forms.Form.OnFormClosing(FormClosingEventArgs e)
at System.Windows.Forms.Form.WmClose(Message& m)
at System.Windows.Forms.Form.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)[/tt]

It is as if it does not understand the syntax of the prefix...


Sunaj
'The gap between theory and practice is not as wide in theory as it is in practice'
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top