FAQ: APIs and DLLs in Visual Basic 4.0

Last reviewed: August 25, 1997
Article ID: Q145647
The information in this article applies to:
  • Standard, Professional, and Enterprise Editions of Microsoft Visual Basic, 16-bit and 32-bit, for Windows, version 4.0

SUMMARY

This article covers some of the most Frequently Asked Questions (FAQ) about nontechnical issues for Microsoft Visual Basic for Windows. You can find this and other FAQ articles by querying on the keyword "FAQ." You can find additional general references in the Microsoft Knowledge Base by searching on "article list."

The Visual Basic 3.0 FAQ covering APIs and DLLs may contain additional information regarding a portion of these questions. This FAQ is in the process of being completed. More frequently asked questions will be added to this article by the end of January 1996.

Most of the following information can be found in the "Calling Procedures in DLLs" chapter of the "Programmer's Guide." Additional information can be found in the VB4DLL.TXT text file.

MORE INFORMATION

This article addresses the following questions:

  1. How do you pass an BSTR string to a DLL or API?
2. Why do I get a GP fault after calling an API/DLL that returns a string? 3. How do I pass numbers to my DLL? 4. How do I pass a User Defined Type or Structure? 5. I keep getting "Error Loading DLL" when I try to call my function. All
   the declarations are set up correctly; what else could be going wrong?
6. How do I manipulate INI files? 7. How can I find out more about calling the windows API? 8. How can I create a transparent bitmap, or layer multiple bitmaps on
   top of each other?

Questions and Answers

1. Q. How do you pass an BSTR string to a DLL or API?

   A. To pass a string to an API or DLL call that expects a BSTR, use a
      type library.

2. Q. Why do I get a GP fault after calling an API/DLL that returns a
      string?

   A. You need to initialize your string buffer before you make the call;
      for example:

      szFileName = Space(128)      'initialize to 128 spaces
      Call GetFileName(szFileName)  'call the DLL which modifies the string

3. Q. How do I pass numbers to my DLL?

   A. Usually, you want to use ByVal when passing numerics to your DLL.

4. Q. How do I pass a User Defined Type or Structure?

   A. User Defined Types or Structures (in C) can be passed by reference.
      Because Visual Basic passes this way by default, all you need to do
      is place the user defined type variable in the argument list. For
      example, if you have the following user defined type and function
      declaration:

         Type POINTAPI
           x As Integer
           y As Integer
         End Type
         Declare Function PassUDT Lib "MyLib.DLL" (pt As POINTAPI) _
         As Integer

      you could call the function passing the variable as follows:

         Dim CoOrdinate As POINTAPI
         Dim ret as integer
         Coordinate.X = 10
         Coordinate.Y = 10
         ret = PassUDT(CoOrdinate)

5. Q. I keep getting "Error Loading DLL" when I try to call my function.
      All the declarations are set up correctly; what else could be going
      wrong?

   A. The following are the some of the reasons for this error message:

       - Loading 16-bit DLL into 32-bit address space.
       - Loading 32-bit DLL into 16-bit address space.
       - The DLL cannot find a supporting file.

      Use the LoadLibrary function to load the DLL to get more descriptive
      error messages. If you created the DLL, check your .DEF file and
      make sure the LIBRARY name is the same as the filename.

6. Q. How do I manipulate INI files?

   A. Visual Basic can manipulate INI files through the Windows API. These
      calls are documented in several Knowledge Base articles.

      For additional information, please see the following articles in the
      Microsoft Knowledge Base:

      ARTICLE-ID: Q75639
      TITLE     : How to Access Windows Initialization Files Within Visual
                  Basic

      ARTICLE-ID: Q115328
      TITLE     : DOCERR: Write*ProfileString Declaration Incorrect in API

      ARTICLE-ID: Q110826
      TITLE     : DOCERR: GetPrivateProfileString Declaration Incorrect in
                  API

7. Q. How can I find out more about calling the windows API?

    A. There are two useful help files shipped with Visual Basic,
      Professional Edition. The Windows SDK Help file discusses Windows API
      general topics, functions, structures and messages. Its companion
      help file, Windows 3.1 API Help, offers the Visual Basic declare
      statements, type declarations and global constants used to access
      much of the Windows API. In addition, there are the following
      resources:

      The "Calling Procedures in DLLs" chapter of the "Programmer's Guide"
      is a good place to start looking at DLL and VB issues.

      For additional information, please see the following articles in
      the Microsoft Knowledge Base:

      ARTICLE-ID: Q106553
      TITLE     : How to write C DLL's and Call Them from Visual Basic

      ARTICLE-ID: Q110219
      TITLE     : LONG: How to Call windows API from VB - General
                  Guidelines

      ARTICLE-ID: Q109290
      TITLE     : Popular Windows API Functions Used from Visual Basic 3.0

8. Q. How can I create a transparent bitmap, or layer multiple bitmaps on
      top of each other?

   A. If bitmaps are layered on top of one another, the overlapped regions
      will not show through the bitmaps that are on top unless the upper
      bitmap has transparent regions. Article Q94961 shows how to create a
      transparent bitmap from Visual Basic using Windows APIs.

      For additional information, please see the following article in
      the Microsoft Knowledge Base:

      ARTICLE-ID: Q94961
      TITLE     : How to Create a Transparent Bitmap Using Visual Basic


Additional query words: gpf gp-fault general protection FAQ
Keywords : APrgOther kbfaq
Version : 4.0
Platform : NT WINDOWS


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: August 25, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.