[Gtk-sharp-list] Gtk 1.0.6 gives invalid cast exception on internal GdkWindow related calls when using the .net runtime.

Anset Anset@anset.org
Mon, 14 Mar 2005 12:48:14 +0100


Hi,

A couple of days ago, I had to upgrade the Gtk-sharp on my Win32 box 
because I am using Gtk-sharp 1.0.6 on my linux (gentoo) box. I am doing 
cross OS development and there seem to be incompatibilities between 
Gtk-sharp 1.0.4 and 1.0.6. (To be complete, on my linux box, everything 
works great.)

The one I came across was the argument list of the 
Gtk.RenderPixmapAndMask method. In 1.0.4 the mask argument must be a 
Gtk.Bitmap, in 1.0.6 it must be a Pixmap. Basically I don't care which 
is used, but it does break my apps... Both 1.0.4 and 1.0.6 are installed 
in the GAC with a 1.0.0.0 version number, so the dll versioning of the 
Gac does not catch this. :(

But anyway, I decided to download the Win32 sdk from novel forge (thank 
you Mr. Martinez :) )
But unfortunately, that did not make me a happy camper... I got problems 
with both Mono and .Net.
(I was able to solve some of the problems, but I list them here anyway.)

I made a small test program that works fine witht the 1.0.4 Gtk-sharp as 
it was delivered in the Win32 sdk version 1.0.2. After installing the 
1.0.2 sdk version (all from novel forge) the 1.0.4 libs are overwritten 
by the 1.0.6 versions. (I do not use the 1.9.2 libs that are labeled 
2.0.0.0 in the Gac. I keep using the 1.0.0.0 version which, as far as I 
understand means I am using the 1.0.0.0 versions of the libs.)

The test program will show the 2 problems I was/am now seeing.
I am using sharpdevelop and all the needed libs are included in the 
References (again, all 1.0.0.0 versions)

Here is the program:

using System;

class Test  : Gtk.Window
{	
	public static void Main(string[] args)
	{
		Gtk.Application.Init ();
		new Test ();
		Gtk.Application.Run ();
	}
		
	private Test() : base ("Test")
	{
		this.DeleteEvent += new Gtk.DeleteEventHandler (On_Delete);

		this.Realize();
		this.GdkWindow.SetDecorations(Gdk.WMDecoration.Resizeh);
		
		Gdk.Pixbuf pb = Gdk.Pixbuf.LoadFromResource("Start_Image1.png");

		Gdk.Pixmap mask_pm;
		Gdk.Pixmap Background_Image;
		pb.RenderPixmapAndMask(out Background_Image, out mask_pm, 0);
						
		this.ShowAll();
			
	}
				
	private void On_Delete (object o, Gtk.DeleteEventArgs args)
	{
		Gtk.Application.Quit ();
	}
}

Compilation with either csc or mcs works without fault. (mcs gives 
wanings about unused variables, but that is all).

Running the program with either .Net or Mono will give different errors:

Case 1. Running with .Net:
--------------------------
Unhandled Exception: System.InvalidCastException: Specified cast is not 
valid.
    at GLib.ObjectManager.CreateObject(IntPtr raw)
    at GLib.Object.GetObject(IntPtr o, Boolean owned_ref)
    at Gtk.Widget.get_GdkWindow()
    at Test..ctor() in c:\SharpDevelop Projects\Test\Main.cs:line 17
    at Test.Main(String[] args) in c:\SharpDevelop 
Projects\Test\Main.cs:line 8
Press any key to continue . . .

This complains about the SetDecorations call. It would appear that the 
GdkWindow is not accepted. I have no idea why...

If I comment out that line an tray again, I now get:

Unhandled Exception: System.InvalidCastException: Specified cast is not 
valid.
    at GLib.ObjectManager.CreateObject(IntPtr raw)
    at GLib.Object.GetObject(IntPtr o, Boolean owned_ref)
    at GLib.Object.GetObject(IntPtr o)
    at Gdk.Pixbuf.RenderPixmapAndMask(Pixmap& pixmap_return, Pixmap& 
mask_return, Int32 alpha_threshold)
    at Test..ctor() in c:\SharpDevelop Projects\Test\Main.cs:line 24
    at Test.Main(String[] args) in c:\SharpDevelop 
Projects\Test\Main.cs:line 8
Press any key to continue . . .

I don't know what goes wrong here.

Case2. Running with Mono (or Mono Runtime interpreter):
-------------------------------------------------------

** (<unknown>:3012): WARNING **: Missing method RenderPixmapAndMask in 
assembly
C:\SharpDevelop Projects\Test\bin\Debug\Test.exe typeref index 20

This application has requested the Runtime to terminate it in an unusual 
way.
Please contact the application's support team for more information.
Press any key to continue . . .

This time, the cast is no problem, which is great, but now the 
RenderPixmapAndMask method is not found. And I need it for my program.

I don't understand why this would only break in mono...

The solution to this Mono problem was actually quite simple. I was still 
working with the 1.0.2 version of mono and it would appear that this 
version does not like Gtk-sharp 1.0.6... I removed the older mono and 
the gtk-sharp installs (just to keep it clean), downloaded and installed 
the 1.0.6 version and then reinstalled the gtk-sharp 1.9.2.

After this (and adapting the PATH environment var) I can work again, 
using either the csc or mcs compilers. But I have to use the Mono 
runtime. he .net runtime still gives the same problem.

Now, the mono solution makes me think I am having the same kind of 
problem with the .Net runtime.
I have upgraded my xp to SP2 and the .net runtime to the latest versions 
available, but sadly, the problem remains.

Can anybody help me with this?
If more info is needed, I will gladly provide it.

Best regards,

Anset.