Intro:
for more advanced coding, we do some real close coding to the OS. that means we create some functions to work with, but the system has some similar method to work with it self. now Microsoft gave us a way to make our job easier and simpler, by offring us to use the functions and procedures that windows it self uses. all those methods are stored in files called libraries, with the extension .DLL. those are called APIs, Literaly Application Programming Interface. so far sounds cool right?
Declaring:
in VB.Net, there is a defined method of declaring APIs. here how it works:
Declare Function FuncName Lib “Library” Alias “Alias” ([arglist]) As Type
now lets explain it:
-the word Declare means we are calling a method from the outside.
-the word Function defines the type of the method we are calling. in same cases it can be a SUB instead.
-the word FuncName is what we chose for name of the function.
-now Lib “Library” means that we will specify from where we are gonna call the method. e.g : Lib "User32.Dll". here our function is stored in the User32.dll file located in the system directory.
-Alias “Alias” will specify the name of the function inside that dll. it is well defined, not something we can choose.
-the ([arglist]) is the arguments that we will pass to the method,same as regular methods. e.g: (Byval i As Integer, Byval j As String). those parameters are well defined for each api.
-and as any function, its type needs to be specified. As type will define the type of the returned Value.
Extras: this is not it! Options are more than that!
-Before Declare, it is optionnal to add visiblity properties, such as Private,Public and Friend.
-After Declare, it is optionnal to add the encoding type of the method, such as Ansi, Unicode and Auto. the default in vb.net is unicode.
-it can Be a Function or a Sub. but remember that a Sub does not return a value, so we remove the "As Type" when using SUBs.
-we can declare an API without an "Alias", but in this case, the Name of the method (FuncName) must be the same as the alias, means the same name of the function in it's dll. and it's logic, cuz the computer gonna look for the dll, and search the function with the alias inside. when an alias is not specified, he will use the Name of the method to search. if we choose w.e, it will not be found.
-note that some APIs have no Arguments.
here is an exemple of an api:
Private Declare Ansi Function GetPrivateProfileStringA Lib "kernel32.dll" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
New Declaration !!
Now this looks alot like vb6 declarations, but some options are added.
in VB.NET, we got the possibilty to declare those in a New way:
After Adding This Import :
We can declare those APIs this way:
<DllImport(“Library”, Parameters)> Function FuncName ([arglist]) As Type
End Function
Explaining Again: (tired of typing lool):
-DllImport : this word means we are calling a method from the outside. if you forget the System.Runtime.InteropServices import, this will give you an error, as DllImport is not defined.
-"Library" : with the same meaning, the Dll File name. e.g : "User32.dll"
-Parameters : this part is a set of parameters for the api. in the earlier declaration, we can add ansi for exemple to set the encoding. now in the new declaration, all parameters goes here. they are in total 8 parameters, all optionnals. here is an e.g:
<DllImport("user32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
-after that, we have the word Function, that will define the type of method (Function/Sub).
-([arglist]): almost the same as the other declaration, just arguments to pass to you method. the only diffrence, is that we can do some more specification about one or more variables, with the key word MarshalAs, to set some properties to some variable, when it is unmanaged. here is an exemple:
(ByVal I As Integer,<MarshalAs(UnmanagedType.LPArray,SizeParamIndex:=6)>ByVal R() As Byte).
MarshalAs Offers 10 Parameters To set, 1 necessary and 9 optionnals.
-we come to the As Type, which is the same, removed when declaring a Sub.
-our Declaration ends with The Statament 'End Function' Or 'End Sub' according to what you declared.
Extras!! yes!!
-this declaration supports also being Private,Public,Friend,Shared...ect...
-well nothing to add much, i wanted to talk more about the parameters you can set and options, also about MarshalAs and unmanaged types, but options are alot, and i don't want to spend the night typing loool, so just do some more research about those, you will learn alot.
here is an exemple of an api declaration:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function FindWindow(
ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
I'm About Done, Conclusion
Windows API is Just a big Chapter, this is the very basic of it.
now, for the Apis, the way to find them is MSDN of microsoft. you can find all informations you need about the function of a method, it's arguments and paramters, and more.
you can also use google, another way to find APIs.usally when you search the name of an api with google, the MSDN page is first result anyway. Also there are alot of sites made specially to store API declarations for alot of programming languages.
one more thing, after declaring an api, you can use it like you wrote it in your class, and call it when ever you like. good thing about api's, they can replace big methods with one line declaration, which saves space in your app.so have fun searching what apis you need to make your code smaller, and easier to use.
Well i think this is it. tut fully written by Miharbi-Dono.
don't be shy to reply.
for more advanced coding, we do some real close coding to the OS. that means we create some functions to work with, but the system has some similar method to work with it self. now Microsoft gave us a way to make our job easier and simpler, by offring us to use the functions and procedures that windows it self uses. all those methods are stored in files called libraries, with the extension .DLL. those are called APIs, Literaly Application Programming Interface. so far sounds cool right?
Declaring:
in VB.Net, there is a defined method of declaring APIs. here how it works:
Declare Function FuncName Lib “Library” Alias “Alias” ([arglist]) As Type
now lets explain it:
-the word Declare means we are calling a method from the outside.
-the word Function defines the type of the method we are calling. in same cases it can be a SUB instead.
-the word FuncName is what we chose for name of the function.
-now Lib “Library” means that we will specify from where we are gonna call the method. e.g : Lib "User32.Dll". here our function is stored in the User32.dll file located in the system directory.
-Alias “Alias” will specify the name of the function inside that dll. it is well defined, not something we can choose.
-the ([arglist]) is the arguments that we will pass to the method,same as regular methods. e.g: (Byval i As Integer, Byval j As String). those parameters are well defined for each api.
-and as any function, its type needs to be specified. As type will define the type of the returned Value.
Extras: this is not it! Options are more than that!
-Before Declare, it is optionnal to add visiblity properties, such as Private,Public and Friend.
-After Declare, it is optionnal to add the encoding type of the method, such as Ansi, Unicode and Auto. the default in vb.net is unicode.
-it can Be a Function or a Sub. but remember that a Sub does not return a value, so we remove the "As Type" when using SUBs.
-we can declare an API without an "Alias", but in this case, the Name of the method (FuncName) must be the same as the alias, means the same name of the function in it's dll. and it's logic, cuz the computer gonna look for the dll, and search the function with the alias inside. when an alias is not specified, he will use the Name of the method to search. if we choose w.e, it will not be found.
-note that some APIs have no Arguments.
here is an exemple of an api:
Private Declare Ansi Function GetPrivateProfileStringA Lib "kernel32.dll" (ByVal lpApplicationName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Integer, ByVal lpFileName As String) As Integer
New Declaration !!
Now this looks alot like vb6 declarations, but some options are added.
in VB.NET, we got the possibilty to declare those in a New way:
After Adding This Import :
Code
Imports System.Runtime.InteropServicesWe can declare those APIs this way:
<DllImport(“Library”, Parameters)> Function FuncName ([arglist]) As Type
End Function
Explaining Again: (tired of typing lool):
-DllImport : this word means we are calling a method from the outside. if you forget the System.Runtime.InteropServices import, this will give you an error, as DllImport is not defined.
-"Library" : with the same meaning, the Dll File name. e.g : "User32.dll"
-Parameters : this part is a set of parameters for the api. in the earlier declaration, we can add ansi for exemple to set the encoding. now in the new declaration, all parameters goes here. they are in total 8 parameters, all optionnals. here is an e.g:
<DllImport("user32.dll", CharSet:=CharSet.Unicode, ExactSpelling:=True)>
-after that, we have the word Function, that will define the type of method (Function/Sub).
-([arglist]): almost the same as the other declaration, just arguments to pass to you method. the only diffrence, is that we can do some more specification about one or more variables, with the key word MarshalAs, to set some properties to some variable, when it is unmanaged. here is an exemple:
(ByVal I As Integer,<MarshalAs(UnmanagedType.LPArray,SizeParamIndex:=6)>ByVal R() As Byte).
MarshalAs Offers 10 Parameters To set, 1 necessary and 9 optionnals.
-we come to the As Type, which is the same, removed when declaring a Sub.
-our Declaration ends with The Statament 'End Function' Or 'End Sub' according to what you declared.
Extras!! yes!!
-this declaration supports also being Private,Public,Friend,Shared...ect...
-well nothing to add much, i wanted to talk more about the parameters you can set and options, also about MarshalAs and unmanaged types, but options are alot, and i don't want to spend the night typing loool, so just do some more research about those, you will learn alot.
here is an exemple of an api declaration:
<DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)>
Private Shared Function FindWindow(
ByVal lpClassName As String, _
ByVal lpWindowName As String) As IntPtr
End Function
I'm About Done, Conclusion
Windows API is Just a big Chapter, this is the very basic of it.
now, for the Apis, the way to find them is MSDN of microsoft. you can find all informations you need about the function of a method, it's arguments and paramters, and more.
you can also use google, another way to find APIs.usally when you search the name of an api with google, the MSDN page is first result anyway. Also there are alot of sites made specially to store API declarations for alot of programming languages.
one more thing, after declaring an api, you can use it like you wrote it in your class, and call it when ever you like. good thing about api's, they can replace big methods with one line declaration, which saves space in your app.so have fun searching what apis you need to make your code smaller, and easier to use.
Well i think this is it. tut fully written by Miharbi-Dono.
don't be shy to reply.



![[Image: moonlight_sanctuary.png]](https://moonlight-sanctuary.org/images/moonlight_sanctuary.png)
