473,657 Members | 2,531 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.d ll" _

Alias "GetComputerNam e" (ByVal buf As String, _

ByRef size As Integer) As Integer

Private Sub Button1_Click(B yVal sender As System.Object, _

ByVal e As System.EventArg s) 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("ker nel32.dll")]

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

private void button1_Click(o bject 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 2164
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******** ******@TK2MSFTN GP05.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.d ll" _

Alias "GetComputerNam e" (ByVal buf As String, _

ByRef size As Integer) As Integer

Private Sub Button1_Click(B yVal sender As System.Object, _

ByVal e As System.EventArg s) 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("ker nel32.dll")]

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

private void button1_Click(o bject 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.Environm ent.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("Ker nel32")]

static extern unsafe bool GetComputerName (byte* lpBuffer,

long* nSize);

private void button1_Click(o bject 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.Enc oding textEnc =

new System.Text.ASC IIEncoding();

MessageBox.Show ("Computer name: " +

textEnc.GetStri ng(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(o bject sender, EventArgs e)

{
string myComputerName;

myComputerName =
System.Windows. Forms.SystemInf ormation.Comput erName;

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.Environm ent.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("ker nel32.dll")]

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

private void button1_Click(o bject sender, EventArgs e)

{

string myComputerName = System.Environm ent.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("ker nel32.dll")]

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

private void button1_Click(o bject sender, EventArgs e)

{

string myComputerName =

System.Windows. Forms.SystemInf ormation.Comput erName;

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(o bject sender, EventArgs e)

{

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

System.Environm ent.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("ker nel32.dll")]

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

private void button1_Click(o bject 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
1478
by: | last post by:
Is there a way to detect if the computer name has been changed in .NET?
2
4512
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 #define statements that associate GUIDs wih a friendly name. For e.g. consider the file zoo.h: ....
4
1748
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 like: 1) How to keep the installation date on the user's computer so that a simple uninstall and a complete deletion of the application's directory
42
5601
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
4289
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 system.windows.forms.dll inside an asp-dot-net code. any information here will help. TIA, z.
5
2289
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 the company name. Can someone help me plz? Thanks
6
7674
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 all files on my computer and cannot find how to retrieve this information in VB.Net 2005. This information is essential for our system implementation. I have tried: NetworkInformation.IPGlobalProperties.GetIPGlobalProperties but it does not...
0
1908
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 administrator or a domain administrator, but a regular domain user (with administrator privileges on the local machine) can't run the code successfully. They get error 1219, which I believe means "Multiple connections to a server or shared resource by the same...
18
1846
by: vivek | last post by:
What will happen if i replace a typedef with a #define?
0
8420
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8740
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8516
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7353
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4173
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2743
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.