Sample Cross-Platform External Test Function
The following section describes and includes the C source code for a sample cross-platform test function: LogMsg. It is a basic function that opens a log file and appends a line to it. If you compile the code using a C++ compiler, the functions must be declared using external C to ensure C-language linkage.
The sample program also contains an interface function (LogMsg_intf). Prior to PeopleTools 8.52, this interface function was required for all non-Microsoft Windows environments. The interface function references a provided header file, pcmext.h. The interface function is passed type codes that can be optionally used for parameter checking.
The interface function is required only for the Solaris x86_64 environment.
For all the other PeopleTools-supported UNIX platforms, the functions from the UNIX shared libraries can be used directly. To maintain backward compatibility, the interface functions are also supported on these UNIX platforms.
In the following sample test program, the interface function is compiled only when compiling for the specified non-Windows environments.
/*
* Simple test function for calling from PeopleCode.
* This is passed two strings, a file name and a message.
* It creates the specified file and writes the message
* to it.
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#ifdef _WINDOWS
#define DLLEXPORT __declspec(dllexport)
#define LINKAGE __stdcall
#else
#define DLLEXPORT
#define LINKAGE
#endif
DLLEXPORT int LINKAGE LogMsg(char * fname, char * msg);
/********************************************************
* PeopleCode External call test function. *
* *
* Parameters are two strings (filename and message) *
* Result is 0 if error, 1 if OK *
* *
* *
* To call this function, the following PeopleCode is *
* used *
* *
* Declare Function LogMsg Library "testdll" *
* (string, string) *
* Returns integer; *
* *
* &res = LogMsg("\temp\test.log", "This is a test"); *
* *
********************************************************/
DLLEXPORT int LINKAGE LogMsg(char * fname, char * msg)
{
FILE *fp;
fp = fopen(fname, "a"); /* append */
if (fp == NULL) return 0;
fprintf(fp, "%s\n", msg);
fclose(fp);
return 1;
}
#ifndef _WINDOWS
/********************************************************
* Interface function. *
* *
* This is not needed for Windows.... *
* *
********************************************************/
#include "pcmext.h"
#include "assert.h"
/* This interface function is required only for the following platforms:
- HP-UX Itanium
- Solaris x86_64
- z/Linux
*/
void LogMsg_intf(int nParam, void ** ppParams, EXTPARAMDESC * pDesc)
{
int rc;
/* Some error checking */
assert(nParam == 2);
assert(pDesc[0].eExtType == EXTTYPE_STRING
&& pDesc[1].eExtType == EXTTYPE_STRING
&& pDesc[2].eExtType == EXTTYPE_INT);
rc = LogMsg((char *)ppParams[0],
(char *)ppParams[1]);
*(int *)ppParams[2] = rc;
}
#endif