APIs to determine OS in mac..PLz help

Joined
Oct 27, 2008
Messages
13
Reaction score
0
Points
1
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.
 
OP
R
Joined
Oct 27, 2008
Messages
13
Reaction score
0
Points
1
I have got this from help :-

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

But i am not able to use it properly.
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's Specs
17" MacBook Pro, 2.33GHz C2D, 2GB RAM
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.
 
OP
R
Joined
Oct 27, 2008
Messages
13
Reaction score
0
Points
1
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.
 
OP
R
Joined
Oct 27, 2008
Messages
13
Reaction score
0
Points
1
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.
 
Joined
Mar 15, 2007
Messages
161
Reaction score
4
Points
18
Your Mac's Specs
17" MacBook Pro, 2.33GHz C2D, 2GB RAM
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.
 
OP
R
Joined
Oct 27, 2008
Messages
13
Reaction score
0
Points
1
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.
 
OP
R
Joined
Oct 27, 2008
Messages
13
Reaction score
0
Points
1
Thanks guys for the help................especially mystic_fm
it is working fine now.........

cheers
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top