c ++中的进程之间的通信(macos)

时间:2017-06-29 07:52:10

标签: c++ objective-c webview process fork

我想知道它是否可能以某种方式在两个进程之间建立通信。例如,我有一个父进程调用子进程,这是一个带webview的窗口。我正在使用 execl 来启动webview。我想从webview下载文件时,要将文件路径发送回父进程。这是可能的,我应该用它来实现它吗?

这是我现在的代码:

void openWebView(void){
    WriteReport("Open web view");
#ifdef __OBJC__
    WriteReport("it is OBJC!");

    pid_t my_pid, parent_pid, child_pid;
    int status;

    /* get and print my pid and my parent's pid. */

    my_pid = getpid();    parent_pid = getppid();
    printf("\n Parent: my pid is %d\n\n", my_pid);
    printf("Parent: my parent's pid is %d\n\n", parent_pid);

    /* print error message if fork() fails */
    if((child_pid = fork()) < 0 )
    {
        perror("fork failure");
        exit(1);
    }

    /* fork() == 0 for child process */

    if(child_pid == 0)
    {  WriteReport("\nChild: I am a new-born process!\n\n");
        my_pid = getpid();
        parent_pid = getppid();
               execl("/Users/ivan/Library/Developer/Xcode/DerivedData/webView-bncefxvsqlnnjrfhvxylipyqaryp/Build/Products/Debug/webView", "webView", 0, 0);
        perror("execl() failure!\n\n");

        _exit(1);
    }
    /*
     * parent process
     */
    else
    {
        system("ps -acefl | grep ercal");
        printf("\n \n");
        wait(&status); /* can use wait(NULL) since exit status
                        from child is not used. */
        WriteReport("\n Parent: my child is dead. I am going to leave.\n \n ");
    }


#endif
}

这是我在Obj-C中的webview代码:

-(void)applicationDidFinishLaunching:(NSNotification *)a_notification
{
    WKWebViewConfiguration *theConfiguration =
    [[WKWebViewConfiguration alloc] init];

    WKWebView *wb = [[WKWebView alloc] initWithFrame:self._window.contentView.frame configuration:theConfiguration];
    wb.autoresizingMask = NSViewWidthSizable | NSViewHeightSizable;

    NSURL *url = [NSURL URLWithString: @"https://website.bg"];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];

    [wb loadRequest:request];
    wb.frame = CGRectMake(self._window.contentView.frame.origin.x,self._window.contentView.frame.origin.y, self._window.contentView.frame.size.width, self._window.contentView.frame.size.height);

    [self._window.contentView addSubview:wb];
}

@end

void start_program(void)
{
    NSApplication *app = [NSApplication sharedApplication];
    // Critical to have this so that you can add menus
    if([[NSUserDefaults standardUserDefaults] boolForKey:@"HideDockIcon"])
        [NSApp setActivationPolicy:NSApplicationActivationPolicyAccessory];

    NSWindow *window =
    [[NSWindow alloc]
     initWithContentRect:NSMakeRect(0, 0, 800, 600) styleMask: NSWindowStyleMaskClosable backing: NSBackingStoreBuffered defer: YES];

    [window center];
    [window makeKeyAndOrderFront:window];
    [window setStyleMask:[window styleMask] | NSWindowStyleMaskTitled | NSWindowStyleMaskResizable];

    Window_delegate *d = [Window_delegate new];
    window.delegate = d;

    App_delegate *application_delegate = [[App_delegate alloc] init:window];
    app.delegate = application_delegate;
    [app activateIgnoringOtherApps:YES];
    [app run];
}

int main(int argc, char **argv)
{

    @autoreleasepool {
        start_program();
    }
}

我非常感谢有关我的问题的指导方面的任何帮助。 儒略

0 个答案:

没有答案