0

again I need help. I try to call SWI-Prolog via C based on a dll on windows.

In my C progam I want to use the function "consult_cooco". The C code looks like

#include "consult_cooco.h"
#include <SWI-Prolog.h>

int consult_cooco( char** strInput )
{
    char *program = "consult_cooco";
    char *plav[2];
    int rval;

    char* xmlstring;
    term_t av;
    predicate_t p;

    /* make the argument vector for Prolog */
    plav[0] = program;
    plav[1] = NULL;

    putenv("SWI_HOME_DIR=C:\\Program Files (x86)\\swipl");
    if ( !PL_initialise(1, plav ) )
        PL_halt(1);


    av = PL_new_term_refs(2);
    p = PL_predicate("start_dialog", 2, "user");

    printf("consult_cooco:: input >> %s\n",*strInput);

    // Prolog call: start_dialog('ohne Zitrone, mit Joghurt, Butter',XML).
    av = PL_new_term_refs(2);
    PL_put_atom_chars(av, *strInput);

    if ( PL_call_predicate(NULL, PL_Q_NORMAL, p, av) )
    { 
        rval = PL_get_atom_chars(av+1, &xmlstring);
        printf(" >> xmlstring of %s is \n\n%s (returned %i)\n",*strInput,xmlstring,rval);
    } 
    else
    {
        printf(" >> no answer found\n");
    }


    PL_halt(1);

    return 0;
}

I created the dll by

swipl-ld -shared -dll -o consult_cooco -goal true consult_cooco.c dialog.pl -DLIKES_EXPORTS -v

I linked C sources by

gcc CooCoServer.o -static-libgcc -lws2_32 -L. -lconsult_cooco -I. -o startcoocoserver

Begin of my Prolog file dialog.pl looks like

start_dialog( InputString, XMLResult ) :-
    create_individual_filename('dialog_','.debug',Filename),
    tell(Filename),
    process_input( InputString, WordList ),
    do(WordList, XMLResult ),
    told.

Without

putenv("SWI_HOME_DIR=C:\\Program Files (x86)\\swipl");

I got the error

[FATAL ERROR: Could not find system resources]

as described in SWI Prolog manual.

But still Prolog predicates can not be found, the error description is

ERROR: '$c_call_prolog'/0: Undefined procedure: start_dialog/2

start_dialog is defined in dialog.pl, see above.

My assumption is that plav[0] = "consult_cooco"; is wrong and no link to the Prolog file is found.

When using an exe file starting from a C main file by changing

int consult_cooco( char** strInput )
{
    char *program = "consult_cooco";

to

int main( int argc, char** argv )
{
    char *program = argv[0];

it works. It works also with

int main( int argc, char** argv )
{
    char *program = "call_cooco";

What do I wrong when using the dll? What must be put into char *program?

Would appreciate your help very much.

false
  • 10,264
  • 13
  • 101
  • 209
kiw
  • 155
  • 6

1 Answers1

0

Sorry for that, the answer is already given here:

I have to set

plav[0] = (char*)"startcoocoserver.exe";
plav[1] = (char*)"-x";
plav[2] = (char*)"dialog.exe";

and to use then

if(!PL_initialise(3, plav))

An additional building step is necessary

swipl --goal=true --stand_alone=true -o dialog -c dialog.pl

before doing the other steps. Then it works :)

PS: I did not found how to delete a question - admin, please do...

Community
  • 1
  • 1
kiw
  • 155
  • 6