Hi,
I have just finished an SQL function for SQLite which will allow to call
exported function form a Win32 DLL.
 
This is an example of the calling syntax
update UserPassword set Password =
se_fn("mydll_01","Encrypt",'NewPassword');
 
or
 
select se_fn("mydll_01","Encrypt",[Message]) from UserMessages
 
The function is called  se_fn (SQLite Extension)
Parameter One: DLL name
Parameter Two: Function Name
All other parameter are passed to the extension DLL
 
My thoughts behind this function was to help the many people who are using
wrappers or who use languages that don't support points or c type strings
(like VB ASP PHP,..).  These languages can't use the sqlite_create_function.
By using this function they can still extend SQLite for more power.
Some of the uses I can see are compression , encryption, scientific
calculations , basically any thing.
 
At the moment I have written the WIN32 function and I would not know where
to start for any other platform.
 
This is the function as it appear in func.c, any comments would be welcome 
 
#if WIN_32
#include "Winbase.h"
#include "windows.h"
/***************************************************************************
This is an extesion Function which allows the loading and execution of
exported 
functions from DLL's.  The main purpose of this function is to provide
extensions 
to SQLite to enviroments that don't support C type pointer or strings. 
i.e VB ASP PHP and the like.
 
The external DLL must be a standard WIN32 DLL which exports two function
This is the first function, which is used to release the memory allocated by
the second function
void FreeMemory(char **argr) {
 if (argr[0] == NULL) {
  return;
 }
 free(argr[0]);
}
This is a prototype of the second function (the one you wnat to call)  The
first two parameter 
are your calling parameter as pssed in the SQL statement.  The third
parameter is the return parameter 
for results.  You must allocate the memory for that and NULL terminate the
value.
 
void TestFunction_01(int argc, const char **argv, char **argr) {
 // Test to see if we have a pointer for argr
 // Do some processing
 argr[0]=(char *)malloc(100);
 sprintf(argr[0],"Done");
 return;
}
 
***************************************************************************/
typedef void (*SE_ExternalFunction)(int , const char **, char **);
typedef void (*SE_FreeMemory)(char **);
 
static void SE_fnFunc (sqlite_func *context, int argc, const char **argv){
 HINSTANCE hinstLib;
 BOOL fFreeResult;
 SE_ExternalFunction LocalFunction;
 SE_FreeMemory FreeMemoryFunction;
 char *argr=NULL;
 
 if (argc < 2 ) {
  // Return is not enough parameters
  return;
 }
 hinstLib=LoadLibrary(argv[0]);
 if (hinstLib != NULL) {
  // We have loaded the DLL now lets map the function call
  // Load the extenal function
  LocalFunction=(SE_ExternalFunction)GetProcAddress(hinstLib, argv[1]); 
  // Load the Release memory fuction
  FreeMemoryFunction=(SE_FreeMemory)GetProcAddress(hinstLib, "FreeMemory"); 
  if (LocalFunction != NULL && FreeMemoryFunction != NULL) {
   // We have mapped the Function
   // Allocate argr;
   LocalFunction(argc, argv, &argr);
   if (argr != NULL) {
    sqlite_set_result_string(context, argr, -1);
   } 
   else 
   {
    sqlite_set_result_string(context, NULL, -1);
   }
   // Free the memory allocated to argr on the other heap.  
   //Must do this by using the FreeMemeory function in the DLL
   FreeMemoryFunction(&argr);
  }
  else
  {
   // Error Loading Function
   sqlite_set_result_string(context, "Error Mapping Functions", -1);
  }
  // Free library
  fFreeResult = FreeLibrary(hinstLib); 
 }
 else
 {
  // Error loading Library
  sqlite_set_result_string(context, "Error Loading Library", -1);
  return;
 }
}
#endif
 
Kind regards
Greg 

Reply via email to