[Gtk-sharp-list] ListBase struct

Jeremy Wells jeremy@olicomp.com
Fri, 02 Apr 2004 21:20:48 +0100


--=-2uAEQuvt5EwZ95Jl374s
Content-Type: text/plain
Content-Transfer-Encoding: 7bit

I'm getting the error message

** ERROR **: Type GLib.ListBase which is passed to unmanaged code must
have a StructLayout attribute

when executing the following code:

[DllImport("libgnomevfs-2")]
static extern IntPtr gnome_vfs_mime_get_default_application(string
mime_type);

...

IntPtr ptr = gnome_vfs_mime_get_default_application(mime_type);
GnomeVFSMimeApplication ret = GnomeVFSMimeApplication.New(ptr);

...

GnomeVFSMimeApplication self = new GnomeVFSMimeApplication();
self = (GnomeVFSMimeApplication) Marshal.PtrToStructure (raw,
self.GetType ());

with the very last line being the problem. I can't understand how
ListBase is even involved in this. can anyone help me out? Original file
is attached.

Jeremy

--=-2uAEQuvt5EwZ95Jl374s
Content-Disposition: attachment; filename=Vfs.cs
Content-Type: text/x-csharp; name=Vfs.cs; charset=ANSI_X3.4-1968
Content-Transfer-Encoding: 7bit

/*
This file is derived from
	Vfs.cs
	Author: John Luke  <jluke@cfl.rr.com>
	License: LGPL

	and

	GnomeIconLookup.cs
	Copyright (C) 2003, Mariano Cano Prez <mariano.cano@hispalinux.es> 
	Licence: GPL
*/


using GtkSharp;
using Glade;
using Gnome;
using GLib;
using Gtk;
using Gdk;
using System.IO;
using System;
using System.Runtime.InteropServices;

public enum GnomeVFSMimeApplicationArgumentType
{
	Uris,
	Path,
	UrisForNonFiles
}

[StructLayout(LayoutKind.Sequential)]
public struct GnomeVFSMimeApplication
{
	public string id;
	public string name;
	public string command;
	public bool can_open_multiple_files;
	public GnomeVFSMimeApplicationArgumentType expects_uris;
	public List supported_uri_schemes;
	public bool requires_terminal;
	
	public IntPtr reserved1;
	public IntPtr reserved2;

	public static GnomeVFSMimeApplication Zero = new GnomeVFSMimeApplication ();
	
	public static GnomeVFSMimeApplication New (IntPtr raw)
	{
		if(raw == IntPtr.Zero)
		{
			return GnomeVFSMimeApplication.Zero;
		}
		GnomeVFSMimeApplication self = new GnomeVFSMimeApplication();
		self = (GnomeVFSMimeApplication) Marshal.PtrToStructure (raw, self.GetType ());
		return self;
	}

	public static bool operator == (GnomeVFSMimeApplication a, GnomeVFSMimeApplication b)
	{
		return a.Equals (b);
	}

	public static bool operator != (GnomeVFSMimeApplication a, GnomeVFSMimeApplication b)
	{
		return ! a.Equals (b);
	}

	public override bool Equals (object o)
	{
		 if (!(o is GnomeVFSMimeApplication))
			 return false;

		return ((GnomeVFSMimeApplication) o) == this;
	}
	
	public override int GetHashCode ()
	{
		return this.GetHashCode ();
	}
}

public class Vfs
{
	static Gnome.IconTheme iconTheme;
	static Gnome.ThumbnailFactory thumbnailFactory;

	[DllImport ("gnomevfs-2")]
	static extern bool gnome_vfs_init ();
		
	[DllImport ("gnomevfs-2")]
	static extern bool gnome_vfs_initialized ();
	
	[DllImport ("gnomevfs-2")]
	static extern bool gnome_vfs_shutdown ();
		
	[DllImport ("gnomevfs-2")]
	static extern string gnome_vfs_get_mime_type (string uri);
		
	[DllImport ("gnomevfs-2")]
	static extern string gnome_vfs_get_mime_type_for_data (string data, int length);
	
	[DllImport ("gnomevfs-2")]
	static extern string gnome_vfs_mime_get_icon (string mime_type);
		
	[DllImport ("gnomevfs-2")]
	static extern bool gnome_vfs_mime_type_is_known (string mime_type);
	
	[DllImport ("gnomevfs-2")]
	static extern string gnome_vfs_mime_get_description (string mime_type);
	
	[DllImport("libgnomevfs-2")]
	static extern IntPtr gnome_vfs_application_registry_get_applications(string mime_type);

	[DllImport("libgnomevfs-2")]
	static extern IntPtr gnome_vfs_mime_get_default_application(string mime_type);
		
	private Vfs ()
	{
	}
	
	// gnome_program_init calls this for you
	public static bool Init ()
	{
		iconTheme = new Gnome.IconTheme();	
		thumbnailFactory = new Gnome.ThumbnailFactory(ThumbnailSize.Normal);

		return gnome_vfs_init ();
	}
	
	public static string GetIcon (string mimetype)
	{
		return gnome_vfs_mime_get_icon (mimetype);
	}
	
	public static string GetMimeType (string filename)
	{
		return gnome_vfs_get_mime_type (filename);
	}
	
	public static string GetMimeTypeFromData (string data)
	{
		return gnome_vfs_get_mime_type_for_data (data, data.Length);
	}
		
	public static string GetDescription(string mimetype)
	{
		return gnome_vfs_mime_get_description (mimetype);
	}
		
	public static bool IsKnownType (string mimetype)
	{
		return gnome_vfs_mime_type_is_known (mimetype);
	}
		
	public static GLib.List GetApplications(string mime_type)
	{
		IntPtr raw_ret = gnome_vfs_application_registry_get_applications(mime_type);
		GLib.List app = new GLib.List(raw_ret);
		return app;
		}
	
	public static GnomeVFSMimeApplication GetDefaultAction(string mime_type)
	{
		IntPtr ptr = gnome_vfs_mime_get_default_application(mime_type);
		GnomeVFSMimeApplication ret = GnomeVFSMimeApplication.New(ptr);
		return ret;
	}
		
	public static void RunDefaultApplication(string file)
	{
		try {
   			string mime_type = GetMimeType(file);
   			GnomeVFSMimeApplication da = GetDefaultAction(mime_type);
   			string command = da.command;
   			System.Diagnostics.ProcessStartInfo p = new System.Diagnostics.ProcessStartInfo(da.command,file);		
   		} catch {
   		}
   	}

	public static Pixbuf GetIcon(string filename, int size)
	{
		Gdk.Pixbuf pixbuf;

		try {
			Gnome.IconLookupResultFlags result;
			string icon = Gnome.Icon.LookupSync(iconTheme, thumbnailFactory, filename, "", Gnome.IconLookupFlags.None, out result);
	
			int i;
			string p_filename = iconTheme.LookupIcon (icon, size, new Gnome.IconData (), out i);
			pixbuf = new Gdk.Pixbuf (p_filename); 
		} catch {
			pixbuf = new Gdk.Pixbuf(null, "file.gif");
		}
	
		return pixbuf.ScaleSimple(size,size,Gdk.InterpType.Bilinear);
	}

	public static DirectoryInfo GetHome() 
	{
		return new DirectoryInfo(Gnome.User.DirGet()).Parent;
	}
		
	public static bool Shutdown ()
	{
		return gnome_vfs_shutdown ();
	}
		
	public static bool Initialized
	{
		get { return gnome_vfs_init (); }
	}
}

--=-2uAEQuvt5EwZ95Jl374s--