How to Pass Numeric Variables to a C DLL

Last reviewed: June 21, 1995
Article ID: Q94960
The information in this article applies to:

- Standard and Professional Editions of Microsoft Visual Basic for

  Windows, versions 2.0 and 3.0
- Microsoft QuickC for Windows, version 1.0

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.

MORE INFORMATION

Example One

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

  2. Place five command buttons (Command1, Command2, Command3, Command4, and Command5) on Form1.

  3. Add two Text boxes (Text1 and Text2) to Form1.

  4. Add the following code to the General Declarations section of Form1:

       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
    
    

  5. Add the following code to the click event of each command buttons:

       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
    
    

  6. Start Microsoft QuickC for Windows, or if it's already running, choose New from the File menu.

  7. 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);
       }
    
       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.
    
    

  8. From the File menu, choose Save As, and save the file as PASSNUMS.C.

  9. From the File menu, choose New, and Type these .DEF file lines:

        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
    
10. From the File menu, choose Save As, and save the file as PASSNUMS.DEF.

  1. From the Project Menu, choose Open and enter PASSNUMS.

  2. Choose the OK button. Add PASSNUMS.C and PASSNUMS.DEF to the project.

  3. From the Options menu, choose Project. Set the program type to Windows

        DLL and set the compiler memory model to Large.
    

  4. From the Project menu, choose Rebuild All. This creates PASSNUMS.DLL.

  5. Return to Visual Basic and run the program. Pressing any of the command

        buttons will change the contents of the two text boxes.
    

Example Two

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

  2. Place two command buttons (Command1, Command2) on Form1.

  3. Add 2 Text boxes (Text1, Text2) to Form1.

  4. 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 "multvars.dll" (ByVal w%,
          ByVal x&, ByVal y!, ByVal z#) As Double
       Declare Function bunchbyref Lib "multvars.dll"
          (x%, y&, z!, a#) As Double
    
    

  5. 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& = 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
    
    

  6. Start Microsoft QuickC for Windows or choose New from the File menu.

  7. 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);
       }
    
    

  8. From the File menu, choose Save As, and save the file as MULTVARS.C.

  9. From the File menu, choose New, and type these .DEF file lines:

       LIBRARY     MULTVARS
       EXETYPE     WINDOWS 3.1
       DATA        PRELOAD MOVABLE SINGLE
       CODE        PRELOAD MOVABLE DISCARDABLE
       EXPORTS
                   bunchparam @1
                   bunchbyref @2
    
    

  10. From the File menu, choose Save As, and save the file as MULTVARS.DEF.

  11. From the Project Menu, choose Open and enter MULTVARS.

  12. Choose the OK button. Add MULTVARS.C and MULTVARS.DEF to the project.

  13. From the Options menu, choose Project. Set the program type to

        Windows DLL and set the compiler memory model to Large.
    

  14. From the Project menu, choose Rebuild All. This creates MULTVARS.DLL.

  15. Return to Visual Basic and run the program. Pressing either Command

        button will change the contents of the text boxes.
    


Additional reference words: 2.00 3.00
KBCategory: kbprg kbcode
KBSubcategory: APrgOther


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: June 21, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.