[plt-scheme] MzScheme and .NET

From: Chris Double (chris at double.co.nz)
Date: Tue Mar 18 03:33:29 EST 2003

"Pedro Pinto" <ppinto at cs.cmu.edu> writes:

> I am speculating that it might be possible to map .NET namespaces
> into MzScheme modules (a hack no doubt, but maybe one that can be
> lived with).

I planned to do this with Goo too but never got around to it. The idea
was for my quick and dirty C# program to generate wrappers for the
reflection libraries and then write Goo code that used these wrappers
to create an FFI that allowed dynamic loading and executing of .NET
assemblies. That way I wouldn't have to compile and link Managed C++
code whenever I wanted to use a new .NET class. That Goo code would
generate 'better' wrappers using namespaces, etc. I've not yet got
around to doing that though.

I just noticed that the distribution I have on my website does not
include the source to the C# program that generates the wrappers. I'll
hunt it down and make it available.

> How did you address ref arguments? I have no idea how to represent
> this in Scheme.

Depending on what the argument is I do various things to it. All .NET
objects are represented on the Goo aside as an instance of a class
that stores an integer. This integer is an index into a .NET array
which points to the actual .NET object. So code to call .NET must
marshall and unmarshall all this. Here's an example:

extern "C" 
clrobj  
  system_net_webproxy_getproxy_uri(clrobj self, clrobj destination)
{
	try
	{
		System::Uri* _destination = 
          dynamic_cast<System::Uri*>(CLRHolder::Get(destination));
		System::Net::WebProxy* _self = 
          dynamic_cast<System::Net::WebProxy*>(CLRHolder::Get(self));
		System::Uri* _ret = _self->GetProxy(_destination);
		clrobj ret__ret = CLRHolder::Add(_ret);
		return ret__ret;
	}
	catch(Exception* e)
	{
		dotnetraiseerror(dnstring2cstring(e->get_Message()));
	}
	return 0;
}

CLRHolder::Add and ::Get convert to/from the index I mentioned above
to the .NET object by retrieving it from the array. Things like arrays
and such are a little bit different since I need to copy data back and
forth. Basically I create a .NET array to hold the data. Copy the data
from the Goo array into it. Call the .NET routine. Then copy the data
back into the original array.

If you are interested in an example of how a particular method is
handled let me know and I'll show you the generated code.

The downside to my approach is you get two garbage collected
heaps. The .NET one and the Goo one. But at least it works.

Chris.




Posted on the users mailing list.