?
Solved

HostName

Posted on 2001-07-31
5
Medium Priority
?
182 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 100 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
Suggested Courses

650 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