Solved

HostName

Posted on 2001-07-31
5
175 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
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Retrieve PID of MicrosoftEdge.exe with GetForegroundWindow() 6 177
Add values of each row in an array 3 63
Safe conversion? 4 72
How do i run a c++ file? 15 46
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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 how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

809 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