[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-gsl] GSL and VB6

From: mail
Subject: Re: [Help-gsl] GSL and VB6
Date: Sun, 26 Jun 2005 09:34:45 +0200

On 25 Jun 2005 at 20:14, Rui Fernandes wrote:

> Hi,
> I'm trying to use GSL's dlls with Visual Basic 6.0. I'm declaring some test 
> functions like this
> Public Declare Function gsl_acosh Lib "libgsl.dll" (ByVal x As Double)
> but I get the error "Bad DLL calling convention" ( error 49 ) when I call it 
> like, for example
> Dim a As Double
> a = gsl_acosh(0.2)
> Does anyone knows what is wrong here? The debug gives the error on the 
> "a=gsl_...". It seems to find the function, but it doesn't work.
> Please answer as soon as possible.
> Kind regards,
> Rui Miguel Fernandes
> Porto - Portugal

As arccos takes its values in [1,+inf[ its inverse arccosh has no
real solution for 0.2, that is 1.369438406004566*I, I=imag unit.
Write arccosh as ln(x+(x-1)^(1/2)*(x+1)^(1/2)) to cover that case,
GSL here works within the reals.

For the error message(s) use google to search for "An Explanation
of Trappable Errors in Visual Basic" and "142138" as web document
(quite useful for working with VB/VBA). There it is stated:

  Your program is calling a routine in a dynamic-link library (DLL)
  that either is being passed the wrong type or number of arguments
  or does not use the Pascal calling convention. Make sure that the
  arguments passed to the DLL routine exactly match the arguments 
  expected by the routine. If the DLL routine expects arguments by
  value, then make sure ByVal is specified for those arguments in
  the declaration for the routine. 

As to my knowledge the GSL DLLs on Windows unfortunately do not
use the 'Pascal calling convention' (or __stdcall using MSVC).

One either has to re-compile the whole library for that. Or just
use a wrapper which does nothing but cares for that convention and
if you only need it for simple functions (no arrays, structs ...)
that is not too complicated:

You can find an example in Excel/VBA with a wrapper DLL at , that DLL should work
in pure VB as well (it even does with Maple).

It uses typedef double (*double_fct_1)(double); // GSL hat cdecl als export

and the following simple and stupid function (which has to be exported
for use in VB, see the docu in MSVC): 

double __stdcall wrap1_txt(char* fctname, double x)
        char * theLIB;
        char * theFunction;
        double dummy;

        theLIB = "gsl.dll";
        dummy = 0.0;

        HINSTANCE hLib=LoadLibrary(theLIB);

        if(hLib==NULL) {

        myfct = (double_fct_1)GetProcAddress((HMODULE)hLib, fctname);

        if((myfct==NULL)) {

        dummy = myfct(x);

Thus it takes some GSL function name as string and a real float
to call it from the GSL.DLL and returns the result (with some
simple and idiotic error values), but cares for Pascal convention.

That approach also works if your function has several arguments as
input or output (use appropriate typedef and assignment). Note it
does not check the syntax.

Hope that helps ...

reply via email to

[Prev in Thread] Current Thread [Next in Thread]