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



The information in this article applies to:

  • Microsoft Visual C++ 2005 Express Edition
  • Microsoft Visual C++ .NET (2003)
  • Microsoft Visual C++ .NET (2002)

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

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

  1. Start Microsoft Visual Studio .NET 2002, Visual Studio .NET 2003, or Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.
  3. 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.
  4. In the Name text box, type ComputerName, and then click OK.
  5. In Solution Explorer, double-click ComputerName.cpp to open the Code window.
  6. 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.
    
    #include "stdafx.h"
    
    #using <system.dll>
    #using <mscorlib.dll>
    #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"NetBIOS Name = {0}", result);
    
    	//Retrieve the DNS name. 
    		IPHostEntry* hostInfo ; 
    	hostInfo = System::Net::Dns::GetHostByName(S"LocalHost"); 
    	
    	//Display the results to the Console window.
    	Console::WriteLine(S"DNS Name = {0}", 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:
    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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.
    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

  7. Press the CTRL+F5 key combination to build and run the application. The NetBIOS and DNS computer names are displayed in the Console window.
Note If you press CTRL+F5 to start the program, the Console window remains open after the code is compiled.

back to the top

REFERENCES

For more information about the Environment Class, visit the following Microsoft Developer Network (MSDN) Web site: For more information about NetBIOS, visit the following MSDN Web site:back to the top

Modification Type:MajorLast Reviewed:1/4/2006
Keywords:kbNetBIOS kbLangCPP kbHOWTOmaster KB815664 kbAudDeveloper kbAudITPRO