How to make your own ELF
by
, 19-Jul-2013 at 04:33 PM (21552 Views)
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.
- Start Microsoft Visual Studio 2008
- From the File menu, select New and then Project….
- From the Project types pane, under Visual C++, select MFC.
- From the Templates pane, select MFC DLL.
- Choose a name for the project, such as ExtendedLibraryFunctions, and enter it in the Name field.
- Press OK to start the MFC DLL wizard. From the Overview page of the MFC DLL wizard dialog, press Next.
- From the Application Settings page of the Win32 Application Wizard, under Application type, select Regular DLL with MFC statically linked.
- Press Finish to create the project.
- Under the Project menu click on the properties item
- Select as configuration All Configurations and set the next options
- General
o Use of MFC = Use Standard Windows Librarieso Character Set = Use Multi-Byte Character Set
- C/C++ ->Advanced
o Calling convention = _stdcallVisual Report Writer uses the standard calling convention (_stdcall). If you are planning to use another development environment make sure that you use this convention.
- Click on OK to accept the settings
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:
Open the ExtendedLibraryFunctions.def file to define the function to be exported. The Library definition will be something like :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;}
Add the functions that you want to expose.Code:LIBRARY "ExternalLibraryFunctions". EXPORTS
Now the library is finished and can be build. To build the library click on the menu build an select build solution.Code:EXPORTSnumber2words
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.Attachment 6649
- Open Visual Report Writer and open a report where you want to use the function in.
- Add a new function object to the report
- In the function editor click on the button add function Attachment 6648
- 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.
- Select the library you just copied
- Select the function in the dropdown box
- Provide the number of parameters that the function has.
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:
Happy codingCode:return number2words({Customer.Purchases})
Bénar Bloemendal