View RSS Feed

Development Team Blog

How to use Windows API functions in Visual Report Writer

Rate this Entry
by , 19-Jul-2013 at 04:37 PM (106362 Views)
In this blog I’ll show you how to use Windows API functions in Visual Report Writer.
We are using the function PathFileExists exposed by the library ‘Shlwapi.dll

Visual Report Writer uses the VARIANT type to communicate with the External Library Functions (ELF). Because of this Windows API’s can’t be called directly, doing so will stop the rendering of the reports as Visual Report Writer expects a variant type to return and direct calling might even crash the Visual Report Writer.

To call windows API functions we have to use wrapper functions that converts the variant type of Visual Report Writer to the types that the API call wants.

Please read the blog http://support.dataaccess.com/Forums...e-your-own-ELF for an explanation on how to create a ELF dll. The DLL created by that blog will be used here.

The variants uses Unicode to format the strings, thus we are using the Unicode variant of PathFileExists called PathFileExistsW. Include the header file of Shlwapi at the top of ExternalLibraryFunctions.cpp file to access the function.

Code:
#include "Shlwapi.h"
Add the next code to the end of ExternalLibraryFunctions.cpp to convert the call made by Visual Report Writer to the API function.

Code:
VARIANT __declspec(dllexport) FileExists(VARIANT PathFile)
{
char *pszPath; int retval; CComVariant vRes; USES_CONVERSION; // Check argument type if (PathFile.vt != VT_BSTR) {
vRes.vt = VT_ERROR; vRes.scode = MAKE_HRESULT(1,FACILITY_ITF,1); return vRes;
} retval = PathFileExistsW(PathFile.bstrVal); // return a boolean instead of a integer variable vRes = (retval==1); return vRes;
}
To expose the function we add in ExternalLibraryFunctions.def the next line of code at the end of the EXPORTS section

Code:
FileExists
Now the export section looks like :

Code:
EXPORTS
number2words FileExists
We can build the library and copy the resulted dll file to the folder where Visual Report Writer resides. Add the ELF to Visual Report Writer and use it like :
Code:
//check if there is a logo of the customer
if (FileExists("C:\logos\" + {Customer.Name} + ".jpg")) then
return "C:\logos\" + {Customer.Name} + ".jpg"
else
return ""
end
Happy coding

Bénar Bloemendal

Comments