Mike,
As Newbie & Herfried suggests, why reinvent the wheel, there are any number
of ways in .NET to get the computer name, such as My.Computer.Name.
To answer your question your code has 2 issues:
1. did not initialize the string before calling the API, the API expects a
buffer to return the name in.
2. Long is 64-bit in VB.NET, the GetComputerName API returns a BOOL (aka a
Boolean).
Code is nearby 1:1 from Internet-tutorials.
The code is VB6, you need to find a VB.NET sample. I would recommend looking
here:
http://www.pinvoke.net
Which includes many/most Win32 API calls in C# & VB. However not all APIs
are in both languages. Unfortunately GetComputerName happens not to have a
VB syntax...
--
Hope this helps
Jay B. Harlow [MVP - Outlook]
..NET Application Architect, Enthusiast, & Evangelist
T.S. Bradley -
http://www.tsbradley.net
"Mike Behrendt" <Mi*****@gmx.dewrote in message
news:11**********************@q2g2000cwa.googlegro ups.com...
Hey @all!
option explicit on
Private Declare Function GetComputerName Lib "kernel32" Alias _
"GetComputerNameA" (ByVal lpBuffer As String) As Long
Private Const MAX_COMPUTERNAME_LENGTH = 15
Sub Demo_GetComputerName()
Dim strBuffer As String
Dim lngResult As Long
lngResult = GetComputerName(strBuffer)
If lngResult = 1 Then
MsgBox(strBuffer)
End If
End Sub
gives me under .net 2 (VB 2005 Express) in row
lngResult=GetComputername(strbuffer) a
"System.AccessViolationException"-error (access on saved memory).
(translated by me from german version)
Code is nearby 1:1 from Internet-tutorials.
Where is the error?
greets,
Mike.