C++ Integration

Creating an extension with C/C++

or any language capable of creating Standard Shared Libraries

In NT these are commonly called .DLL’s (Dynamic Link Libraries) under UNIX environments .SO’s (Shared Libraries).

 

Layout of this Document:

Sample 1 – An MFC DLL in NT

Sample 2 – A Non-MFC DLL in NT

Sample 3 – A C/C++ Shared Lib in Sun/Linux
Calling the extension from TransactXML Server
Calling the extension from the XSLT engine

=======  Sample 1 – An MFC DLL in NT =======

c-cpp001c-cpp003

  1. Add the following source code to MyDll.cpp that was generated for you by AppWizard.

// ================================================================

typedef void (*appendFunction)(void *pStream, const char *string);

extern “C” const char *ExposedMethods()

{

static char pzInterface[] =

“SayHello&&var1Name&&char *&&&&”

“StringCat&&String1&&char *&&String2&&char *&&&&”;

return pzInterface;

}

 

extern “C” void SayHello(void *pStream,     appendFunction StreamBack,                const char *var1Name)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

StreamBack(pStream, “[“);
StreamBack(pStream, var1Name);
StreamBack(pStream, “]:Hello C++”);
}

extern “C” void StringConcat(void *pStream,   appendFunction StreamBack,    const char *String1,      const char *String2)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState());

StreamBack(pStream, String1);
StreamBack(pStream, “:”);
StreamBack(pStream, String2);
}
// ================================================================

  1. Add the following source code to the EXPORTS section of your .DEF file created for you by App Wizard.

ExposedMethods

SayHello

StringConcat

 

3. Set the [CStdCall] section in TXML.TXT like this

[CStdCall]
Path=C:\MySharedLibs

4. Compile the DLL and move it to the location configured in step 3

Copy MyDLL.dll c:\MySharedLibs

=======  Sample 2 – A non-MFC DLL in NT =======

c-cpp005

c-cpp007

  1. Add the following source code to MyDll.cpp that was generated for you by AppWizard.

// ================================================================

typedef void (*appendFunction)(void *pStream, const char *string);

extern “C” MYCPPDLL_API char *ExposedMethods()

{

static char pzInterface[] =

// See Example 1, for adding multiple entry points

“SayHello&&varName&&char *&&&&”;

return pzInterface;

}

 

extern “C” MYCPPDLL_API void SayHello(void *pStream,     appendFunction StreamBack,      const char *varName)

{

StreamBack(pStream, “[“);
StreamBack(pStream, varName);
StreamBack(pStream, “]:Hello C++”);
}

// ================================================================

  1. Set the [CStdCall] section in TXML.TXT like this

[CStdCall]
Path=C:\MySharedLibs

3. Compile the DLL and move it to the location configured in step 2

Copy MyCppDLL.dll c:\MySharedLibs

 

 

=======  Sample 3 – A C/C++ Shared Lib in Sun/Linux  =======

  1. Create a source file. For this example we’ll call it Sample.cpp

// ================================================================

#include< string.h>

typedef void (*appendFunction)(void *pStream, const char *string);

extern “C” char *ExposedMethods()

{

static char pzInterface[] =

// See Example 1, for adding multiple entry points

“SayHello&&varName&&char *&&&&”;

return pzInterface;

}

extern “C” char *SayHello(void *pStream,    appendFunction StreamBack,const char *varName)

{

StreamBack(pStream, “[“);
StreamBack(pStream, varName);
StreamBack(pStream, “]:Hello C++”);
}

// ================================================================

  1. Create a make file.

CC=g++                  LINUX-GNU

CPPFLAGS = -O3 -w -c

INCDIR = -I.

DB2Driver: Sample.o

g++ -shared -o MySample.so Sample.o -lm -ldl -lnsl

Sample.o: ./Sample.cpp

$(CC) $(CPPFLAGS) $(INCDIR) ./Sample.cpp

 

 

CC=g++                  SUN-GNU

CPPFLAGS = -O3 -w -c

INCDIR = -I../../../Library/inc

MySample: Sample.o

g++ -G -o MySample.so Sample.o -lm -ldl -lnsl

$(COPY) MySample.so /MySharedLibs/.

Sample.o: ./Sample.cpp

$(CC) $(CPPFLAGS) $(INCDIR) ./Sample.cpp

 

  1. Set the [CStdCall] section in TXML.TXT like this

[CStdCall]
Path=\MySharedLibs

4. Compile the DLL and move it to the location configured in step 2

copy MySample.SO \MySharedLibs

 

=== Calling the extension from TXML Server ===

1. From DesignerXML, double click an element to get the following dialog:

Java-E1

On the Markup page of the Properties dialog shown above, change the “Element Value Source” from “No value” to “Custom extension”.  This causes the dialog below to appear. The Dialog will be empty by default. You should set the following values to call the example built above:

C-CPP-3

 

This causes the Element “MyElement” to get it’s value from the external C/C++ function.  Double click the parameter to set it’s value.

===  Calling the extension from XSLT Engine ===

XSLT Engine Syntax:

functoid:LANGUAGE%COMPONENT%INTERFACE%METHOD()

To call the example above use the following syntax:

functoid:CStdCall%MyDLL.dll%MyDLL%SayHello()

Here’s a complete example to transform this XML:

<customers at=’yes’>

<name>Chris</name>

<name>Sean</name>

<name>Brian</name>

<name>Lad</name>

</customers>

to this HTML table:

Python2

 by processing this XSL:

<xsl:stylesheet version=’1.0′

xmlns:xsl=”http://www.w3.org/1999/XSL/Transform&#8221;

xmlns:functoid=”functoid”>

<xsl:template match=”customers”>

<table width=”20%” border=”1″>

<tbody>

<xsl:for-each select=”name”>

<!–Sort the table by customer name–>

<xsl:sort/>

<tr>

<th>

<xsl:value-of select= “functoid:CStdCall%MyDLL.dll%MyDLL%SayHello(.)”/>

</th>

</tr>

</xsl:for-each>

</tbody>

</table>

</xsl:template>

</xsl:stylesheet>

© 2015 United Business Technologies, Inc. All Rights Reserved.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s