Microsoft KB Archive/145647

From BetaArchive Wiki

FAQ: APIs and DLLs in Visual Basic 4.0

ID: Q145647



The information in this article applies to:


  • Microsoft Visual Basic Standard, Professional, and Enterprise Editions, 16-bit and 32-bit, for Windows, version 4.0





SUMMARY

This article covers some of the most Frequently Asked Questions (FAQ) about non-technical 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:

    Q75639 : How to Access Windows Initialization Files Within Visual Basic


    Q115328 : DOCERR: Write*ProfileString Declaration Incorrect in API

    Q110826 : 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.
  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:

Additional query words: gpf gp-fault general protection FAQ

Keywords :
Version : WINDOWS:4.0
Platform : WINDOWS
Issue type :
Technology :


Last Reviewed: May 20, 1999
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.