Microsoft KB Archive/815664

= How to obtain the NetBIOS and DNS computer names by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 815664

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft Visual C++ .NET 2002 Standard Edition

-





For a Microsoft Visual Basic .NET version of this article, see 302656.



For a Microsoft Visual C# .NET version of this article, see 303902.

IN THIS TASK

 * SUMMARY
 * Steps to Build the Sample
 * REFERENCES



SUMMARY
This step-by-step article describes how to obtain the name of the computer where your application is running. The following sample code shows how to obtain both the NetBIOS (simple) name and the fully qualified Domain Name System (DNS) computer name.

back to the top

Steps to Build the Sample
 Start Microsoft Visual Studio .NET 2002, Visual Studio .NET 2003, or Visual Studio 2005. On the File menu, point to New, and then click Project. In Visual Studio .NET 2002, click Visual C++ Projects under Project Types, and then click Managed C++ Application under Templates.

In Visual Studio .NET 2003, click Visual C++ Projects under Project Types, and then click Console Application (.NET) under Templates.

In Visual Studio 2005, click Visual C++ under Project Types, and then click Console Application under Templates. In the Name text box, type ComputerName, and then click OK. In Solution Explorer, double-click ComputerName.cpp to open the Code window.  Replace the existing code with the following code: // This is the main project file for Visual C++ application project // that is generated by using an Application Wizard.


 * 1) include &quot;stdafx.h&quot;


 * 1) using <system.dll>
 * 2) using <mscorlib.dll>
 * 3) include <tchar.h>

using namespace System; using namespace System::Net;

// This is the entry point for this application. int _tmain(void) {   String *result;

//Retrieve the NetBIOS name. result = System::Environment::get_MachineName;

//Display the results to the Console window. Console::WriteLine(S&quot;NetBIOS Name = {0}&quot;, result);

//Retrieve the DNS name. IPHostEntry* hostInfo ; hostInfo = System::Net::Dns::GetHostByName(S&quot;LocalHost&quot;); //Display the results to the Console window. Console::WriteLine(S&quot;DNS Name = {0}&quot;, hostInfo->HostName); return 0; } Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> Expand Configuration Properties, and then click General.</li> Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

</li> Press the CTRL+F5 key combination to build and run the application. The NetBIOS and DNS computer names are displayed in the Console window.</li></ol>

Note If you press CTRL+F5 to start the program, the Console window remains open after the code is compiled.

back to the top

<div class="references_section">