First of all: If you transfer a MapBasic string to a Win API dll, it must be By Reference, i.e NO "ByVal" keyword before the string declaration in the function declaration.
Right: Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (lpBuffer As String, nSize As Integer) As Integer Wrong: Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Integer) As Integer This error is very common using Win API's in MapBasic, because you probably find some example on the web using the relevant API call in _Visual Basic_ and edit the example to MapBasic. In Visual Basic, You must have the "ByVal" keyword on strings to get the VB runtime to produce a null - terminated string _address_ as an argument to a dll (a "C" string like most Win API function will accept as strings). I love it when Bill G. is being logical and straightforward in designing computer languages.... You can use the following rules to convert a VB win api example to MapBasic : Change "Integer" to "SmallInt" Change "LongInt" to "Integer" Change "Double" to "Float" Change "ByVal String" to "String" in the VB Win API declarations There are other rules, but above edit-rules will take care of 90 % of the conversion problems regards Bo Thomsen -----Oprindelig meddelelse----- Fra: Jay C Russell [mailto:[EMAIL PROTECTED] Sendt: 14. juli 2004 16:39 Til: [EMAIL PROTECTED] Emne: MI-L GetUserName Api Problem >From a previous post by Jacques Paris, I created a program to return the current user name logged into Windows. I am running Windows 2k with Mapinfo 7.0. When I test the function the slength returned is of the correct length, but the username value is blank. Help appreciated as always! Source code included below. Thanks, Jay Russell Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Integer) As Integer Sub FindUser Dim username As String ' receives name of the user Dim slength As Integer ' length of the string Dim retval As Integer ' return value ' Create room in the buffer to receive the returned string. username = Space$(255) ' room for 255 characters slength = 255 ' initialize the size of the string ' Get the user's name and display it. retval = GetUserName(username, slength) ' slength is now the length of the returned string username = Left$(username, slength - 1) ' extract the returned info from the buffer ' (We subtracted one because we don't want the null character in the trimmed string.) Print "The name of the current user is "+ username End Sub 'FindUser --------------------------------------------------------------------- List hosting provided by Directions Magazine | www.directionsmag.com | To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Message number: 12607 --------------------------------------------------------------------- List hosting provided by Directions Magazine | www.directionsmag.com | To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] Message number: 12627
