Solved

HostName

Posted on 2001-07-31
5
176 Views
Last Modified: 2006-11-17
I want to get the IP Address of the machine where my Application  run ((As String)) ? I found gethostbyname,gethostname but I unfamilier with them?
0
Comment
Question by:aminmohsalih
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 5

Expert Comment

by:proskig
ID: 6337736
Do you need dotted IP addresses xxx.xxx.xxx.xxx
Machine name? or your dns name (all or primary)?
0
 

Author Comment

by:aminmohsalih
ID: 6339483
any one IP addresses xxx.xxx.xxx.xxx or DNS Name
0
 
LVL 5

Expert Comment

by:proskig
ID: 6339737
So, on UNIX you can probably use

char hostName[MAXHOSTNAMELEN];
gethostname(hostName,MAXHOSTNAMELEN);


0
 

Accepted Solution

by:
NizamLapis13 earned 25 total points
ID: 6371150


In unix,
 
 int gethostname(chat *name, int namelen);

will work,

The namelen is the length of the array passed(char *name);
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6372609
// Here are two functions I used for getting the IP address.
//They may require some modification based on your needs
#include <afx.h>

//////////////////////////////////////////////////////////////////////////////
// Function name     : GetCardAddress
// Description          : Gets the name of the network card
// Return type          : CString reference
// Argument               : void
static     CString&     GetCardAddress(void)
{
     static     CString     csCardAddress;
     auto          DWORD          dwCardLength          =     128;
     auto          ULONG          ulKeyType               =     REG_SZ;
     static     HKEY          hKey                         =     NULL;
     csCardAddress.GetBufferSetLength(dwCardLength);
     if(ERROR_SUCCESS ==
          RegOpenKeyEx(HKEY_LOCAL_MACHINE,
          "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\NetworkCards\\1",
          0, KEY_READ, &hKey))
          {
          ///////////////////////////////////////////////
          // Put the Card Address in the CString
          if (ERROR_SUCCESS != RegQueryValueEx(
               hKey,   "ServiceName",
               NULL,     &ulKeyType,     (BYTE *)(LPCTSTR)csCardAddress, &dwCardLength))
               {
               //////////////////////////////////////////////
               // If we can't get the ServiceName return NULL
               csCardAddress.Empty();
               return csCardAddress;
               }
          RegCloseKey(hKey);
          }
     ASSERT(csCardAddress.GetLength());
     return     csCardAddress;
}

//////////////////////////////////////////////////////////////////////////////
// Function name     : GetIPAddress
// Description          : Gets the IP address
// Return type          : CString Reference
// Argument               : void
CString&     GetIPAddress(void)
{
     auto          CString     csFormat;
     static     CString     csIPAddress;
     auto          DWORD          dwIPAddressLength     =     15;
     auto          ULONG          ulIPKeyType               =     REG_MULTI_SZ;
     static     HKEY          hKey                         =     NULL;
     //
     csIPAddress.GetBufferSetLength(dwIPAddressLength);

     hKey=NULL;
     //
     auto     CString     csCardAddress =     GetCardAddress();
     if (!csCardAddress.GetLength())
          {
          return     csIPAddress;
          }

     csFormat.Format(
          "SYSTEM\\ControlSet001\\Services\\%s\\Parameters\\Tcpip",
          csCardAddress);
     //
     if(ERROR_SUCCESS==
          RegOpenKeyEx(HKEY_LOCAL_MACHINE,csFormat,0,KEY_READ,&hKey))
          {
          ///////////////////////////////////////////////
          // Put the IPAddress into the CString
          if (ERROR_SUCCESS != RegQueryValueEx(
               hKey,   "IPADDRESS",
               NULL,     &ulIPKeyType,     (BYTE *)(LPCTSTR)csIPAddress, &dwIPAddressLength))
               {
               //////////////////////////////////////////////
               // If we can't get the IP Address return NULL
               csIPAddress.Empty();
               return csIPAddress;
               }
          RegCloseKey(hKey);
          }
     ASSERT(0 != csIPAddress.GetLength());
     return     csIPAddress;
}

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

733 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question