|
-
Jun 6th, 2001, 10:25 AM
#1
Thread Starter
Addicted Member
GetComputerName
Every time i try to run GetComputerName my system crashes. Has anyone had this before? Does anyone know why this is happening?
TIA
Due to the energy crisis, the light at the end of the tunnel has been turned off.
Sorry for any inconvenience this may cause
-
Jun 6th, 2001, 10:27 AM
#2
Thread Starter
Addicted Member
mistake!!! Only VB6 crashes, soz
Due to the energy crisis, the light at the end of the tunnel has been turned off.
Sorry for any inconvenience this may cause
-
Jun 6th, 2001, 11:06 AM
#3
Maybe you're using it wrong. Post your code up.
-
Jun 6th, 2001, 11:15 AM
#4
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
-
Jun 7th, 2001, 04:11 AM
#5
Thread Starter
Addicted Member
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
Due to the energy crisis, the light at the end of the tunnel has been turned off.
Sorry for any inconvenience this may cause
-
Jun 7th, 2001, 04:37 AM
#6
Thread Starter
Addicted Member
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)
Due to the energy crisis, the light at the end of the tunnel has been turned off.
Sorry for any inconvenience this may cause
-
Jun 7th, 2001, 04:43 AM
#7
Thread Starter
Addicted Member
-
Jun 7th, 2001, 08:10 AM
#8
Addicted Member
Why spaces ?
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
-
Jun 7th, 2001, 08:22 AM
#9
Addicted Member
...continued...
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.
-
Jun 7th, 2001, 02:49 PM
#10
It's not necessary to fill the name$ variable with spaces; you could fill it in with nulls (ASCII code 0).
You could fill them with whatever you want, just make sure they're filled.
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
|