You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-user@axis.apache.org by "Gold, Jack L (US SSA)" <ja...@baesystems.com> on 2008/09/09 22:05:55 UTC

FAQ: Axis2/C with C++

I was reading the FAQ and the response to "How can I use Axis2/C with

C++?" doesn't seem to have an answer I can work with.

 

If I wrote my code in C++, then how would I have 'C' source files and
'C' code to wrap in an extern?  Am I missing something here?  I think
this FAQ answer needs a bit more detail for someone unfamiliar with 'C'

externs.

 

Jack Gold

 


RE: Axis2/C with C++

Posted by Dann Corbit <DC...@connx.com>.
 

From: Gold, Jack L (US SSA) [mailto:jack.gold@baesystems.com] 
Sent: Tuesday, September 09, 2008 1:06 PM
To: axis-c-user@ws.apache.org
Subject: FAQ: Axis2/C with C++

 

I was reading the FAQ and the response to "How can I use Axis2/C with

C++?" doesn't seem to have an answer I can work with.

 

If I wrote my code in C++, then how would I have 'C' source files and
'C' code to wrap in an extern?  Am I missing something here?  I think
this FAQ answer needs a bit more detail for someone unfamiliar with 'C'

externs.

 

For any Axis2/c routine you want to call from C++, create a header file
like this:

extern "C" {

/* Prototypes of the Axis2/C functions you want to reference go here. */

}

...

/* Include the header and use the functions in your code now... */

 

If you want the Axis2/C routines to access your C++ code, then in your
C++ file, create a function interface with extern "C" in your C++ file
definitions and publish this interface in an include file.

The techniques are general and are the same whenever you want to use
e.g.:

Your C++ file:

#include <string>

using namespace std;

class connectionObject

    {

 private:

    string user_;

    string password_;

    string server_;

    int port_;

 public:

    connectionObject()

        {

        user_ = "";

        password_ = "";

        server_ = "";

        port_ = 0;

        }

    connectionObject(char *user, char *password, char *server, int port
= 7513)

        {

        setUser(user);

        setPassword(password);

        setServer(server);

        setPort(port);

        }

    connectionObject(string user, string password, string server, int
port = 7513)

        {

        setUser(user);

        setPassword(password);

        setServer(server);

        setPort(port);

        }

    void setUser(char *user)

        {

        user_ = user;

        }

    void setUser(string user)

        {

        user_ = user;

        }

    void setPassword(char *password)

        {

        password_ = password;

        }

    void setPassword(string password)

        {

        password_ = password;

        }

    void setServer(char *server)

        {

        server_ = server;

        }

    void setServer(string server)

        {

        server_ = server;

        }

    void setPort(int port)

        {

        port_ = port;

        }

    void setPort(short port)

        {

        port_ = port;

        }

    };

 

extern "C" void *make_connection(char *user, char *password, char
*server, int port)

    {

    // Supposing you want to use your connection object - construct one
from user data and return its address:

    connectionObject *connection = new connectionObject(user, password,
server, port);

    return (void *) connection; // abstract handle passed to C program,
used in subsequent API calls.

    }

 

Header file header.h:

void *make_connection(char *user, char *password, char *server, int
port);

 

C file:

#include <stdlib.h>

#include <stdio.h>

#include "header.h"

 

int main(void)

{

     void *conn = make_connection("JasonV", "Ca$h4me&!4u",
"www.foobird.com", 5432);

     if (conn == NULL)

     {

           puts("Failed to get connection object");

        exit(EXIT_FAILURE);

     }

     else

     {

           puts("Connection OK...");

           /* do important stuff here. */

     }

     return 0;

}