change screen resolution with objective-c

i noticed that if my mac starts up with out the external display or projector being powered on already, the screen resolution is not always as set before.
so i put this together from a post i found here:

void testApp::changeScreenRes(int h, int v){  
	CGRect screenFrame = CGDisplayBounds(kCGDirectMainDisplay);  
	CGSize screenSize  = screenFrame.size;  
	printf("current resolution is %f %f\n", screenSize.width, screenSize.height);  
	if(h != screenSize.width || v != screenSize.height){  
		CGDirectDisplayID display = CGMainDisplayID(); // ID of main display  
		CFDictionaryRef mode = CGDisplayBestModeForParameters(display, 32, h, v, NULL); // mode to switch to  
		CGDisplayConfigRef config;  
		if (CGBeginDisplayConfiguration(&config) == kCGErrorSuccess) {  
			CGConfigureDisplayMode(config, display, mode);  
			CGCompleteDisplayConfiguration(config, kCGConfigureForSession );  
			sprintf(logStr, "changed screen resolution to  %i, %i ",h,v);  
			printf(logStr, "Error changing resolution to %i, %i ",h,v);  
	} else{  
		printf(logStr, "screen resolution did not need to be changed. it is %i, %i ",h,v);  

it does not notice if i am trying to set it to a resolution larger than supported by the display.