|
-
May 22nd, 2001, 03:13 PM
#1
Thread Starter
Lively Member
finding the computer name
how do i find the name of the computer using my program, and the current username that is logged into that computer?
thanks,
greg
-
May 22nd, 2001, 03:22 PM
#2
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
-
May 22nd, 2001, 03:33 PM
#3
Hyperactive Member
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
[vbcode]
'*****************************
MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
'*****************************
[/vbcode]
-
May 22nd, 2001, 04:35 PM
#4
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.
-
May 22nd, 2001, 04:43 PM
#5
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).
-
May 22nd, 2001, 04:52 PM
#6
Hyperactive Member
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
[vbcode]
'*****************************
MsgBox "MCD :: [email protected]", vbInformation + vbOKOnly, "User"
'*****************************
[/vbcode]
-
May 22nd, 2001, 04:53 PM
#7
I think MetallicaD means why not
Code:
Call GetComputerName sComputer, 255
Call GetUserName sName, 255
Since they are both declared as function that retunrs a value?
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?
-
May 22nd, 2001, 05:43 PM
#8
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:
Code:
Myfunction Arg1, Arg2
'or
Call MyFunction(Arg1, Arg2)
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:
lResult = MyFunction(Arg1, Arg2)
If lResult = 0 Then MsgBox "Error:"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|