Results 1 to 5 of 5

Thread: API function

  1. #1
    Join Date
    Apr 2002
    Posts
    168

    Unanswered: API function

    Declare Function abGetSystemDirectory Lib "kernel32" _
    Alias "GetSystemDirectoryA" (ByVal lpBuffer As String, _
    ByVal nSize As Long) As Long

    Private Sub Command8_Click()

    Dim strBuffer As String
    Dim intLength As Integer
    Dim strDirectory As String

    strBuffer = Space$(160)

    intLength = abGetSystemDirectory(strBuffer, Len(strBuffer))

    strDirectory = Left(strBuffer, intLength)
    MsgBox strDirectory
    End Sub


    I use this function I got from a book to get the windows directory. What I am confused is strDirectory has the value of "C:\WINNT\System32" and its value comes from strBuffer while strBuffer is only passed to abGetSystemDirectory ByValue, so its value should not change. Did I miss some thing ? Thanks.

  2. #2
    Join Date
    Feb 2002
    Posts
    2,232
    The GetSystemDirectory api function will return c:\winnt\system32. Perhaps you wanted to use GetWindowsDirectory instead. What directory are you trying to retrieve ?

  3. #3
    Join Date
    Apr 2002
    Location
    Germany
    Posts
    228
    I'm not really sure about that but it's that way with a lot of api calls. This is what I thought of (feel free to correct it):

    You gotta consider you are actually calling a C function. In C there is no seperate string data type. A string in C is a char (i.e. byte) array terminated with binary 0 (\0 escape sequence in C). So even if you wanted to pass a string ByVal you can't as nearly all C functions expect a pointer to the first element of a char array as parameter for a string. I don't know how the stuff is working with ASCII -> Unicode translation and stuff then but that's what I see from being a C/C++ programmer.

    I'd be interested in a bit more insight on how VBA calls C functions as well.

  4. #4
    Join Date
    Feb 2002
    Posts
    2,232
    Strings in VB are BSTR data types - which is just a pointer to a memory location with some header information and the actual string. So strings are not character arrays but pointers to character arrays. So byVal xxxx as string is actually functioning correctly - passing the guts of the BSTR which is a pointer.

    See the following article in MSDN - "Passing Strings to a DLL Procedure"

    Last edited by rnealejr; 05-10-02 at 14:10.

  5. #5
    Join Date
    Apr 2002
    Posts
    168
    That's true.

    I read a book about calling DLL procedure. I just found out, that ByVal in its parameter actually takes the address, not just the value. The keyword ByVal only means that the string is null-terminated because C language is used. Unlike when we create our own function using ByVal where it only passes the value.

    Thanks

Posting Permissions

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