Results 1 to 6 of 6

Thread: A few API questions

  1. #1

    Thread Starter
    Hyperactive Member MPrestonf12's Avatar
    Join Date
    Jun 1999
    Location
    NY
    Posts
    330

    Post

    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???


    ------------------


  2. #2
    New Member
    Join Date
    Feb 2000
    Posts
    12

    Post

    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


  3. #3
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Brooklyn NY USA
    Posts
    1,258

    Post

    PLEASE CHECK OUT MICROSOFT DEVELOPERS LIBRARY.

  4. #4
    PowerPoster Chris's Avatar
    Join Date
    Jan 1999
    Location
    K-PAX
    Posts
    3,238

    Post

    It may difficult to get the answer from MSDN, But this site definately good & a lot of sample code as recommended by Frans C


    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

  5. #5
    Addicted Member
    Join Date
    Feb 2000
    Posts
    224

    Post

    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.




  6. #6
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Post Sometimes you should change the data type

    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:
    Code:
    Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" _
        (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
    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 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
    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.

    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:
    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
    Just my 2 cents...

    ~seaweed

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width