I have started using API in VB and I was wonder how do you know when to use constants or how to declare variables in the function.
when its like (systeminfo as systeminfo???
------------------
Printable View
I have started using API in VB and I was wonder how do you know when to use constants or how to declare variables in the function.
when its like (systeminfo as systeminfo???
------------------
You must use a reference which gives you the exact declare wording, the value of any constants, and the usage of the function. One place to start is the API viewer that comes with VB. It’s in the Add-in menu selection. If it’s not listed than you have to use the Add-in manager.
A good on line API guide is at http://www.vbapi.com on the web
PLEASE CHECK OUT MICROSOFT DEVELOPERS LIBRARY.
It may difficult to get the answer from MSDN, But this site definately good & a lot of sample code as recommended by Frans C
Quote:
posted 03-09-2000 01:46 PM
If you feel really confident, you can try using microsofts cryptoapi. But actualy it is quite complex.
For an example:
http://members.xoom.com/_XMCM/KPDTeam/api/api113.htm
I would like to point out one thing here..
Though the API viewer suggests the declaration
for example :
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
You can even type
Declare Function GetWindowLong lib "User32" Alias "GetWindowLongA" (ByVal myhandle As Long , ByVal myIndex As Long )As Long
..i.e .. you can give your own names to parameter declaration. But what you should not is ..change the data type. i.e..you should not put integer or string for long.
In some cases a parameter is declared "As Any" in the microsoft API viewer, because it can be a long or a string. It is probably best in these cases to write a seperate function for each data type the parameter can take, so VB can do some data-type checking before compiling.
For example, the FindWindow API takes two parameters, both declared "As Any" if pasted from the API viewer, like so:
The two parameters are the name of the class and the name of the window. This function finds a window based on it's class or it's caption. You should supply a string value for one or the other, and a 0 (as long) for the value not supplied. Thus, you would declare two API functions taking different data types, like so:Code:Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
(ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
This way, VB can do a little validation for you at compile time instead of getting a run-time error if you happen to pass the wrong data type ("As Any" will accept any data type). This is what Dan Appleman suggests.Code:Declare Function FindWindowByClass Lib "user32.dll" Alias "FindWindowA" _
(ByVal lpClassName As String, ByVal lpWindowName As Long) As Long
Declare Function FindWindowByName Lib "user32.dll" Alias "FindWindowA" _
(ByVal lpClassName As Long, ByVal lpWindowName As String) As Long
What I usually do, in fact (as if I do this all the time :) ), is leave the initial declaration as microsoft has it, and then simply write two wrapper functions like so:
Just my 2 cents...Code:Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
(ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Public Function FindWindowByClass(sClassName As String) As Long
FindWindowByClass = FindWindow(sClassName, CLng(0))
End Function
Public Function FindWindowByTitle(sTitle As String) As Long
FindWindowByTitle = FindWindow(CLng(0), sTitle)
End Function
~seaweed