Skip to content

Instantly share code, notes, and snippets.

@rdpate
Forked from TooTallNate/.gitignore
Created July 9, 2011 20:01
Show Gist options
  • Save rdpate/1073904 to your computer and use it in GitHub Desktop.
Save rdpate/1073904 to your computer and use it in GitHub Desktop.
low-level objc runtime apis
*
!*.m
!Makefile
// To compile: gcc -o array array.m -lobjc -framework Foundation
#include <objc/runtime.h>
#include <dlfcn.h>
#include <stdio.h>
int main() {
void *sdl_library = dlopen("/System/Library/Frameworks/Foundation.framework/Versions/Current/Foundation", RTLD_LAZY);
if (sdl_library == NULL) {
printf("Got dlopen error!\n");
} else {
printf("dlopen successful!\n");
// Set up an NSAutoreleasePool
Class nsautoreleasepool = objc_getClass("NSAutoreleasePool");
id pool = class_createInstance(nsautoreleasepool, 0);
SEL initSel = sel_registerName("init");
id poolAfterInit = objc_msgSend(pool, initSel);
// Create an NSMutableArray
Class nsmutablearray = objc_getClass("NSMutableArray");
// Regular way to create an NSMutableArray,
// equivalent to [[NSMutableArray alloc] initWithCapacity: 10]
id array = objc_msgSend(nsmutablearray, sel_registerName("alloc"));
id arrayAfterInit = objc_msgSend(array, sel_registerName("initWithCapacity:"), 10);
// Alternative, shorthand way of creating an NSMutableArray,
// equivalent to [NSMutableArray arrayWithCapacity: 10]
//id arrayAfterInit = objc_msgSend(nsmutablearray, sel_registerName("arrayWithCapacity:"), 10);
// Add an NSString and the nsma class reference to the array
objc_msgSend(arrayAfterInit, sel_registerName("addObject:"), @"test");
objc_msgSend(arrayAfterInit, sel_registerName("addObject:"), nsmutablearray);
// Print out the length and debug printout
NSLog(@"%d", objc_msgSend(arrayAfterInit, sel_registerName("count")));
NSLog(@"%@", arrayAfterInit);
}
return 0;
}
// To compile: gcc -o hello hello.m -lobjc
#include <objc/runtime.h>
#include <dlfcn.h>
#include <stdio.h>
int main() {
void *sdl_library = dlopen("/System/Library/Frameworks/Foundation.framework/Versions/Current/Foundation", RTLD_LAZY);
if (sdl_library == NULL) {
printf("Got dlopen error!\n");
} else {
printf("dlopen successful!\n");
// Set up an NSAutoreleasePool
Class nsautoreleasepool = objc_getClass("NSAutoreleasePool");
id pool = class_createInstance(nsautoreleasepool, 0);
SEL initSel = sel_registerName("init");
id poolAfterInit = objc_msgSend(pool, initSel);
// Set up a NSString with the contents "Hello World" from a C string
Class nsstring = objc_getClass("NSString");
SEL stringUTF8sel = sel_registerName("stringWithUTF8String:");
id hello = objc_msgSend(nsstring, stringUTF8sel, "Hello World\n");
// Print it back out as a C string
printf("%s", (char *)objc_msgSend(hello, sel_registerName("UTF8String")));
}
return 0;
}
# using CXXFLAGS for Obj-C is *really* weird
CC = gcc
CXXFLAGS = -lobjc -framework Foundation
FILES = hello.m array.m
.PHONY: all
all: hello array
%: %.m
$(CC) $(CXXFLAGS) -o $@ $<
.PHONY: clean
clean:
rm -f hello array
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment