[Mono-devel-list] cocoa#

Patrick Beard pcbeard at mac.com
Sat Jun 19 13:12:31 EDT 2004


I know of no official Cocoa# effort. However, here's a little sample 
code I've written that might get things started. I'm still learning C# 
myself, so I don't know if there is a more direct way to get a library 
loaded, so this causes the Cocoa umbrella framework to be loaded by 
calling into NSRealMemoryAvailable().

- Patrick

using System;
using System.Reflection;
using System.Runtime.InteropServices; // for DllImport.

public class CocoaTest {
     const string libobjc = "libobjc.dylib";

     [DllImport(libobjc, EntryPoint="objc_getClass", 
CharSet=CharSet.Ansi)]
     public static extern int objc_getClass(string name);

     [DllImport(libobjc, EntryPoint="sel_registerName", 
CharSet=CharSet.Ansi)]
     public static extern int sel_registerName(string name);

     [DllImport(libobjc, EntryPoint="objc_msgSend")]
     public static extern int objc_msgSend(int self, int cmd);

     [DllImport(libobjc, EntryPoint="objc_msgSend", 
CharSet=CharSet.Ansi)]
     public static extern int objc_msgSend(int self, int cmd, string 
arg);

     static int selector(string name) {
         return sel_registerName(name);
     }

     const string Cocoa = 
"/System/Library/Frameworks/Cocoa.framework/Cocoa";

     [DllImport(Cocoa, EntryPoint="NSRealMemoryAvailable")]
     public static extern uint NSRealMemoryAvailable();

     [DllImport(Cocoa, EntryPoint="NSLog")]
     public static extern void NSLog(int message);

     public static void Main(string[] args) {
         // Call into Cocoa to get it loaded.
         uint realMemoryAvailable = NSRealMemoryAvailable();
         Console.WriteLine("realMemoryAvailable = " + 
realMemoryAvailable);
         // Push an autorelease pool.
         int NSAutoreleasePool = objc_getClass("NSAutoreleasePool");
         int pool = objc_msgSend(NSAutoreleasePool, selector("new"));
         // allocate an NSString.
         int NSString = objc_getClass("NSString");
         int message = objc_msgSend(NSString, 
selector("stringWithCString:"), "Hello from Cocoa!");
         NSLog(message);
         objc_msgSend(pool, selector("release"));
     }
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: text/enriched
Size: 2168 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20040619/117b5ba4/attachment.bin 


More information about the Mono-devel-list mailing list