CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 1 of 1
  1. #1
    Join Date
    Jul 2001
    Location
    Sunny South Africa
    Posts
    11,283

    VB.NET API General : How Do I Use The API in VB.NET

    Q: How do I use APIs in .NET?

    A: If you come from a VB 6 background, you probably are accustomed to Declaring APIs like :
    Code:
    Private Declare Function SetSysColors Lib "user32" _
    (ByVal nChanges As Long, lpSysColor As _
    Long, lpColorValues As Long) As Long
    The breakdown of this can be:
    • Declare indicates that you will be using an "external" function.
    • SetSysColors is the name of the external function
    • Lib "user32" Indicates that the SetSysColor function is present in the User32.dll
    • Then the argumants are listed in brackets


    If you were to use the exact same declaration inside VB.NET, you would notice that it is acceptable.

    Q: But, is it really?

    A: Well, it should logically work, but you may not always get away with this declaration inside VB.NET

    Q: Why?

    A: The major headache would be the datatypes. Sometime an API would be declared using Integers and it will not work properly. In this article, I've outlined the Unmanaged types. In VB 6, we could have used a Long type variable to hold the value of a Window's handle, in VB.NET we must and should use the IntPtr type variable.

    Q: What should the proper Declaration in .NET look like?

    A: Before we get there, we must know about the System.RunTime.InterOpSevices Namespace and DLLImport

    Q: What are the Memebers of DLLImport:
    A:
    BestFitMapping

    CallingConvention

    CharSet

    EntryPoint

    ExactSpelling

    PreserveSig

    SetLastError

    ThrowOnUnmappableChar

    Based on this, it is clear that we should Import the System.RunTime.InterOpSevices Namespace, at the top of your form ( above everything else ) for example :
    Code:
    Imports System.Runtime.Interopservices 'Import
    Public Class Form1
    And then, declare the API in the following way:
    Code:
        <DllImport("User32", EntryPoint:="SetSysColors")> _
        Private Shared Function SetSysColors(ByVal nChanges As Int32, ByRef lpSysColor As Int32, ByRef lpColorValues As Int32) As Int32
        End Function
    The breakdown is as follows:
    • DLLImport indcates that we are "importing" a function present in an external dll
    • User32, means that we are using User32.dll here
    • EntryPoint, which is a named argument, is used to indicate which function we are going to use
    • Private Shared Fuction, means indicates the name of the function, for VB purposes
    • Then, the argument list in brackets. If you look closely, you will notice the Datatype differences between the VB 6 version ( as stated above ), and the .NET version.


    Q: Is there an equivalent tool to VB6's API Text Viewer?

    A: Yes, you can download it from here :
    API Text Viewer 2004
    Last edited by HanneSThEGreaT; June 10th, 2008 at 05:51 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured