PC Review


Reply
Thread Tools Rate Thread

How to define a name of a computer on C#?

 
 
Carl Daniel [VC++ MVP]
Guest
Posts: n/a
 
      28th Aug 2006
Dr. Zharkov wrote:
>
> Inform, please, where in a code on C# an error?
>


http://www.pinvoke.net/default.aspx/...puterName.html

-cd


 
Reply With Quote
 
 
 
 
Michael A. Covington
Guest
Posts: n/a
 
      28th Aug 2006
I see one obvious difference:

> Dim myComputerName As String = Space(30)


versus

> string myComputerName = "";


I don't really know how these things work, but I'd try changing that to:

string myComputerName = " ";

and see if it makes a difference.




"Dr. Zharkov" <valery-(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hello. To define a name of a computer in project VS 2005 after click of
> Button1 on Form1, on Visual Basic such code is used:
>
>
>
> Declare Auto Function GetComputerName Lib "kernel32.dll" _
>
> Alias "GetComputerName" (ByVal buf As String, _
>
> ByRef size As Integer) As Integer
>
>
>
> Private Sub Button1_Click(ByVal sender As System.Object, _
>
> ByVal e As System.EventArgs) Handles Button1.Click
>
> Dim myComputerName As String = Space(30)
>
> Dim Length As Integer = 30
>
> Dim ReturnValue As Integer
>
> ReturnValue = GetComputerName(myComputerName, Length)
>
> MsgBox("Name of my computer: " & myComputerName)
>
> End Sub
>
>
>
> In other same project VS 2005, I have copied this code on Visual C# as
> follows:
>
>
>
> [System.Runtime.InteropServices.DllImport("kernel32.dll")]
>
> public static extern int GetComputerName(string buf, ref int size);
>
>
>
> private void button1_Click(object sender, EventArgs e)
>
> {
>
> string myComputerName = "";
>
> int Length = 30;
>
> int ReturnValue;
>
> ReturnValue = GetComputerName(myComputerName, ref Length);
>
> MessageBox.Show("Name of my computer: " + myComputerName);
>
> }
>
>
>
> Building this code occurs without errors, however the name of a computer
> is
> not deduced.
>
> Inform, please, where in a code on C# an error?
>
> Beforehand thanks for the answer, Valeriy, Moskva, Russia.
>
>



 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      28th Aug 2006
Why use the extern, when you can use System.Environment.MachineName?

(mentioned in the article Carl linked to, but worth mentioning
explicitely.)

Marc

 
Reply With Quote
 
JTC ^..^
Guest
Posts: n/a
 
      28th Aug 2006

> private void button1_Click(object sender, EventArgs e)
>
> {
>

string myComputerName;

myComputerName =
System.Windows.Forms.SystemInformation.ComputerName;

MessageBox.Show("Name of my computer: " +
myComputerName);

>
> }


Regards JTC ^..^
 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      28th Aug 2006
Like so:

using System;
class Program {
static void Main() {
string machineName = System.Environment.MachineName;
}
}

Only needs a reference to System; there is a "forms" way to do it, but
that needs more references.

Marc

 
Reply With Quote
 
Marc Gravell
Guest
Posts: n/a
 
      28th Aug 2006
Why this obsession with calling the kernel32 function; in both your
examples *you already have the name*, so you don't need the PInvoke
step. At all.

Marc

 
Reply With Quote
 
Dr. Zharkov
Guest
Posts: n/a
 
      28th Aug 2006
Hello. To define a name of a computer in project VS 2005 after click of
Button1 on Form1, on Visual Basic such code is used:



Declare Auto Function GetComputerName Lib "kernel32.dll" _

Alias "GetComputerName" (ByVal buf As String, _

ByRef size As Integer) As Integer



Private Sub Button1_Click(ByVal sender As System.Object, _

ByVal e As System.EventArgs) Handles Button1.Click

Dim myComputerName As String = Space(30)

Dim Length As Integer = 30

Dim ReturnValue As Integer

ReturnValue = GetComputerName(myComputerName, Length)

MsgBox("Name of my computer: " & myComputerName)

End Sub



In other same project VS 2005, I have copied this code on Visual C# as
follows:



[System.Runtime.InteropServices.DllImport("kernel32.dll")]

public static extern int GetComputerName(string buf, ref int size);



private void button1_Click(object sender, EventArgs e)

{

string myComputerName = "";

int Length = 30;

int ReturnValue;

ReturnValue = GetComputerName(myComputerName, ref Length);

MessageBox.Show("Name of my computer: " + myComputerName);

}



Building this code occurs without errors, however the name of a computer is
not deduced.

Inform, please, where in a code on C# an error?

Beforehand thanks for the answer, Valeriy, Moskva, Russia.


 
Reply With Quote
 
Dr. Zharkov
Guest
Posts: n/a
 
      28th Aug 2006
Mr. Michael A. Covington. Many thanks for the recommendation, I have checked
up it, but she has not corrected a error.

Mr. Marc Gravell. Many thanks for the recommendation, but I do not know, how
it to use.



Mr. Carl Daniel.

Many thanks for very useful link. By means of this link, the code is carried
out without errors, shows a name of a computer and has such kind:



[System.Runtime.InteropServices.DllImport("Kernel32")]

static extern unsafe bool GetComputerName(byte* lpBuffer,

long* nSize);



private void button1_Click(object sender, EventArgs e)

{

byte[] buffor = new byte[512];

long size = buffor.Length;

unsafe

{

long* pSize = &size;

fixed (byte* pBuffor = buffor)

{

GetComputerName(pBuffor, pSize);

}

}

System.Text.Encoding textEnc =

new System.Text.ASCIIEncoding();

MessageBox.Show("Computer name: " +

textEnc.GetString(buffor));

}



Inform, please, and how this code to write down for Managed Visual C#?



Beforehand thanks for the answer, Valeriy, Moskva, Russia.




 
Reply With Quote
 
Dr. Zharkov
Guest
Posts: n/a
 
      28th Aug 2006
Mr. Marc Gravell. Many thanks for the help. With your help now such code on
C# correctly shows a name of a computer:



[System.Runtime.InteropServices.DllImport("kernel32.dll")]

public static extern int GetComputerName(string buf, ref int size);



private void button1_Click(object sender, EventArgs e)

{

string myComputerName = System.Environment.MachineName;

int Length = 30;

int ReturnValue;

ReturnValue = GetComputerName(myComputerName, ref Length);

MessageBox.Show("Name of my computer: " + myComputerName);

}



Mr. JazzTheCat. Many thanks for the help. With your help now such code on C#
correctly shows a name of a computer:



[System.Runtime.InteropServices.DllImport("kernel32.dll")]

public static extern int GetComputerName(string buf, ref int size);



private void button1_Click(object sender, EventArgs e)

{

string myComputerName =

System.Windows.Forms.SystemInformation.ComputerName;

int Length = 30;

int ReturnValue;

ReturnValue = GetComputerName(myComputerName, ref Length);

MessageBox.Show("Name of my computer: " + myComputerName);

}



Once again many thanks. Valeriy, Moskva, Russia.


 
Reply With Quote
 
Dr. Zharkov
Guest
Posts: n/a
 
      29th Aug 2006
Mr. Marc Gravell.

Thanks for your remark. Really, in my last letter the code can be written
down much easier:

private void button1_Click(object sender, EventArgs e)

{

MessageBox.Show("Name of my computer: " +

System.Environment.MachineName);

}



I want to understand with a technique of application of dynamic-link library
(DLL).

Excuse me, please, but at me the same request to inform, how to change a
code resulted below, that with help DLL and Managed C# this code deduced a
name of a computer (by analogy to the code resulted earlier on Visual
Basic)?



[System.Runtime.InteropServices.DllImport("kernel32.dll")]

public static extern int GetComputerName(string buf, ref int size);



private void button1_Click(object sender, EventArgs e)

{

string myComputerName = "";

int Length = 30;

int ReturnValue;

ReturnValue = GetComputerName(myComputerName, ref Length);

MessageBox.Show("Name of my computer: " + myComputerName);

}

Beforehand thanks for the answer, Valeriy, Moskva, Russia.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Is there a way to synchronize favorites between 2 computers? i.e. copy any favorites on computer A but not computer B to computer B and any on computer B but not computer A to computer A? Huck Microsoft Outlook Contacts 1 17th Sep 2006 10:54 AM
RIS and define diferent computer names Jorge Moura Windows XP Setup 6 3rd Jun 2006 11:38 PM
Where to define MSN =?Utf-8?B?cGV0ZXJfY29sb2duZQ==?= Windows XP Networking 1 19th Jan 2006 12:10 PM
Define Value Fernando Peixito Microsoft Access Macros 1 3rd Feb 2004 11:50 AM
Define DNS server address on client computer using command-line Alex Microsoft Windows 2000 DNS 0 5th Aug 2003 11:38 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 01:59 PM.