Click here to Skip to main content
15,888,202 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to write a DYLIB, (in Code::Blocks), which is to be called from a Mac application, that simply calls the standard C system() function, while giving the option of whether to wait for finishing, in the second argument, (1 or 0).

C++
#import <pthread.h>
#import <stdlib.h>
#include <unistd.h>

extern "C"
{
    char *tfname;

    void *system_async(void *arg)
    {
        system(tfname);

        return NULL;
    }

    double ExecuteShell(char *fname,double wait)
    {
        pthread_t tID;
        tfname = fname;

        if (wait>=1)
        {
            system(fname);
        }
        else
        {
            pthread_create(&tID,NULL,&system_async,NULL);
            usleep(5000);
        }

        return 0;
    }
}

Now when calling the DYLIB, (I can't remember the compiler I used, either GCC or LLVM), I use this code within GameMaker Studio:

ExecuteShell('open /Applications/Calculator.app',0);

However, calculator does not run. Trying to change the second argument to 1, and I get the same result. Calculator does not run. No errors. It seems to be ignoring my code all together, so I was wondering if there was something wrong with how I wrote my DYLIB.

I look at this question someone else had: link.

string command = "open " + filePath;
system(command.c_str());

I see they used .c_str(). Should I be using .c_str()? Is that the problem? The code I use right now is system(fname);. Should I change that to system(fname.c_str());? Is it OK that I'm using the type char * for the argument fname? Or because I'm using that type, do I have to cast/convert it to the type string?

I'm using someone else's Mac to port this small project of mine to work on Mac, anyway I'm trying to get help on here, before trying to build it again, because I'm trying to minimize how much I'm asking to use my friend's Mac computer. The hope is, the next build I do, with very little tweak, will finally work and run calculator.

Thanks in advance!

What I have tried:

#import <pthread.h>
#import <stdlib.h>
#include <unistd.h>

extern "C"
{
    char *tfname;

    void *system_async(void *arg)
    {
        system(tfname);

        return NULL;
    }

    double ExecuteShell(char *fname,double wait)
    {
        pthread_t tID;
        tfname = fname;

        if (wait>=1)
        {
            system(fname);
        }
        else
        {
            pthread_create(&tID,NULL,&system_async,NULL);
            usleep(5000);
        }

        return 0;
    }
}
Posted
Updated 14-Mar-17 22:33pm
v2

1 solution

You had the right solution in your bag:

C++
string command = "open " + filePath;
system(command.c_str());

should be the right thing.

You must supply a correct input parameter for the system call. In the end every pointer is only a memory area. And such memory have to represent the correct data in the correct data type for the function. And there is a different between an Unicode and ANSI string. (Also to UTF-8). Watch it in some memory view.

Read about c_str() function to understand that it returns a char*.

If you like coding get a Mac. The cheapest way is a Mac mini. Cheaper when used. ;-)
 
Share this answer
 
Comments
time-killer-games 17-Mar-17 22:52pm    
Thank you! I had a feeling that was all it was I needed. Now that I have that confirmed I'll update the build. Thanks for everything!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900