Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Easy way to get computer name

Status
Not open for further replies.

dekcool

Programmer
Joined
Oct 2, 2002
Messages
231
Location
PH
environ("COMPUTERNAME") DEK
 
???? ONLY returns a 'newline' for me. Perhaps you have added the VARIABLE "COMPUTERNAME" to your environment and set it to some value prior to this?

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 

Try:

Option Explicit

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function GetComputerName() As String
Dim lpBuffer As String
Dim Pos
lpBuffer = Space$(255)

GetComputerName lpBuffer, Len(lpBuffer)
Pos = InStr(lpBuffer, Chr$(0))
If Pos > 0 Then Sp_GetComputerName = Left$(lpBuffer, Pos - 1)

End Function [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
CCLINT,

You appear to be at least one cup (of coffee) short.

The API declaration and procedure name cannot be the same.

Otherwise it is at least close (also need to 'loose' the "sp_ or change the procedure name to include it (probably would be there with the next cup?).

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
yes of course you need variable to store the name

strName = environ("COMPUTERNAME") DEK
 

MichaelRed: Right so.

Came from copying and pasting.

However, I trust that anyone using any code posted in this forum(usually snippits or just ideas, and sometimes the ways that someone is actually using the code that they have posted is somewhat different) would have enough understanding to correct the minor typos, etc.

If not, then they need to ask.

If still not, then maybe they are the ones who are "at least one cup (of coffee) short".

Often, perfect or 100% correct code isn't posted, and also often missing error handling, or other points.
I am sure it also isn't expected.
(New) ideas, different methods and sample code is what makes this forum attractive, and not necessarily 100% syntax correct code.

Otherwise, if this isn't understood, then maybe code like this shouldn't be used at all until some basics are better understood.

I would, however, expect that code posted in a FAQ, on the other hand, would be more correct and somewhat more professional.



>The API declaration and procedure name cannot be the same.

It can be easily seen that the proceedure name should have been: Sp_GetComputerName
Anyone in doubt could ask, or, someone spotting these minor erros, could post a line of correction if they have doubt that some beginner may not know how to correct the error.

>(also need to 'loose' the "sp_"...
Wouldn't help, as then the problem being that two functions (the API function and Proceedure name) cannot be the same.

Thanks for pointing it out.

Again, the below code is just an idea of how the objective can be accomplished, and not necessarily an end product with 100% correctness:

Option Explicit

Private Declare Function GetComputerName Lib "kernel32" Alias "GetComputerNameA" (ByVal lpBuffer As String, nSize As Long) As Long

Public Function GetComputerName_pF() As String

Dim lpBuffer As String
Dim pos

lpBuffer = Space$(255)

GetComputerName lpBuffer, Len(lpBuffer)

pos = InStr(lpBuffer, Chr$(0))
If pos > 0 Then GetComputerName_pF = Left$(lpBuffer, pos - 1)

End Function [/b][/i][/u]*******************************************************
General remarks:
If this post contains any suggestions for the use or distribution of code, components or files of any sort, it is still your responsibility to assure that you have the proper license and distribution rights to do so!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top