Microsoft KB Archive/94960

= How to Pass Numeric Variables to a C DLL =

Article ID: 94960

Article Last Modified on 11/18/2003

-

APPLIES TO


 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft Visual Basic 3.0 Professional Edition
 * Microsoft QuickC 1.0

-



This article was previously published under Q94960



SUMMARY
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.



Example One
 Start Visual Basic or if you are in Visual Basic, choose New Project from the File menu (ALT, F, N). Form1 is created by default. Place five command buttons (Command1, Command2, Command3, Command4, and Command5) on Form1. Add two Text boxes (Text1 and Text2) to Form1.  Add the following code to the General Declarations section of Form1: Declare Function noparams Lib &quot;passnums.dll&quot; As Integer Declare Function passint Lib &quot;passnums.dll&quot; (ByVal x%) As Integer Declare Function passlong Lib &quot;passnums.dll&quot; (ByVal x&amp;) As Long Declare Function passfloat Lib &quot;passnums.dll&quot; (ByVal x!) As Single Declare Function passdouble Lib &quot;passnums.dll&quot; (ByVal x#) As Double

  Add the following code to the click event of each command buttons: Sub Command1_Click text1.Text = Str$(noparams) text2.Text = &quot;Noparams&quot; End Sub

Sub Command2_Click i% = 21 text1.Text = Str$(passint(55)) text2.Text = Str$(passint(i%)) End Sub

Sub Command3_Click i&amp; = 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

 Start Microsoft QuickC for Windows, or if it's already running, choose New from the File menu.  Add the following code to the new file: #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); }

</li></ol>

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.

<ol> From the File menu, choose Save As, and save the file as PASSNUMS.C.</li>  From the File menu, choose New, and Type these .DEF file lines: <pre class="fixed_text">   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

</li> From the File menu, choose Save As, and save the file as PASSNUMS.DEF.</li> From the Project Menu, choose Open and enter PASSNUMS.</li> Choose the OK button. Add PASSNUMS.C and PASSNUMS.DEF to the project.</li> From the Options menu, choose Project. Set the program type to Windows DLL and set the compiler memory model to Large.</li> From the Project menu, choose Rebuild All. This creates PASSNUMS.DLL.</li> Return to Visual Basic and run the program. Pressing any of the command buttons will change the contents of the two text boxes.</li></ol>

Example Two
<ol> Start Visual Basic, or if Visual Basic is already running, from the File menu, choose New Project (ALT, F, N). Form1 is created by default.</li> Place two command buttons (Command1, Command2) on Form1.</li> Add 2 Text boxes (Text1, Text2) to Form1.</li>  Add the following code to the General Declarations section of Form1: ' Enter each of the following Declare statements on one, single line: Declare Function bunchparam Lib &quot;multvars.dll&quot; (ByVal w%,     ByVal x&amp;, ByVal y!, ByVal z#) As Double Declare Function bunchbyref Lib &quot;multvars.dll&quot; (x%, y&amp;, z!, a#) As Double

</li>  Add the following code to the click events of the Command buttons: 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&amp; = 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

</li> Start Microsoft QuickC for Windows or choose New from the File menu.</li>  Add the following code to the new file: #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); }

</li> <li>From the File menu, choose Save As, and save the file as MULTVARS.C.</li> <li> From the File menu, choose New, and type these .DEF file lines: <pre class="fixed_text">  LIBRARY     MULTVARS EXETYPE    WINDOWS 3.1 DATA       PRELOAD MOVABLE SINGLE CODE       PRELOAD MOVABLE DISCARDABLE EXPORTS bunchparam @1 bunchbyref @2

</li> <li>From the File menu, choose Save As, and save the file as MULTVARS.DEF.</li> <li>From the Project Menu, choose Open and enter MULTVARS.</li> <li>Choose the OK button. Add MULTVARS.C and MULTVARS.DEF to the project.</li> <li>From the Options menu, choose Project. Set the program type to Windows DLL and set the compiler memory model to Large.</li> <li>From the Project menu, choose Rebuild All. This creates MULTVARS.DLL.</li> <li>Return to Visual Basic and run the program. Pressing either Command button will change the contents of the text boxes.</li></ol>

Additional query words: 2.00 3.00

Keywords: KB94960

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.