Every time i try to run GetComputerName my system crashes. Has anyone had this before? Does anyone know why this is happening?
TIA
Printable View
Every time i try to run GetComputerName my system crashes. Has anyone had this before? Does anyone know why this is happening?
TIA
mistake!!! Only VB6 crashes, soz
Maybe you're using it wrong. Post your code up.
Here's some code that should work.
VB Code:
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 iLength As Long sName = Space$(255) iLength = GetComputerName(sName, 255) sName = Left$(sName, iLength) MsgBox sName End Sub
thanks for your help, what i wasnt doing was filling the string with spaces. (why do i have to do that?)
little mod to the left bit (why left$?) and it was working fine.
thanks Alex.
Dim sName As String
Dim iLength As Long
sName = Space$(255)
iLength = GetComputerName(sName, 255)
sName = Left$(sName, Len(sName) - iLength)
Label2.Caption = sName
for some reason the length came out as one, this did seem abnormal but all i needed to do was take the null terminator off the string (i think)
stupidity strike me again!!!
i think this is how it should have been
Dim sName As String
Dim iLength As Long
Dim result As Long
sName = Space$(255)
iLength = 255
result = GetComputerName(sName, iLength)
sName = Left$(sName, iLength)
Label2.Caption = sName
:D :D :D :o :D :D :D
It's not necessary to fill the name$ variable with spaces; you could fill it in with nulls (ASCII code 0).
e.g.
Dim sName as String
It is absolutelly necessary though to
Sorry about the unfinished message. Here it goes :
Dim sName as String
sName=string(255,0)
(maybe not char 0 but char let's say 20)
this way, a **constant** memory block of 255 chars in size is allocated to a variable (sName) and can be safely passed to the API call. On the other hand, a variable with no initialization occupies dynamically the memory, say : sName=sName+"hello"
All similar API calls I know of require this.
I am not sure if I made myself clear, maybe reading some of the C basics (LPSTR compared to BSTR in Visual Basic) would help.
You could fill them with whatever you want, just make sure they're filled.Quote:
It's not necessary to fill the name$ variable with spaces; you could fill it in with nulls (ASCII code 0).