| title | Walkthrough: Calling Windows APIs (Visual Basic) | ||||||||
|---|---|---|---|---|---|---|---|---|---|
| ms.custom | |||||||||
| ms.date | 07/20/2015 | ||||||||
| ms.prod | .net | ||||||||
| ms.reviewer | |||||||||
| ms.suite | |||||||||
| ms.technology |
|
||||||||
| ms.topic | article | ||||||||
| helpviewer_keywords |
|
||||||||
| ms.assetid | 9280ca96-7a93-47a3-8d01-6d01be0657cb | ||||||||
| caps.latest.revision | 20 | ||||||||
| author | dotnet-bot | ||||||||
| ms.author | dotnetcontent |
Windows APIs are dynamic-link libraries (DLLs) that are part of the Windows operating system. You use them to perform tasks when it is difficult to write equivalent procedures of your own. For example, Windows provides a function named FlashWindowEx that lets you make the title bar for an application alternate between light and dark shades.
The advantage of using Windows APIs in your code is that they can save development time because they contain dozens of useful functions that are already written and waiting to be used. The disadvantage is that Windows APIs can be difficult to work with and unforgiving when things go wrong.
Windows APIs represent a special category of interoperability. Windows APIs do not use managed code, do not have built-in type libraries, and use data types that are different than those used with Visual Studio. Because of these differences, and because Windows APIs are not COM objects, interoperability with Windows APIs and the [!INCLUDEdnprdnshort] is performed using platform invoke, or PInvoke. Platform invoke is a service that enables managed code to call unmanaged functions implemented in DLLs. For more information, see Consuming Unmanaged DLL Functions. You can use PInvoke in [!INCLUDEvbprvb] by using either the Declare statement or applying the DllImport attribute to an empty procedure.
Windows API calls were an important part of [!INCLUDEvbprvb] programming in the past, but are seldom necessary with Visual Basic .NET. Whenever possible, you should use managed code from the [!INCLUDEdnprdnshort] to perform tasks, instead of Windows API calls. This walkthrough provides information for those situations in which using Windows APIs is necessary.
[!INCLUDEnote_settings_general]
The most common way to call Windows APIs is by using the Declare statement.
-
Determine the name of the function you want to call, plus its arguments, argument types, and return value, as well as the name and location of the DLL that contains it.
[!NOTE] For complete information about the Windows APIs, see the Win32 SDK documentation in the Platform SDK Windows API. For more information about the constants that Windows APIs use, examine the header files such as Windows.h included with the Platform SDK.
-
Open a new Windows Application project by clicking New on the File menu, and then clicking Project. The New Project dialog box appears.
-
Select Windows Application from the list of [!INCLUDEvbprvb] project templates. The new project is displayed.
-
Add the following
Declarefunction either to the class or module in which you want to use the DLL:[!code-vbVbVbalrInterop#9]
The Declare statement includes the following elements.
The Auto modifier instructs the runtime to convert the string based on the method name according to common language runtime rules (or alias name if specified).
The name following the Function keyword is the name your program uses to access the imported function. It can be the same as the real name of the function you are calling, or you can use any valid procedure name and then employ the Alias keyword to specify the real name of the function you are calling.
Specify the Lib keyword, followed by the name and location of the DLL that contains the function you are calling. You do not need to specify the path for files located in the Windows system directories.
Use the Alias keyword if the name of the function you are calling is not a valid [!INCLUDEvbprvb] procedure name, or conflicts with the name of other items in your application. Alias indicates the true name of the function being called.
Declare the arguments and their data types. This part can be challenging because the data types that Windows uses do not correspond to Visual Studio data types. [!INCLUDEvbprvb] does a lot of the work for you by converting arguments to compatible data types, a process called marshaling. You can explicitly control how arguments are marshaled by using the xref:System.Runtime.InteropServices.MarshalAsAttribute attribute defined in the xref:System.Runtime.InteropServices namespace.
Note
Previous versions of [!INCLUDEvbprvb] allowed you to declare parameters As Any, meaning that data of any data type could be used. [!INCLUDEvbprvb] requires that you use a specific data type for all Declare statements.
Some arguments are combinations of constants. For example, the MessageBox API shown in this walkthrough accepts an integer argument called Typ that controls how the message box is displayed. You can determine the numeric value of these constants by examining the #define statements in the file WinUser.h. The numeric values are generally shown in hexadecimal, so you may want to use a calculator to add them and convert to decimal. For example, if you want to combine the constants for the exclamation style MB_ICONEXCLAMATION 0x00000030 and the Yes/No style MB_YESNO 0x00000004, you can add the numbers and get a result of 0x00000034, or 52 decimal. Although you can use the decimal result directly, it is better to declare these values as constants in your application and combine them using the Or operator.
-
Consult the documentation for the Windows function you are calling. Determine the name of the constants it uses and the name of the .h file that contains the numeric values for these constants.
-
Use a text editor, such as Notepad, to view the contents of the header (.h) file, and find the values associated with the constants you are using. For example, the
MessageBoxAPI uses the constantMB_ICONQUESTIONto show a question mark in the message box. The definition forMB_ICONQUESTIONis in WinUser.h and appears as follows:#define MB_ICONQUESTION 0x00000020L -
Add equivalent
Conststatements to your class or module to make these constants available to your application. For example:[!code-vbVbVbalrInterop#11]
-
Add a button named
Button1to the startup form for your project, and then double-click it to view its code. The event handler for the button is displayed. -
Add code to the
Clickevent handler for the button you added, to call the procedure and provide the appropriate arguments:[!code-vbVbVbalrInterop#12]
-
Run the project by pressing F5. The message box is displayed with both Yes and No response buttons. Click either one.
[!INCLUDEvbprvb] automatically converts the data types of parameters and return values for Windows API calls, but you can use the MarshalAs attribute to explicitly specify unmanaged data types that an API expects. For more information about interop marshaling, see Interop Marshaling.
-
Determine the name of the function you want to call, plus its arguments, data types, and return value.
-
To simplify access to the
MarshalAsattribute, add anImportsstatement to the top of the code for the class or module, as in the following example:[!code-vbVbVbalrInterop#13]
-
Add a function prototype for the imported function to the class or module you are using, and apply the
MarshalAsattribute to the parameters or return value. In the following example, an API call that expects the typevoid*is marshaled asAsAny:[!code-vbVbVbalrInterop#14]
The DllImport attribute provides a second way to call functions in DLLs without type libraries. DllImport is roughly equivalent to using a Declare statement but provides more control over how functions are called.
You can use DllImport with most Windows API calls as long as the call refers to a shared (sometimes called static) method. You cannot use methods that require an instance of a class. Unlike Declare statements, DllImport calls cannot use the MarshalAs attribute.
-
Open a new Windows Application project by clicking New on the File menu, and then clicking Project. The New Project dialog box appears.
-
Select Windows Application from the list of [!INCLUDEvbprvb] project templates. The new project is displayed.
-
Add a button named
Button2to the startup form. -
Double-click
Button2to open the code view for the form. -
To simplify access to
DllImport, add anImportsstatement to the top of the code for the startup form class:[!code-vbVbVbalrInterop#13]
-
Declare an empty function preceding the
End Classstatement for the form, and name the functionMoveFile. -
Apply the
PublicandSharedmodifiers to the function declaration and set parameters forMoveFilebased on the arguments the Windows API function uses:[!code-vbVbVbalrInterop#16]
Your function can have any valid procedure name; the
DllImportattribute specifies the name in the DLL. It also handles interoperability marshaling for the parameters and return values, so you can choose Visual Studio data types that are similar to the data types the API uses. -
Apply the
DllImportattribute to the empty function. The first parameter is the name and location of the DLL containing the function you are calling. You do not need to specify the path for files located in the Windows system directories. The second parameter is a named argument that specifies the name of the function in the Windows API. In this example, theDllImportattribute forces calls toMoveFileto be forwarded toMoveFileWin KERNEL32.DLL. TheMoveFileWmethod copies a file from the pathsrcto the pathdst.[!code-vbVbVbalrInterop#17]
-
Add code to the
Button2_Clickevent handler to call the function:[!code-vbVbVbalrInterop#18]
-
Create a file named Test.txt and place it in the C:\Tmp directory on your hard drive. Create the Tmp directory if necessary.
-
Press F5 to start the application. The main form appears.
-
Click Button2. The message "The file was moved successfully" is displayed if the file can be moved.
xref:System.Runtime.InteropServices.DllImportAttribute
xref:System.Runtime.InteropServices.MarshalAsAttribute
Declare Statement
Auto
Alias
COM Interop
Creating Prototypes in Managed Code
Marshaling a Delegate as a Callback Method