Sign in to follow this  
Followers 0
Zohar

How to Receive an Event from a .NET Form in AutoIt

7 posts in this topic

#1 ·  Posted (edited)

Hi

 

I created a simple Form in C#, in a DLL project.

I marked that Form with the ComVisible(true) attribute,

and in the project properties for that Class Library project, in the Build tab, I checked the "Register for COM Interop" checkbox.

Just like I describe in this thread:

Calling a .NET DLL from AutoIt, with ComVisible(true)

 

After using ObjCreate(), I now want to try and use ObjEvent() on that Form,

in order to receive one of the Form's events.

 

Here is what I have done:

Local $F =ObjCreate("AutoItInterop.Form1")
ObjEvent($F,"Form1_")
$F.Show()
Sleep(50000)



Func Form1_MouseMove()
    MsgBox(0,"","hello")
EndFunc

Unfortunately, the code inside the Form1_MouseMove() function never gets ran.

 

What do I need to do to successfully catch the event inside AutoIt?

 

Thank you

Edited by Zohar

Share this post


Link to post
Share on other sites



#2 ·  Posted (edited)

I must ask you, seriously, if you only want to use C# to create GUI, so why don't you use C# with AutoItX? It's far easier, event is handled directly in C# code, and you don't need to mess with COM object event anymore. You even don't need to expose your class to outside world using ComVisible(true).

Edited by binhnx

99 little bugs in the code

99 little bugs!

Take one down, patch it around

117 little bugs in the code!

Share this post


Link to post
Share on other sites

#3 ·  Posted (edited)

I must ask you, seriously, if you only want to use C# to create GUI, so why don't you use C# with AutoItX?

Amazing how many times this has been asked, with the offer to use AutoItX  :)

The answer:

1) If you read about AutoItX, you will see that it exposes only a part of the AutoIt functions - the basic ones.

2) UDFs are not accessible to you If you are in .NET and using AutoItX. So you lose many useful UDFs if you work only in .NET.

That's why, when I need to create GUI, I combine both C# and AutoIt.

 

I hope someone can help me with getting ObjEvent() work..

Edited by Zohar

Share this post


Link to post
Share on other sites

1. The part which does not exist is mostly on GUI functions. Since you use C# to create GUI, AutoItX can do as much as AutoIt does (and may be superior when compare execution speed)

2. It's very very (I cannot say how that much) silly to think like that. If you're in .NET you will have access to tons of library which is much more flexible and stable than the AutoIt's UDF.

Anyway, to use event with COM using .NET is quite complex. First, you will need to declare an event interface. This event must be of type IDispatch

[ComVisible(true), InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface Form1_Event
{
    [DispId(100)]
    void FormMouseMove(int x, int y);
};

Note the DispId attribute. You can pass any int number to it. But if you remove the attribute, you cannot make the event work. 

Then provide your COM class. This class should not inherit from the event interface.

[ComVisible(true), ComSourceInterfaces(typeof(Form1_Event))]
public class Form1 : Form
{
    public delegate void MouseMoveDelegate(int x, int y);
    public event MouseMoveDelegate FormMouseMove;

    public Form1()
    {
        this.MouseMove += new MouseEventHandler(Form1_MouseMove);
    }

    public void Form1_MouseMove(object sender, MouseEventArgs e)
    {
        if (FormMouseMove != null)
            FormMouseMove.Invoke(e.X, e.Y);
    }
 }

The event name and delegate function signature must be the same as the one declared in the event interface.

You must manually delegate all the event you want to expose to outside. 'Cuz the .NET Form event is inherited from Control class but no event interface than all of them is useless from COM object's point of view


99 little bugs in the code

99 little bugs!

Take one down, patch it around

117 little bugs in the code!

Share this post


Link to post
Share on other sites

#5 ·  Posted (edited)

1. The part which does not exist is mostly on GUI functions. Since you use C# to create GUI, AutoItX can do as much as AutoIt does (and may be superior when compare execution speed)

This is not correct..

I am talking about many UDFs regarding Automation..

 

2. It's very very (I cannot say how that much) silly to think like that. If you're in .NET you will have access to tons of library which is much more flexible and stable than the AutoIt's UDF.

Indeed I create many programs in .NET and use many of the classes that the .NET framework supply,

but when the project is related to Automation, then .NET is not the best choice for me,

and instead I use AutoIt.

If the project requires GUI, then I avoid using AutoIt's GUI options, and instead of .NET for it, and combine between the two voa Com Interop..

 

 

Anyway, to use event with COM using .NET is quite complex. First, you will need to declare an event interface. This event must be of type IDispatch

Thank you

I read the code you gave, and also after reading about it via searching in Google,

indeed exposing events in .NET to COM seems to complicate the code..

So for this reason I decided to create a (much) simpler workaround.

The workaround is th catch the events in the Form in some EventHandler which keeps the event names in a Queue,

and then I provide a simple function named GetEvent() which rturns a string, and this function is called every 100ms or so.

Or in simple words: Providing a method for polling..

 

 

Exposing methods in .NET to COM is clean and neat.

Exposing events, unfortunately, is not so..

The code complication is not worth it, and instead a Polling workaround gets the job done with much less code complication.

Edited by Zohar

Share this post


Link to post
Share on other sites

#6 ·  Posted (edited)

Sure? 

A quick Google point to this:

http://stackoverflow.com/questions/2052915/whats-a-good-if-any-net-windows-automation-library

It's recommend White framework.

The interesting part is it also suggest AutoItX

Edit: The polling solution is promissing, if you don't need to worry about your CPU :)

Edited by binhnx

99 little bugs in the code

99 little bugs!

Take one down, patch it around

117 little bugs in the code!

Share this post


Link to post
Share on other sites

#7 ·  Posted (edited)

I played with the White framework, and it is very nice,

but If you compare it to AutoIt, you will see that AutoIt is much more powerful than White.

Back then when I started with AutoIt, I really wanted to stay in the .NET realm, that's why I looked for .NET Automation options.

But AutoIt is simply better, so it's worth to "step out" of the .NET world, and use it too :)

Edited by Zohar

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0