UOAI and WinForms: Why is this COM exception occuring?

The latest version of the UOAI library and all directly related information can be found in this forum.

UOAI and WinForms: Why is this COM exception occuring?

Postby Kaylee on Sat Jul 04, 2009 7:22 am

(I was going to post this in the .NET forum, but I don't seem to have permission to post there.)

I've been playing around with the .NET wrapper for a while, and most everything as worked great. However, today I started with to write a WinForms interface for one of my programs, and I get a COM exception whenever I close the window.

Right now the code is very simple, when the form loads it does this:
Code: Select all
uoai = new UOAI.UOAI();
client = uoai.Clients[0];
charNameLabel.Text = String.Format("Character: {0} (0x{1:X})", client.PlayerName, client.Player.Serial.Value);


I get this error when I close the form:

Code: Select all
InvalidComObjectException was unhandled
COM object that has been separated from its underlying RCW cannot be used.

System.Runtime.InteropServices.InvalidComObjectException was unhandled
  Message="COM object that has been separated from its underlying RCW cannot be used."
  Source="UOAINET"
  StackTrace:
       at UOAI.COMInterfaces.UOMobile.get_ID()
       at UOAI.Mobile.get_Serial()
       at UOAI.Mobile.Dispose()
       at UOAI.Mobile.Finalize()
  InnerException:
Kaylee
n00b
 
Posts: 7
Joined: Fri Mar 27, 2009 12:48 am

Re: UOAI and WinForms: Why is this COM exception occuring?

Postby Artaxerxes on Sat Jul 04, 2009 9:42 am

Kaylee wrote:(I was going to post this in the .NET forum, but I don't seem to have permission to post there.)


That shouldn't be, i'll see if i can fix the permissions.

I think the error you get should be solved if you make sure your programs main function has the [MTAThread] attribute(In C# its the "main"-function in program.cs, don't know about vb.net).

This error has to do with the fact that UOAI lives in a multi-threaded COM appartment, whereas by default your windows forms app lives in a single threaded appartment. That means proper COM and other marshalling is required to communicate between the appartments and .NET fails to do that correctly. If you specify a MTAThread for your main function, your app will live in a multi-threaded appartment and since there is only one of those per process it will be the same appartment as UOAI, so no marshalling is required... therefore solving the problem.
The problem is basically that C# uses multiple marshalling levels : C# -> RCW -> COM -> COM inter-appartment marshalling and at the RCW level its not clear to what thread the RCW objects actually belongs, resulting in the garbage collector cleaning up the RCW before UOAI.Net actually gets to disposing the COM objects.

This will be resolved once the next version of UOAI.NET arrives, since it will be turned into a managed library that is independent of the COM library, so no longer requiring all this marshalling. Can't give an ETA yet, but i will probably be working actively again on UOAI starting from the end of next week.

Greetz,
Artaxerxes
Artaxerxes
Site Admin
 
Posts: 530
Joined: Tue Nov 18, 2008 9:51 pm

Re: UOAI and WinForms: Why is this COM exception occuring?

Postby Kaylee on Sat Jul 04, 2009 12:58 pm

That fixed it, thanks :D
Kaylee
n00b
 
Posts: 7
Joined: Fri Mar 27, 2009 12:48 am

Re: UOAI and WinForms: Why is this COM exception occuring?

Postby Lothiack on Sun Sep 27, 2009 4:08 am

I got the same problem. Can´t really try the proposed solution right now, but I'll try to post another reply if it does work. :D

EDIT: Yep, that does work. Thanks!
Lothiack
n00b
 
Posts: 5
Joined: Sun Sep 27, 2009 3:43 am

Re: UOAI and WinForms: Why is this COM exception occuring?

Postby cc10 on Wed Oct 21, 2009 5:18 am

I have this same problem, but in VB.NET...would anybody know how to fix that?

edit: found this answer just now. for those who are interested:

insert the following code in your main form (in this case Form1):

Code: Select all
<MTAThread()> _
    Public Shared Sub Main()
        ' Run a standard application message loop on the current thread.
        Application.Run(New Form1())
    End Sub


Now go in the properties of the project and disable the application framework. Once this is done, set the Startup object to "Sub Main".

That's it!
cc10
n00b
 
Posts: 2
Joined: Sat Oct 17, 2009 6:45 pm


Return to UOAI Developers Forum

Who is online

Users browsing this forum: No registered users and 1 guest

cron