how do i find the name of the computer using my program, and the current username that is logged into that computer?
thanks,
greg
Printable View
how do i find the name of the computer using my program, and the current username that is logged into that computer?
thanks,
greg
Code:Private Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Private Sub Command1_Click()
Dim sName As String
Dim sComputer As String
sName = Space$(255)
sComputer = Space$(255)
GetComputerName sComputer, 255
GetUserName sName, 255
sComputer = Left(sComputer, InStr(1, sComputer, vbNullChar) - 1)
sName = Left(sName, InStr(1, sName, vbNullChar) - 1)
Print sName & "-" & sComputer
End Sub
Thanks Megatron...
I saw those API calls in the API View, but was under the impression that they returned a long.. how is it that if it does in fact return a long, the result is actually my terminal name and username? Also, do you need to pass it a string that is 255 blanks, could you just past it a 0-length string? and i take it the 255 you are passing is the lenght of that buffer...
Thanks
-Matt
No. the 255 blanks is the buffer, meaning we have space for up to 255 different characters. If we used 0, then that would mean we have space for 0 characters.
As for the resturn value: It's a Long because it will return a 1 when the function suceeds, or a 0 when the function fails.
The return value has nothing to do with the actual name being copied to the buffer. the string and the size are both modified in the function (by reference) so no return value is needed (although it would make our lives easier).
Thanks, that makes sense.. thats why when you call the API, you arent assigning the return value to any long variable... i guess this is true of most API's
Thanks for the help!
-Matt
I think MetallicaD means why not
Since they are both declared as function that retunrs a value?Code:Call GetComputerName sComputer, 255
Call GetUserName sName, 255
I agree, at very least in terms of programming style. And it might be cool to check return values as a matter of from.
Or am I missing something major?
Checking the return values is either a matter of preference or it's essential (depending on the situation). It you simply don't care about the return value, you could use:
But, if the case arises (and sometimes it does) when you need to check if the function suceeds or fails (to report an error message or something) then use:Code:Myfunction Arg1, Arg2
'or
Call MyFunction(Arg1, Arg2)
Code:lResult = MyFunction(Arg1, Arg2)
If lResult = 0 Then MsgBox "Error:"