Article ID: 112673 - Last Review: December 9, 2003 - Revision: 2.0

How to Pass & Return Unsigned Integers to DLLs from VB

This article was previously published under Q112673

On This Page

Expand all | Collapse all

SUMMARY

Visual Basic stores its integer variables in an 8-bit data field, as does C. Visual Basic uses signed integers only, so it reserves one of the bits as a sign bit. In C, you have the choice of an unsigned integer (the variable ranges from 0 to 65536) or a signed integer (the variable ranges from -32767 to +32767 as do Visual Basic integer variables).

MORE INFORMATION

Visual Basic stores its integer variables in an 8-bit data field, as does C. Visual Basic uses signed integers only, so it reserves one of the bits as a sign bit. In C, you have the choice of an unsigned integer (the variable ranges from 0 to 65536) or a signed integer (the variable ranges from -32767 to +32767 as do Visual Basic integer variables).

Step-by-Step Example

Follow a process similar to the following to pass a value greater than 32767 as an integer from Visual Basic to a dynamic link library (DLL) that is expecting an unsigned integer or to return an integer value that is outside the range of valid Visual Basic integers:

  1. Start a new project in Visual Basic. Form1 is created by default.
  2. Add the following code to the general declarations section of Form1. Note that you must actually have a DLL that takes an unsigned integer as a parameter.
       ' MyLong is a function in a DLL that takes an unsigned integer as a
       ' parameter and returns the same value passed in. To run this sample you
       ' will have to create the MYLONG function. Enter the following Declare
       ' statement as one, single line:
       Declare Function MyLong Lib "MyLong.DLL" (ByVal iInt AS Integer)
          As Integer
    						
  3. Add a command button (Comamnd1) to Form1.
  4. Add the following code to the Command1_Click event:
       Sub Command1_Click()
          Dim lValue As Long
          Dim i As Integer, w As Integer
          ' Initialize lvalue:
          lValue = 40000
          If lValue > 32767 Then
             w = lValue - 65536
          Else
             w = lValue     ' Just pass it on
          End If
          ' Call a DLL that is expecting an unsigned integer.
          ' For this example, the MyLong function will return
          ' the same value passed in.
          i = MyLong(w)
    
          ' Convert returned value:
          If i < 0 Then
             lValue = 65536 + i
          Else
             lValue = i
          End If
          ' Display the results:
          Print Str(lValue)
       End Sub
    						
  5. Run the program.

APPLIES TO
  • Microsoft Visual Basic 1.0 Standard Edition
  • Microsoft Visual Basic 2.0 Standard Edition
  • Microsoft Visual Basic 3.0 Professional Edition
  • Microsoft Visual Basic 2.0 Professional Edition
Keywords: 
KB112673
Retired KB ArticleRetired KB Content Disclaimer
This article was written about products for which Microsoft no longer offers support. Therefore, this article is offered "as is" and will no longer be updated.
 

Article Translations