Results 1 to 3 of 3

Thread: [RESOLVED] byval confusion

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    90

    Resolved [RESOLVED] byval confusion

    Hi all,
    I am learning to use API's but feel a bit confused with the way "ByVal" is used in the function Declare statement.By definition of a function if "ByVal" is used then the original argument is not affected.Here's my examle:

    Code:
    Option Explicit
    
    Private Declare Function GetWindowsDirectory Lib "kernel32" Alias "GetWindowsDirectoryA" (ByVal lpBuffer As String, ByVal nSize As Long) As Long
    
    
    Private Sub Command1_Click()
    Dim retstr  As String * 255
    Dim retval  As Long
    
    retstr = "hello world"
    retval = GetWindowsDirectory(retstr, 255)
    
    Label1.Caption = retstr
    End Sub
    This obviously generates the output "C"\WINDOWS" as expected.

    My question is why should the string variable 'retstr' contain anything other than "hello world" since the argument is being passed as "ByVal"?

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: byval confusion

    When passing a string by value to an API function, VB automatically passes the pointer to that string as if it were passed by reference.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jul 2007
    Posts
    90

    Re: byval confusion

    Thanks ...that clears my confusion.

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