[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 184
  • Last Modified:

HostName

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
aminmohsalih
Asked:
aminmohsalih
1 Solution
 
proskigCommented:
Do you need dotted IP addresses xxx.xxx.xxx.xxx
Machine name? or your dns name (all or primary)?
0
 
aminmohsalihAuthor Commented:
any one IP addresses xxx.xxx.xxx.xxx or DNS Name
0
 
proskigCommented:
So, on UNIX you can probably use

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


0
 
NizamLapis13Commented:


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

will work,

The namelen is the length of the array passed(char *name);
0
 
TriskelionCommented:
// 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now