Calling external application from C code

Discussion in 'Mac Programming' started by nuhriko, Sep 7, 2015.

  1. nuhriko macrumors newbie

    Joined:
    Sep 7, 2015
    Location:
    Japan
    #1
    So I have a C code that I am trying to translate from windows into mac. I am trying to call LTspice application using the sample syntax below.

    For windows, it is originally:
    char *strPath ="\"C:/Program Files/LTC/LTspiceIV/scad3.exe\" -b 111219_10.net ";

    Changed it to this for Mac:
    char *strPath ="\"/Applications/LTspice.app\" -b 111219_10.net ";

    And then I get this error:
    sh: /Applications/LTspice.app: is a directory

    I've been googling everywhere and I couldn't find an appropriate solution for this one. Can somebody help me fix this? Thanks in advance!

    Kind regards,
    Bern
     
  2. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #2
    Don't have a system handy to test but try

    char *strPath ="open \"/Applications/LTspice.app\" -b 111219_10.net ";
     
  3. nuhriko thread starter macrumors newbie

    Joined:
    Sep 7, 2015
    Location:
    Japan
    #3
    Thanks for your response. I tried as you say but I'm not really sure if the initial problem has been solved because I then get the error below:

    LSGetApplicationForInfo() failed with error -10814 while trying to determine the application with bundle identifier {filename}.net.

    When I searched, what I understood is that this happens when Mac OSX doesn't know the default program to open this kind of file. However, I already set LTspice as default program for all .net files but this error still occur. Is this relevant or should I post a new thread about this?

    Thank you very much for your help.

    Kind regards,
    Bern
     
  4. Jeffrey Lim macrumors newbie

    Joined:
    Sep 9, 2003
    #4
    What function are you using to execute the program?

    system?
    popen?

    Can you include a little more of your source code to indicate that bit.
     
  5. nuhriko thread starter macrumors newbie

    Joined:
    Sep 7, 2015
    Location:
    Japan
    #5
    Hmm, I hope this is more complete. System function is executed for this.

    else if( (*x) ==1 ){
    char *strPath ="open \"/Applications/LTspice.app\" -b 111219_10.net ";
    system( strPath);}

    Thanks a lot.
     
  6. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #6
    It's because LTspice.app is a directory, these are known as app bundles. The executable file is inside the bundle. Normally this would be found in /Applications/LTspice.app/Contents/MacOS/
     
  7. nuhriko, Sep 8, 2015
    Last edited by a moderator: Sep 9, 2015

    nuhriko thread starter macrumors newbie

    Joined:
    Sep 7, 2015
    Location:
    Japan
    #7
    Do you mean that I should modify the segment as:
    Code:
    else if( (*x) ==1 ){
    char *strPath ="open \"/Applications/LTspice.app/Contents/MacOS\" -b 111219_10.net ";
    system( strPath);}
    
    I did this, however, the error this time says that:
    The file /Applications/LTspice.app/Contents/MacOS does not exist.

    It is possibly linked on how LTspice was installed. From a virtual drive, I just copied the application file to my mac's application folder, pasted it there. Opening LTspice alone is alright, but for some reason the C program codes cannot open it.

    Another thing. I also tried changing "-b" to "-a", like
    Code:
    else if( (*x) ==1 ){
    char *strPath ="open \"/Applications/LTspice.app\" -a 111219_10.net ";
    system( strPath);}
    
    However, I also get the error:
    sh: /Applications/LTspice.app: is a directory.
     
  8. subsonix, Sep 8, 2015
    Last edited: Sep 8, 2015

    subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #8
    You have to locate the executable file inside the .app bundle, in your Windows example it was called scad3 no? I have no idea what the significanse of the -a or -b is, these are options to the executable file and has nothing to do with where it is located.
     
  9. nuhriko thread starter macrumors newbie

    Joined:
    Sep 7, 2015
    Location:
    Japan
    #9
    You are correct, for Windows it is scad3.exe.

    Thank you so much for your clarification. Problem solved! :)
     
  10. nuhriko thread starter macrumors newbie

    Joined:
    Sep 7, 2015
    Location:
    Japan
  11. lloyddean macrumors 6502a

    Joined:
    May 10, 2009
    Location:
    Des Moines, WA
    #11
    I would like you to try

    char *strPath ="open -a \"/Applications/LTspice.app\" -b 111219_10.net ";

    Instead of your current solution as I believe it will more properly set up the application runtime for the Mac GUI needs.

    If it doesn't work go back your current solution.
     
  12. subsonix macrumors 68040

    Joined:
    Feb 2, 2008
    #12
    Open is redundant in this case since the link points to the executable file, system() hands over the execution to the shell and in this case there are arguments involved. Secondly, we don't know if scad3 is a GUI application. So this should do:

    Code:
    system("/Applications/LTspice.app/Contents/MacOS/scad3 -b 111219_10.net");
     

Share This Page