473,320 Members | 1,949 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,320 software developers and data experts.

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

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.
Aug 28 '06 #1
9 2144
Dr. Zharkov wrote:
>
Inform, please, where in a code on C# an error?
http://www.pinvoke.net/default.aspx/...puterName.html

-cd
Aug 28 '06 #2
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" <va************@mtu-net.ruwrote in message
news:ur**************@TK2MSFTNGP05.phx.gbl...
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.


Aug 28 '06 #3
Why use the extern, when you can use System.Environment.MachineName?

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

Marc

Aug 28 '06 #4
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.


Aug 28 '06 #5
private void button1_Click(object sender, EventArgs e)

{
string myComputerName;

myComputerName =
System.Windows.Forms.SystemInformation.ComputerNam e;

MessageBox.Show("Name of my computer: " +
myComputerName);
>
}
Regards JTC ^..^
Aug 28 '06 #6
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

Aug 28 '06 #7
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.ComputerNam e;

int Length = 30;

int ReturnValue;

ReturnValue = GetComputerName(myComputerName, ref Length);

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

}

Once again many thanks. Valeriy, Moskva, Russia.
Aug 28 '06 #8
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

Aug 28 '06 #9
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.
Aug 28 '06 #10

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

Similar topics

3
by: | last post by:
Is there a way to detect if the computer name has been changed in .NET?
2
by: psundara | last post by:
Hi, I'm facing a peculiar problem of finding a way to interpret header information in a smart way. I have this header file that is shared by many users, which contains, among things, a few...
4
by: GiladP1 | last post by:
Hi, I want to copy protect my application. I want to limit its use for a given period of time, say 30 days, after which a license key will be needed. There are various issues to consider here...
42
by: baumann | last post by:
hi all, typedef int (*pfunc)(int , int); pfunc a_func; i know it's ok, but how can define a_func without typedef statement? thanks .
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...
5
by: ThunderMusic | last post by:
Hi, I want to find the computer name and the company name from the network indentification informations. For the computer name, I found System.Net.Dns.GetHostName(), but I fgound nothing for...
6
by: Spyder | last post by:
When you go to "MyComputer" and get properties and select "Computer Name", you get the Domain or Workgroup a computer belongs to. I have looked thru MSDN library, the Internet, the Registry and in...
0
by: BrianT | last post by:
I'm trying to build code that allows the computer name to be changed, then asks the user to reboot to make the change affective. I got the code working when logged in as the local computer...
18
by: vivek | last post by:
What will happen if i replace a typedef with a #define?
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.