// CarbonProcessManager.cpp

#include <iostream>
#import <Carbon/Carbon.h>
#import <Foundation/Foundation.h>
#define PROGNAME "cpmtest"
void c2pstrcpy(Str255 dst, const char* src)
{
    size_t len = std::min<size_t>(strlen(src), 255);
    dst[0] = len;
    memcpy(&dst[1], src, len);
}
int
main(int argc, char **argv)
{
    OSErr               err;
    Str255              path;
    FSRef               fsRef;
    LaunchParamBlockRec launchParams;

    if (argc != 2) {
        printf("usage: %s <full application path>\n", PROGNAME);
       // exit(1);
    }

    c2pstrcpy(path,"Macintosh HD:Applications:Chess.app:Contents:MacOs:Chess");
    NSString* pPath=[NSString stringWithUTF8String:"Macintosh HD:Applications:Thunder.app:Contents:MacOS:Thunder"];
    NSURL* fileURL=[NSURL URLWithString:[pPath stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    NSURL *URL1 = [NSURL fileURLWithPath:@"/Applications/Thunder.app/Contents/MacOS/Thunder" isDirectory:NO];
    err = CFURLGetFSRef((__bridge CFURLRef)URL1, &fsRef);
    if (err == noErr) {
        printf("failed to make FS spec for application (error %d).\n", err);
        exit(1);
    }

    // the extendedBlock constant specifies that we are using the fields that
    // follow this field in the structure
    launchParams.launchBlockID = extendedBlock;

    // length of the fields following this field (again, use a constant)
    launchParams.launchEPBLength = extendedBlockLen;

    // launch control flags
    // we want the existing program to continue, and not terminate
    // moreover, we want the function to determine the Finder flags itself
    launchParams.launchControlFlags = launchContinue + launchNoFileFlags;

    // FSSpec for the application to launch
    launchParams.launchAppRef = &fsRef;

    // no parameters
    launchParams.launchAppParameters = NULL;

    err = LaunchApplication(&launchParams);

    if (err != noErr) {
        printf("failed to launch application (error %d).\n", err);
        exit(1);
    }

    printf("main: launched application, PSN = %lu_%lu\n",
           launchParams.launchProcessSN.highLongOfPSN,
           launchParams.launchProcessSN.lowLongOfPSN);
    printf("main: continuing\n");

    exit(0);
}