Creating a DLL to Call a Function in an External DLL
The following procedure illustrates how to create a DLL to use the EAI DLL Transport business service to call a function in an external DLL.
Two mechanisms are provided for creating a DLL to call a function in an external DLL. With the preferred mechanism, the creator of the external DLL can expose additional API functions to free memory. Two business service method arguments, DLLExternalFunction and DLLExternalFunctionFreeMemory, are available as optional input arguments to the Send and SendReceive methods.
When you use the preferred mechanism for memory deallocation, you must use both of these arguments together: DLLExternalFunction and DLLExternalFunctionFreeMemory.
Optionally, customers can still expose only the argument ExternalFunction, instead of exposing the memory freeing API functions. If you use ExternalFunction, then memory deallocation is done differently, and failure might occur when the EAI DLL Transport business service performs the memory deallocation.
The signature for the memory freeing function would resemble the following:
extern "C" int __declspec(dllexport) TestFree(void* Value)
To create a DLL
Open a VC++ project by choosing the Open menu, then New.
Select a Win32 Dynamic Link Library and give a name to the project, such as MyDLL.
In the next dialog box, select the option Simple dll project.
The following files are created by default:
MyDLL.cpp
StdAfx.h
StdAfx.cpp
Make the following changes in the StdAfx.h and Main.cpp files and check the results in the process simulator:
// MyDLL.cpp : Defines the exported functions for the DLL application. // #include "stdafx.h" #include <string.h> #include <stdio.h> #include <io.h> #include <malloc.h> extern "C" int __declspec(dllexport) TestEAI(const XMLDataBuf* pValue, XMLDataBuf* Value) { FILE *fp = NULL; int retf = 0; int rc = 0; if ((fp = fopen("testeai.txt", "wb")) != NULL) { fprintf(fp, "Before test"); fwrite(pValue->pData, sizeof(char), (size_t)pValue->nLength, fp); fprintf(fp, " After Test"); fclose(fp); } else return -1; if ((fp = fopen("testeai.txt", "rb")) != NULL) { rc = (int)_filelength(_fileno(fp)); Value->pData = (void *)malloc((size_t)(rc + 1)); rc = (int)fread(Value->pData, sizeof(char), (size_t)rc, fp); fclose(fp); Value->nLength = rc; ((char*)Value->pData)[rc] = (char)NULL; } else return -2; return rc; } extern "C" int __declspec(dllexport) TestFree(void* Value) { if(Value != NULL) { free (Value); Value = NULL; } return 0; }