IPC Mechanism to communicate between C++ & Objective C

I’m developing a Mac Application in which i need to implement IPC Mechanism. The Scenario is this :

My Application contains two executables, one is Native Mac App(NSStatusItem app) and other is a terminal app coded on CPP. I want to establish IPC Communication between these two processes. I want to be able to send and recieve messages both from CPP to Objective C and vice versa.

  • Some questions regarding 2d Vectors, C++
  • rand() broken on OS X Mavericks/XCode 5.1? (or, “is this a massive security hole, or am I just a bad programmer??”)
  • How to get return values from pure C function to swift?
  • how to get the current gps coordinates in xamarin
  • Xcode does not find C++ static library
  • How to determine ARMv8 features at runtime on iOS?
  • Which IPC Mechanism would better suit this ?

    Also this wiki(http://en.wikipedia.org/wiki/Inter-process_communication#Main_IPC_methods) Shows, IPC Named Pipes are supported in POSIX and Windows. I wanted to clarify that if i’m using Named Pipes(i know its unidirectional), is it supported in Mac and Objective C ..?

    [P.S : If Possible, Please provide example code or links to IPC in C++ & Objective C).

    2 Solutions Collect From Internet About “IPC Mechanism to communicate between C++ & Objective C”

    Unix domain sockets are Great for this. http://www.cs.cf.ac.uk/Dave/C/node28.html

    If you targeting Mac OS X 10.7 and higher you can use XPC using a Mach service connection for your IPC.

    On your server you create the Mach service, set an event handler that accepts new connections and resume the connection:

    xpc_connection_t conn = xpc_connection_create_mach_service( "com.yourname.product.service", dispatch_get_main_queue(), XPC_CONNECTION_MACH_SERVICE_LISTENER );
    xpc_connection_set_event_handler( conn, ^( xpc_object_t client ) {
        xpc_connection_set_event_handler( client, ^(xpc_object_t object) {
            NSLog( @"received message: %s", xpc_copy_description( object ) );
            xpc_object_t reply = xpc_dictionary_create_reply( object );
            xpc_dictionary_set_string( reply, "reply", "Back from the service" );
            xpc_connection_t remote = xpc_dictionary_get_remote_connection( object );
            xpc_connection_send_message( remote, reply );
        } );
        xpc_connection_resume( client );
    }) ;
    xpc_connection_resume( conn );

    I’m assuming that this is running in your Cocoa app which has an event loop. If there is no event loop you need to make sure there is one running (NSRunloop, dispatch_main(), …)

    In your client you also create an Mach service connection without the XPC_CONNECTION_MACH_SERVICE_LISTENER flag, set an event handler and then resume it. After that you can send messages to your server and receive it’s answers:

    xpc_connection_t conn = xpc_connection_create_mach_service( "com.yourname.product.service", NULL, 0 );
    xpc_connection_set_event_handler( conn, ^(xpc_object_t object) {
        NSLog( @"client received event: %s", xpc_copy_description( object ) );
    xpc_connection_resume( conn );
    xpc_object_t message = xpc_dictionary_create( NULL, NULL, 0 );
    xpc_dictionary_set_string( message, "message", "hello world" );
    xpc_connection_send_message_with_reply( conn, message, dispatch_get_main_queue(), ^(xpc_object_t object) {
        NSLog( @"received reply from service: %s", xpc_copy_description( object ));

    Note that for this to work your client (probably the command line tool in your case) needs to run an event loop too for this to work. In my example that’s the dispatch_main(). This may seem inconvenient at first but it’s necessary and well worth it.

    Also note that my example code misses all error handling which really is necessary.

    The XPC APIs are plain C and thus are useable from C, C++ and Objective-C. You only need to use a compiler that supports blocks.