Vb. NET attribute----DllImport characteristics

Source: Internet
Author: User
Tags error code integer win32 win32 error
We can use the Declare statement to call a procedure in an external DLL. But vb.net provides us with another, more advanced-----dllimport feature.

Such as:

Imports System.Runtime.InteropServices

<dllimport ("user32") > _

Function Findwindow (ByVal lpclassname As String, ByVal Lpwindowname as String) as Integer

End Function

<dllimport ("user32") > _

Function MoveWindow (ByVal hWnd as String, ByVal x As Integer, ByVal y As Integer, ByVal nwidth As Integer, ByVal nheight A s integer, ByVal brepaint As Integer) As Integer

End Function

Sub Test ()

Dim hWnd as Integer = Findwindow (Nothing, "Untitled-nodepad")

If hwnd <> 0 Then MoveWindow (hwnd, 0, 0, 600, 300, 1)

End Sub

This allows the external DLL to be invoked without any code implementation, even if we change the method name Findwindowa, and we can implement the call smoothly because the DllImport feature compiler can automatically track the actual process and the method!

In addition, the DllImport feature Sea supports several optional parameters to precisely define how the external procedure is invoked and how the return value of the external procedure is returned.

CharSet parameter: Used to describe how strings are passed to an external procedure, either CharSet.Ansi (default), CharSet.Unicode.CharSet.Auto.

ExactSpelling parameter: Used to specify whether the method name is exactly the same as the name in the DLL, and the default value is true.

EntryPoint parameter: Used to specify the name of the actual function in the DLL.

CallingConvention parameter: Specifies the calling convention for the entry point, with WINAPI (default), Cdecl,fastcallstdcall, and ThisCall.

SetLastError parameter: Determines whether the called function sets the most recent WIN32 error code, and if set to true, the code can be read by the Err.lastdllerror method or the Marshal.GetLastWin32Error method.

PreserveSig parameter: is a Boolean value, and if true, tells the compiler that the method should not be converted to a return HRESULT value function.

The following uses the DllImport attribute to invoke a method named friend (Friend is a VB reserved name) in MyFunction.dll. The DllImport attribute has a Unicode string and affects the Win32 error code:

<dllimport ("MyFunction.dll", entrypoint:= "Friend", Charset:=charset.unicode, Setlasterror:=true) > _

Function Makefriends (ByVal sl As String, ByVal S2 as String) as Integer

End Function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.