Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get computer name 1

Status
Not open for further replies.

mactonio

Programmer
Nov 18, 2003
55
US
Hi,
does anyone know what function i can use in my program to get the computer name that the program is running on.

i was thinking of using getenv but there are no environment set for computer name so trying to find am alternative.

thanks
 
You can use the GetComputerName API.
Example:
Code:
#include <windows.h>

int WINAPI WinMain ( HINSTANCE, HINSTANCE, LPSTR, int )
{
  char  ComputerName [MAX_COMPUTERNAME_LENGTH + 1];
  DWORD cbComputerName = sizeof ( ComputerName );
  if ( GetComputerName ( ComputerName, &cbComputerName ))
     { MessageBox ( NULL, ComputerName, "Name of this computer:",
                    MB_OK | MB_ICONINFORMATION ); } }

Marcel
 
If you are using winsock in dev c++, u can use gethostname function which is cross platform:

Code:
#include <iostream>
#include <winsock.h>

using namespace std;

int main(int argc, char *argv[])
{
    WSADATA wsa_data;
    /* Load Winsock 2.0 DLL */
    if (WSAStartup(MAKEWORD(2, 0), &wsa_data) != 0)
    {
    cerr << "WSAStartup() failed" << endl;
    return (1);
    }
    
    char myhostname[256] ;
    int rc  = gethostname(myhostname, sizeof myhostname);
    cout << myhostname << endl ;
    
    WSACleanup(); /* Cleanup Winsock */
    
    system("PAUSE");
    return EXIT_SUCCESS;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top