VB. NET dllimport features

Source: Internet
Author: User
'We can use the declare statement to call the process in the external DLL. However, VB. NET provides us with another more advanced feature-dllimport.

For example:

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 as integer, byval brepaint as integer) as integer

End Function

Sub test () sub test ()

Dim hwnd as integer = findwindow (nothing, "untitled-nodepad ")

If hwnd <> 0 then movewindow (hwnd, 0, 0,600,300, 1)

End sub

'In this way, external dll can be called without any code implementation. Even if we change the method name to find0000wa, we can also implement it smoothly, because the dllimport feature compiler can automatically track the actual process and method!

'In addition, The dllimport feature supports several optional parameters to precisely define the method of calling an external process and the return value of an external process.

'Charset parameter: used to describe how a string is transmitted to an external process. It can be charset. ANSI (default), charset. Unicode. charset. Auto.

'Exactspelling parameter: used to specify whether the method name is exactly the same as the name in DLL. The default value is true.

'Entrypoint parameter: Specifies the actual function name in the DLL.

'Callingconvention parameter: Specifies the call convention for the entry point. The values include winapi (default value), cdecl, fastcallstdcall, and thiscall.

'Setlasterror parameter: determines whether the last Win32 error code has been set for the called function. If it is set to true, the code can be read using the err. lastdllerror method or marshal. getlastwin32error method.

The 'preservesig parameter is a Boolean value. If it is true, the compiler is told that the method should not be converted to a function that returns the hresult value.

'The following uses the dllimport feature to call the method named friend in myfunction. dll (friend is the VB retained name). The dllimport feature has a unicode string and affects 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.