[Gtk-sharp-list] Getting Application.Init (args) to work

Jonathan Pryor jonpryor@vt.edu
Wed, 03 Dec 2003 07:44:12 -0500

I'm likely missing something, but why do you need to special-case the
32-bit and 64-bit handling?  Why don't you just use an IntPtr[] instead
of int[] (for 32-bit platforms) and long[] (for 64-bit platforms)?

For example:

	static IntPtr make_buf (string[] progargs)
		InitPtr[] ptrs = new IntPtr[progargs.Length];
		for (int i = 0; i < ptrs.Length; ++i)
			ptrs[i] = (IntPtr) Marshal.StringToHGlobalAuto (progargs[i]);

		IntPtr buf = g_malloc (
			(ulong) Marshal.SizseOf (typeof(string)) 
			* (ulong) ptrs.Length

		Marshal.Copy (ptrs, 0, buf, ptrs.Length);

		return buf;

I haven't tried running this, but it seems that it should work properly,
without requiring special handling between 32- and 64-bit platforms.

 - Jon

On Tue, 2003-12-02 at 21:57, Peter Williams wrote:
> Hi,
> 	I've been working on getting Application.Init () to work when given
> commandline arguments. The current code just tries to use a "ref
> string[] args" parameter when calling gtk_init (), but that doesn't
> work, because the runtime doesn't support passing references to arrays
> into unmanaged code. Or at least, if you try to use the code right now,
> it totally breaks. (Another problem is that gtk_init() expects argv[0]
> to be the program name, which is not the case if you pass it the
> arguments given to your Main() function.)
> 	Anyway, I cooked up a patch to get this working, based on the advice
> here: 
> http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpguide/html/cpconarrayssample.asp
> Unfortunately, the result is nasty, and then there's the issue of 64-bit
> safety, which I have attempted to address. There's probably several ways
> to go about it; I chose the one that I thought created the least
> confusing code. (It works on my 32-bit system; the 64-bit version is
> untested.)
> 	Mike and I would like to find a neater solution to the problem, so I'd
> be interested if someone more familiar with the runtime had an idea
> about a better approach.
> 	Peter