Results 1 to 9 of 9
  1. #1


    Member Since
    Oct 27, 2008
    Posts
    13
    APIs to determine OS in mac..PLz help
    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******

  2. #2


    Member Since
    Oct 27, 2008
    Posts
    13
    I have got this from help :-

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

    But i am not able to use it properly.
    ******Rouble******

  3. #3


    Member Since
    Oct 27, 2008
    Posts
    13
    I am working with C++ source code
    ******Rouble******

  4. #4


    Member Since
    Mar 15, 2007
    Posts
    161
    Specs:
    17" MacBook Pro, 2.33GHz C2D, 2GB RAM
    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.

  5. #5


    Member Since
    Oct 27, 2008
    Posts
    13
    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******

  6. #6


    Member Since
    Oct 27, 2008
    Posts
    13
    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******

  7. #7


    Member Since
    Mar 15, 2007
    Posts
    161
    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.

  8. #8


    Member Since
    Oct 27, 2008
    Posts
    13
    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******

  9. #9


    Member Since
    Oct 27, 2008
    Posts
    13
    Thanks guys for the help................especially mystic_fm
    it is working fine now.........

    cheers
    ******Rouble******

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Accessing font FOND resource without using deprecated APIs
    By birsingh in forum OS X - Operating System
    Replies: 0
    Last Post: 05-12-2015, 02:04 AM
  2. Replies: 8
    Last Post: 04-25-2014, 08:21 AM
  3. iOS 5 includes new APIs for face detection
    By OneMoreThing... in forum Apple Rumors and Reports
    Replies: 0
    Last Post: 07-26-2011, 10:35 AM
  4. Determine Brand From Mac Address?
    By phinsup in forum Other Hardware and Peripherals
    Replies: 2
    Last Post: 02-22-2009, 11:51 PM
  5. APIs to determine OS in mac..PLz help
    By roublesaha in forum OS X - Operating System
    Replies: 2
    Last Post: 12-22-2008, 11:26 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •