How to Pass Numeric Variables to a C DLLID: Q94960
|
This article shows by example how to pass numeric variables from Visual Basic for Windows to a C DLL. The first example shows how to call C functions with single parameters of all numeric types. The second example shows how to pass multiple parameters and how to pass variables by reference so they can be manipulated on the C side.
Declare Function noparams Lib "passnums.dll" () As Integer
Declare Function passint Lib "passnums.dll" (ByVal x%) As Integer
Declare Function passlong Lib "passnums.dll" (ByVal x&) As Long
Declare Function passfloat Lib "passnums.dll" (ByVal x!) As Single
Declare Function passdouble Lib "passnums.dll" (ByVal x#) As Double
Sub Command1_Click ()
text1.Text = Str$(noparams())
text2.Text = "Noparams"
End Sub
Sub Command2_Click ()
i% = 21
text1.Text = Str$(passint(55))
text2.Text = Str$(passint(i%))
End Sub
Sub Command3_Click ()
i& = 45000
text1.Text = Str$(passlong(40000))
text2.Text = Str$(passlong(i&))
End Sub
Sub Command4_Click ()
i! = 1.35
text1.Text = Str$(passfloat(1.23))
text2.Text = Str$(passfloat(i!))
End Sub
Sub Command5_Click ()
i# = 1234.5678
text1.Text = Str$(passdouble(1.23456))
text2.Text = Str$(passdouble(i#))
End Sub
#include <windows.h>
#include <stdio.h>
/* Noparams takes no parameters and returns a 2 */
extern int far pascal noparams()
{
return(2);
}
/* add 32 to the integer passed in */
extern int far pascal passint(int a)
{
a += 32;
return(a);
}
/* passlong() takes a long integer and adds 7 to it */
extern long far pascal passlong(long x)
{
x += 7;
return(x+7);
}
// passfloat passes a floating point number
extern float far pascal passfloat(float x)
{
return (x += (float) 1.45927);
}
// passdouble passes a floating point number
extern double far pascal passdouble(double x)
{
return (x+=(double) 1.45927);
}
NOTE: Microsoft C and Borland C return values of type Double differently. Therefore the passdouble example above won't work in Borland C. Use the following code in Borland C:
// return a value through y
void FAR PASCAL _export passdouble(double x, double *y)
{
// do processing here
// use '*y =' instead of a return statement
*y = x;
}
Borland C is manufactured by a vendor independent of Microsoft; Microsoft makes no warranty, implied or otherwise, regarding Borland C's performance or reliability.
LIBRARY PASSNUMS
EXETYPE WINDOWS 3.1
DATA PRELOAD MOVABLE SINGLE
CODE PRELOAD MOVABLE DISCARDABLE
EXPORTS
noparams @1
passint @2
passlong @3
passfloat @4
passdouble @5
' Enter each of the following Declare statements on one, single line:
Declare Function bunchparam Lib "multvars.dll" (ByVal w%,
ByVal x&, ByVal y!, ByVal z#) As Double
Declare Function bunchbyref Lib "multvars.dll"
(x%, y&, z!, a#) As Double
Sub Command1_Click ()
i% = 123
j& = 40000
k! = 1.234
l# = 1234.567
text1.Text = Str$(bunchparam(123, 40000, 1.2345, 1.2345))
text2.Text = Str$(bunchparam(i%, j&, k!, l#))
End Sub
Sub Command2_Click ()
i% = 12
j& = 40000
k! = 123.455
l# = 123455.678
x# = bunchbyref(i%, j&, k!, l#)
text1.Text = Str$(i%) + Str$(j&) + Str$(k!) + Str$(l#)
text2.Text = Str$(x#)
End Sub
#include <windows.h>
#include <stdio.h>
/* bunchparam() adds double-precision values and an integer. */
extern double far pascal bunchparam(int a, long b, float c, double d)
{
return(a+b+c+d);
}
extern double far pascal bunchbyref(int *a, long *b, float *c, double *d
)
{
*a += 55;
*b += 77;
*c += (float) 123.456;
*d += 12345.678;
return(*a+*b);
}
LIBRARY MULTVARS
EXETYPE WINDOWS 3.1
DATA PRELOAD MOVABLE SINGLE
CODE PRELOAD MOVABLE DISCARDABLE
EXPORTS
bunchparam @1
bunchbyref @2
Additional query words: 2.00 3.00
Keywords :
Version :
Platform :
Issue type :
Last Reviewed: June 18, 1999