June 10th, 2008, 06:41 AM
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 :
The breakdown of this can be:
Private Declare Function SetSysColors Lib "user32" _
(ByVal nChanges As Long, lpSysColor As _
Long, lpColorValues As Long) As Long
- 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
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:
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 :
And then, declare the API in the following way:
Imports System.Runtime.Interopservices 'Import
Public Class Form1
The breakdown is as follows:
<DllImport("User32", EntryPoint:="SetSysColors")> _
Private Shared Function SetSysColors(ByVal nChanges As Int32, ByRef lpSysColor As Int32, ByRef lpColorValues As Int32) As Int32
- 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 06:51 AM.
Click Here to Expand Forum to Full Width
This a Codeguru.com survey!