473,385 Members | 1,908 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Getting computername

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.

Jan 21 '07 #1
4 3294
Mike,

Why not use this way instead?

MessageBox.Show(Environment.MachineName.ToString)

Newbie Coder
Jan 21 '07 #2
"Newbie Coder" <ne**********@pleasespamme.comschrieb:
Why not use this way instead?

MessageBox.Show(Environment.MachineName.ToString)
.... or 'SystemInformation.ComputerName'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>
Jan 21 '07 #3
Mike Behrendt wrote:
<snip>
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.

Jan 21 '07 #4
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.
Jan 21 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: dthmtlgod | last post by:
I am attempting to retrieve the computer name of a remote user accessing our websever. I have tried the following and both don't work. Any ideas? Returns the webserver computer name Set...
7
by: Mindy Geac | last post by:
Hello, i'm new with ASP! How can I display the hosting computername on the website, We have 2 servers and I want to know which server I'm getting the asp pages from. thnx, Mindy
2
by: Todd | last post by:
Is there a method I can use in C# that will set the ComputerName? Any Help would be appreciated. Thanks Todd
4
by: Saradhi | last post by:
Hi All, Can any one tell me the way to check whether a given user name is a member of domain users or not. I do not know the domain name as well. I should retreive from the logged in user, in...
2
by: Joseph | last post by:
Has anyone had any experience gathering the local computername through a web application with no client side program. I've been researching different avenues and have been coming up blank so far. ...
5
by: z. f. | last post by:
i need to get the computer name from an aspx page. i use System.Windows.Forms.SystemInformation.ComputerName() and it's working fine, but in second thought, it might not be recomended to use the...
0
by: Frank Rosario | last post by:
Hello all; in need of some assistance. I am currently writing some software for our companies intranet, and I need for it to specifically access other machines on the network's Performance...
0
by: amjad | last post by:
Dear All, I am using Visual Studio beta 2005 beta 2 with ASP.Net 2.0 and C#.I run the application and it work correctly with http://localhost:port/projectname/page but not with...
1
by: usman211 | last post by:
i m using the following code for IP and ComputerName, it run accurately with our LAN but when i run it from some remote/hosting servers(like godaddy where the site is hosted) it get the IP of my...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.