Solved

HostName

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now