View RSS Feed

Development Team Blog

How to make your own ELF

Rate this Entry
In this blog, i'll show you how to make a function number2words for use in Visual Report Writer. We are creating a dynamic link library (DLL) containing useful routines that can be used by Visual Report Writer. Using External Library Functions (ELF) is a great way to extend the functionality of Visual Report Writer.

This walkthrough uses C and assumes you understand the fundamentals of the C language and Microsoft Visual Studio 2008.

This blog covers the following:
· Creating a new dynamic link library (DLL) project
· Adding functions to the dynamic link library
· Creating an ELF in Visual Report Writer that references the dynamic link library
· Using the functionality in a report

Creating a new dynamic link library (DLL) project.
  1. Start Microsoft Visual Studio 2008
  2. From the File menu, select New and then Project….
  3. From the Project types pane, under Visual C++, select MFC.
  4. From the Templates pane, select MFC DLL.
  5. Choose a name for the project, such as ExtendedLibraryFunctions, and enter it in the Name field.
  6. Press OK to start the MFC DLL wizard. From the Overview page of the MFC DLL wizard dialog, press Next.
  7. From the Application Settings page of the Win32 Application Wizard, under Application type, select Regular DLL with MFC statically linked.
  8. Press Finish to create the project.
  9. Under the Project menu click on the properties item
  10. Select as configuration All Configurations and set the next options

  • General
o Use of MFC = Use Standard Windows Libraries
o Character Set = Use Multi-Byte Character Set
  • C/C++ ->Advanced
o Calling convention = _stdcall
  1. Click on OK to accept the settings
Visual Report Writer uses the standard calling convention (_stdcall). If you are planning to use another development environment make sure that you use this convention.

Adding functions to the dynamic link library
Now we are ready to add functions that will be exposed by our library to extend the functionality of Visual Report Writer.

Implement the functionality in the ExtendedLibraryFunctions.cpp source file. The code should resemble the following:

Code:
#include "stdafx.h"

//helper function for number2words
//build up the words 
char *wordbuild ( char *n, int ncomm )
{
int i, nleft, len; char *p = NULL; static char ret[1024]; /* Word lists */ static char *ones[] = {"one ","two ","three ","four ","five ","six ","seven ","eight ","nine "}; static char *tens[] = {"ten ","eleven ","twelve ","thirteen ","fourteen ","fifteen ","sixteen ","seventeen ","eighteen ","nineteen "}; static char *twenties[] = {"","twenty ","thirty ","forty ","fifty ","sixty ","seventy ","eighty ","ninety "}; static char *hundreds[] = {"hundred ","thousand ","million ","Billion ","Trillion "}; memset ( ret, '\0', 1024 ); len = strlen ( n ); for ( i = 0; i < len; i++ ) {
if (( p = strchr (( n[i] == ',' ) ? &n[++i] : &n[i], ',' )) == NULL ) p = &n[strlen ( n )]; if ( n[i] == '0' ) {
if (i == 0) strcat ( ret, "zero" ); continue;
} if ( ( nleft = ( p - &n[i] ) ) != 0 ) {
if ( nleft == 3 ) {
strcat ( ret, ones[n[i] - '0' - 1] ); strcat ( ret, hundreds[0] );
}
else if ( nleft == 2 ) {
if ( n[i] == '1' ) {
strcat ( ret, tens[n[++i] - '0'] ); nleft--;
} else {
strcat ( ret, twenties[n[i] - '0' - 1] );
}
} else {
strcat ( ret, ones[n[i] - '0' - 1] );
}
} if ( nleft == 1 && ncomm != 0 ) {
strcat ( ret, hundreds[ncomm--] );
}
} return ret;
} //helper function for number2words //determine the number of commas char *commaize ( unsigned long n, int *ncomm ) {
static char ret[30]; int i = 0; char *p = &ret[sizeof(ret)-1]; *p = '\0'; *ncomm = 0; do {
if ( i % 3 == 0 && i != 0 ) {
*--p = ','; ++*ncomm;
}
*--p = (char)( '0' + n % 10 ); n /= 10; i++;
} while ( n != 0 ); return p;
} //this function will be exposed //ELF functions must use parameters to be passed as type VARIANT VARIANT __declspec(dllexport) number2words(VARIANT n) {
CComVariant vRes; int i; char *p, *ret; USES_CONVERSION; // Check argument type if (n.vt != VT_R8) //VT_R8 = double {
vRes.vt = VT_ERROR; vRes.scode = MAKE_HRESULT(1,FACILITY_ITF,1); return vRes;
} p = commaize( n.dblVal, &i ); ret = wordbuild ( p, i ); // Return the words vRes = (LPCSTR)ret; return vRes;
}
Open the ExtendedLibraryFunctions.def file to define the function to be exported. The Library definition will be something like :
Code:
LIBRARY "ExternalLibraryFunctions". 
EXPORTS
Add the functions that you want to expose.

Code:
EXPORTS
number2words
Now the library is finished and can be build. To build the library click on the menu build an select build solution.

Creating an ELF in Visual Report Writer that references the dynamic link library
Now that we have a library we want to use it in our report. First copy the dll file that we have created to the folder where the OCX of Visual Report Writer is installed.
  1. Open Visual Report Writer and open a report where you want to use the function in.
  2. Add a new function object to the report
  3. In the function editor click on the button add function Attachment 6648
  4. In the Name field you can give the name of the function that will be used in Visual Report Writer, it doesn’t have to be the same name as the function in the library.
  5. Select the library you just copied
  6. Select the function in the dropdown box
  7. Provide the number of parameters that the function has.
Attachment 6649
By clicking on Ok will add our function to the available function list under External library functions

Using the functionality from the class library in the report
Now we can use the function in our report.
By clicking on the function name the function will be inserted into the editor and can be used as all the other integrated functions like:

Code:
return number2words({Customer.Purchases})
Happy coding

Bénar Bloemendal

Comments

  1. ge_nildo's Avatar
    I've a question. How long are we waiting for this resource in visual dataflex? For many times, I needed to create DLL, but, visual dataflex does not have this resource. I think, we need to avoid refering others languages to create something that could be created in visual Dataflex! Off course, if Data Access Developers team, work for it! Thanks!