'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