New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

APIs to determine OS in mac..PLz help


Post Reply New Thread Subscribe

 
Thread Tools
roublesaha

 
Member Since: Oct 27, 2008
Posts: 13
roublesaha is on a distinguished road

roublesaha is offline
Hi All

I have a snippet of code which works in 10.4 but it supported in 10.3.

Hence i want some APIs to determine the OS , so that the snippet gets executed in the proper OS.

Also, please let me know if u have any better way to distinguish between the versions programmatically.

******Rouble******
QUOTE Thanks
roublesaha

 
Member Since: Oct 27, 2008
Posts: 13
roublesaha is on a distinguished road

roublesaha is offline
I have got this from help :-

NSString *str = [[NSProcessInfo processInfo] operatingSystemVersionString];

But i am not able to use it properly.

******Rouble******
QUOTE Thanks
roublesaha

 
Member Since: Oct 27, 2008
Posts: 13
roublesaha is on a distinguished road

roublesaha is offline
I am working with C++ source code

******Rouble******
QUOTE Thanks
mystic_fm

 
Member Since: Mar 15, 2007
Posts: 161
mystic_fm will become famous soon enough
Mac Specs: 17" MacBook Pro, 2.33GHz C2D, 2GB RAM

mystic_fm is offline
Quote:
Originally Posted by roublesaha View Post
I have got this from help :-

NSString *str = [[NSProcessInfo processInfo] operatingSystemVersionString];

But i am not able to use it properly.

I am working with C++ source code
That's because you can't utilize the Cocoa frameworks directly from C++.

There are a few ways to approach this; which one is best depends upon the specifics of your needs. Since you haven't provided any details about why you are trying to do this in C++, I'll take a wild guess that yours is a cross-platform app that doesn't depend upon a GUI front end but which does need OS version information for some reason.

You can access Cocoa framework classes from an Objective-C++ file (i.e., a C++ file with the extension ".mm"). Assuming that redefining the top level of your C++ application as an Objective-C++ file is not an option (which it wouldn't be for a cross-platform app), one approach would be to create a separate Objective-C++ file to do the work, as in the following (very rough) example code:

Code:
main.cpp:

#include "MyCocoaBridge.h";

int main (int argc, char * const argv[])
{
    getVersion();
    return 0;
}

---------------------------------

MyCocoaBridge.h:

void getVersion(void);

---------------------------------

MyCocoaBridge.mm:

#import <Foundation/Foundation.h>
#include <iostream>

void getVersion(void)
{
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    NSString *version = [[NSProcessInfo processInfo] operatingSystemVersionString];
    std::cout << [version UTF8String] << "\n";
    [pool drain];
    return;
}
In a cross-platform app you would of course also have to work out your projects and calls in such a way to replace the .mm file with something else in builds for non-Cocoa environments. And don't forget to add the Foundation framework to your Xcode project.

I hope this helps to put you on a workable track.
QUOTE Thanks
roublesaha

 
Member Since: Oct 27, 2008
Posts: 13
roublesaha is on a distinguished road

roublesaha is offline
I have tried your method...but it is giving errors :

I am using a empty project and included al the frameworks manually.
----------------------------------------------------------------------

main.cpp:

#include "MyCocoaBridge.h";

int main (int argc, char * const argv[])
{
// codes using disk arbitrartion framework(disk locking -unlocking)
getVersion();
return 0;
}

---------------------------------

MyCocoaBridge.h:

void getVersion(void);

---------------------------------

MyCocoaBridge.mm:

#import <Foundation/Foundation.h>
#include <iostream>

void getVersion(void)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *version = [[NSProcessInfo processInfo] operatingSystemVersionString];
std::cout << [version UTF8String] << "\n";
[pool drain];
return;
}
----------------------------------------------------------------
I have included the foundation framework but it is not able to identify it.....

It is showing the following errors :

/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:60: error: expected unqualified-id before '@' token
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:62: error: expected constructor, destructor, or type conversion before '*' token
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:63: error: 'NSString' was not declared in this scope
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:63: error: 'aSelectorName' was not declared in this scope
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:64: error: 'NSString' was not declared in this scope
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:64: error: 'aClassName' was not declared in this scope
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:65: error: expected constructor, destructor, or type conversion before '*' token
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:68: error: variable or field 'NSLog' declared void
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:68: error: 'NSString' was not declared in this scope
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:68: error: 'format' was not declared in this scope
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:68: error: expected primary-expression before '...' token
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:68: error: initializer expression list treated as compound expression
/Developer/SDKs/MacOSX10.4u.sdk/System/Library/Frameworks/Foundation.framework/Headers/NSObjCRuntime.h:69: error: variable or field 'NSLogv' declared void

and many more errors of this sort.


I fail to understand why it is not able to identify Founadation framework whereas it is happy to be with disk arbitrartion framework.

******Rouble******
QUOTE Thanks
roublesaha

 
Member Since: Oct 27, 2008
Posts: 13
roublesaha is on a distinguished road

roublesaha is offline
Hi
I have done some modifications in the settings and it is working wonders.

But tell me one thing....how should i return the value of the string in the cpp file.I mean what variable should i use to store the value returned by the function.

******Rouble******
QUOTE Thanks
mystic_fm

 
Member Since: Mar 15, 2007
Posts: 161
mystic_fm will become famous soon enough
Mac Specs: 17" MacBook Pro, 2.33GHz C2D, 2GB RAM

mystic_fm is offline
I'm glad you figured out the project settings. I'm running Xcode on 10.5, and perhaps should have asked you which version of Xcode you are running since you are on 10.4 - that might be why you had some additional headaches with the project settings.

NSString has a variety of functions for creating a C-style array of characters. Those should be easy enough to return and use in your C++ code.
QUOTE Thanks
roublesaha

 
Member Since: Oct 27, 2008
Posts: 13
roublesaha is on a distinguished road

roublesaha is offline
But can please u tell me how to do the following :

I want to compare the value returned by ( NSString *str = [[NSProcessInfo processInfo] operatingSystemVersionString] ) with 10.4

.If it is less than that execute one piece of code otherwise if it is greater then execute other piece of code.

******Rouble******
QUOTE Thanks
roublesaha

 
Member Since: Oct 27, 2008
Posts: 13
roublesaha is on a distinguished road

roublesaha is offline
Thanks guys for the help................especially mystic_fm
it is working fine now.........

cheers

******Rouble******
QUOTE Thanks

Post Reply New Thread Subscribe


« Apple Script lookup through email | Not able to enable build option in Xcode 1.5 »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Running Windows on a Mac: A Switcher's Guide cwa107 Running Windows (or anything else) on your Mac 283 03-13-2014 04:15 PM
Running Windows on Mac rickmahmoud Running Windows (or anything else) on your Mac 7 02-16-2008 11:31 PM
Mac Pro keeps crashing when using with KVM. where to start my investigation? AlexUK Apple Desktops 6 06-20-2007 05:50 AM
Mac Users Get More ISP Choices schweb Apple Rumors and Reports 1 03-06-2003 10:57 AM

All times are GMT -4. The time now is 11:56 PM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?