Getting computername

M

Mike Behrendt

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.
 
N

Newbie Coder

Mike,

Why not use this way instead?

MessageBox.Show(Environment.MachineName.ToString)

Newbie Coder
 
H

Herfried K. Wagner [MVP]

Newbie Coder said:
Why not use this way instead?

MessageBox.Show(Environment.MachineName.ToString)

.... or 'SystemInformation.ComputerName'.
 
B

Branco Medeiros

Mike Behrendt wrote:
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).
<snip>

Besides what others replied, notice that your declaration is incorrect
regarded the API signature:

<code>
Private Declare Ansi Function GetComputerName _
Lib "kernel32" _
Alias "GetComputerNameA" ( _
ByVal Name As String, _
ByRef CharCount As Integer _
) As Integer

'Option Strict is your friend!
Private Const MAX_COMPUTERNAME_LENGTH As Integer = 15


Sub Demo_GetComputerName()
Dim Name As New String(" "c, MAX_COMPUTERNAME_LENGTH)
Dim Count As Integer = Name.Length
Dim Result As Integer

Result = GetComputerName(Name, Count)
If Result <> 0 Then
MsgBox(Name)
End If
End Sub
</code>

HTH.

Regards,

Branco.
 
J

Jay B. Harlow [MVP - Outlook]

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...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top