MFC and IP problem

Hello,

I have a simple mfc application(exe) and i want to know If it is
possible to get back the IP address of a machine which is to connect
has my PC by ethernet and to show it in a IP case in my MFC
application.

Thank you

0
john.ambou (29)
4/13/2006 9:30:53 AM
vc.mfc 33608 articles. 0 followers. Follow

12 Replies
628 Views

Similar Articles

[PageSpeed] 42

Not very clear what are you talking about. Do you want to
know the local IP address? or may be address of different
PC in the same LAN?


<john.ambou@free.fr> wrote in message news:1144920653.682354.258970@z34g2000cwc.googlegroups.com...
> Hello,
>
> I have a simple mfc application(exe) and i want to know If it is
> possible to get back the IP address of a machine which is to connect
> has my PC by ethernet and to show it in a IP case in my MFC
> application.
>
> Thank you
> 


0
voidcoder (31)
4/13/2006 10:15:17 AM
john.ambou@free.fr napisał(a):
> Hello,
> 
> I have a simple mfc application(exe) and i want to know If it is
> possible to get back the IP address of a machine which is to connect
> has my PC by ethernet and to show it in a IP case in my MFC
> application.
> 
> Thank you
> 
Hi,
sth like this may help:

struct WSAData wsaData;
WSAStartup(MAKEWORD(2, 2), &wsaData);
char name[100];
::memset(name, 0, 100);
::gethostname(name,100);
hostent* host = ::gethostbyname(name);

for(int i = 0; host->h_addr_list[i]; i++)
	std::cout << std::endl << inet_ntoa( *( (in_addr*) host->h_addr_list[i]));	
		
WSACleanup();



Don't forget to include Winsock2.h


T.
0
tomek_czy (2)
4/13/2006 10:19:52 AM
If you are asking for IP address of your machine, try
GetHostName/GetHostByName. 

---
Ajay

0
ajaykalra (6842)
4/13/2006 10:20:03 AM
I want to have IP adresse of a other PC who is in the same LAN

0
john.ambou (29)
4/13/2006 1:08:01 PM
john.ambou@free.fr napisał(a):
> I want to have IP adresse of a other PC who is in the same LAN
> 
My sample is good for you - just send to :gethostbyname(char*)
name of computer ( it may be from web or LAN) - system do the job
for you.

T.
0
tomek_czy (2)
4/13/2006 1:14:18 PM
WHen i used gethost name i have a problem.
This is my source code :

char buf[100];
gethostname(buf,100);
messagebox(buf);

but in the messagebox, we don't get something that looks like a host
name or IP adress

What is the problem ?

0
john.ambou (29)
4/13/2006 4:25:59 PM

"john.ambou@free.fr" wrote:

> WHen i used gethost name i have a problem.
> This is my source code :
> 
> char buf[100];
> gethostname(buf,100);
> messagebox(buf);
> 
> but in the messagebox, we don't get something that looks like a host
> name or IP adress
> 
> What is the problem ?
> 
> 

Examine the return code from gethostname() and then call WSAGetLastError() 
if it is not zero.  You'll probably get 10093 as the error: WSANOTINITIALISED.

Call WSAStartup() first.  Below is a quick console app (it needs to link to 
Ws2_32.lib).  Comment out the WSAStartup() line and it prints "Error 10093"; 
leave the line in and it prints the computer's name.

----------------------------------
#include "stdafx.h"
#include "Winsock2.h"

int _tmain(int argc, _TCHAR* argv[])
{
	char	buffer[100] = {0};
	int		sts;
	WSADATA	wsaData;

	sts = WSAStartup(2, &wsaData);
	sts = gethostname(buffer, sizeof buffer);
	if(sts)
	{
		sts = WSAGetLastError();
		printf("Error %d\n", sts);
	}
	else
	{
		printf("%s\n", buffer);
	}
	Sleep(3000);
	return 0;
}
-------------------------------------------

Unless I tell my firewall that this app is always OK, it takes a LONG time 
before anything happens.  

0
Okie (47)
4/14/2006 12:09:01 AM
Thank you

But when i have hte hostname, how i do to have the IP adress ??

Thanks

0
john.ambou (29)
4/14/2006 12:01:37 PM
I use this source code but i find only 127.0.0.1 but i want hte IP
network who can find in the configuration panel.


 PHOSTENT phe;
 int count;
 int dwSize;
 int errrr;
Char IP[10];
 WORD wVersionRequested;
 WSADATA wsaData;
 int err;
 wVersionRequested = MAKEWORD( 2, 2 );
 err = WSAStartup( wVersionRequested, &wsaData );

 dwSize = sizeof(IP);
    errrr=gethostname(IP, dwSize);

  phe = gethostbyname(IP);
  if ((phe = gethostbyname( IP )) != NULL)
  {
   count = 0;
   while (phe->h_addr_list[count] != NULL)
   {
     sprintf( IP, "%u.%u.%u.%u",  (unsigned char)
phe->h_addr_list[count][0], (unsigned char) phe->h_addr_list[count][1],
(unsigned char) phe->h_addr_list[count][2], (unsigned char)
phe->h_addr_list[count][3]);
      count++; 
    } 
  } 
  MessageBox(IP);

0
john.ambou (29)
4/14/2006 1:55:48 PM
"john.ambou@free.fr" wrote:

> I use this source code but i find only 127.0.0.1 but i want hte IP
> network who can find in the configuration panel.
> 
> 
>  PHOSTENT phe;
>  int count;
>  int dwSize;
>  int errrr;
> Char IP[10];
>  WORD wVersionRequested;
>  WSADATA wsaData;
>  int err;
>  wVersionRequested = MAKEWORD( 2, 2 );
>  err = WSAStartup( wVersionRequested, &wsaData );
> 
>  dwSize = sizeof(IP);
>     errrr=gethostname(IP, dwSize);
> 
>   phe = gethostbyname(IP);
>   if ((phe = gethostbyname( IP )) != NULL)
>   {
>    count = 0;
>    while (phe->h_addr_list[count] != NULL)
>    {
>      sprintf( IP, "%u.%u.%u.%u",  (unsigned char)
> phe->h_addr_list[count][0], (unsigned char) phe->h_addr_list[count][1],
> (unsigned char) phe->h_addr_list[count][2], (unsigned char)
> phe->h_addr_list[count][3]);
>       count++; 
>     } 
>   } 
>   MessageBox(IP);
> 
> 

127.0.0.1 is the generic localhost address.  When I run code similar to 
yours, I see my own IP address.  

Run ipconfig from the command line.  What IP address does it return?

0
Okie (47)
4/14/2006 3:13:02 PM
See below...
On Fri, 14 Apr 2006 08:13:02 -0700, Okie <Okie@discussions.microsoft.com> wrote:

>"john.ambou@free.fr" wrote:
>
>> I use this source code but i find only 127.0.0.1 but i want hte IP
>> network who can find in the configuration panel.
>> 
>> 
>>  PHOSTENT phe;
>>  int count;
>>  int dwSize;
>>  int errrr;
>> Char IP[10];
>>  WORD wVersionRequested;
>>  WSADATA wsaData;
>>  int err;
>>  wVersionRequested = MAKEWORD( 2, 2 );
>>  err = WSAStartup( wVersionRequested, &wsaData );
****
Why not
err = WSAStartup(MAKEWORD(2,2), &wsaData);

why do you feel a compulsion to store the value in a variable?
****
>> 
>>  dwSize = sizeof(IP);
>>     errrr=gethostname(IP, dwSize);
****
errr = gethostname(IP, sizeof(IP));

if(errr == SOCKET_ERROR)
   {
    DWORD err = WSAGetLastError();
    ... report the fact that gethostname failed
    return;
   }

it seems silly to create a value "dwSize" (which also proves that you should avoid using
Hungarian Notation.  If you can't use it right, don't use it al all!) to hold a value that
doesn't need to be kept in a separate variable.

Why are you certain that "10" is sufficient?  MAX_PATH might be a better guess, but 10
seems far to small
****
>> 
>>   phe = gethostbyname(IP);
>>   if ((phe = gethostbyname( IP )) != NULL)
****
NEVER use assignment statements embedded in if-statements.  This is horrendous style and
should be avoided!  In a sane language this construct would be forbidden.

since you already did gethostbyname, there is no reason to do it a second time; you should
have written
    if(phe != NULL)
*****
>>   {
>>    count = 0;
>>    while (phe->h_addr_list[count] != NULL)
>>    {
>>      sprintf( IP, "%u.%u.%u.%u",  (unsigned char)
>> phe->h_addr_list[count][0], (unsigned char) phe->h_addr_list[count][1],
>> (unsigned char) phe->h_addr_list[count][2], (unsigned char)
>> phe->h_addr_list[count][3]);
****
This is a very clumsy way to do what inet_ntoa already does.  And it is also erroneous.
You declared IP as size 10, but and IP address of the form  xxx.xxx.xxx.xxx works out to
16, so this will overwrite parts of your stack.  Since this is an MFC forum, why are you
using sprintf instead of CString::Format?

Note also that gethostbyname explicitly says it is obsolete and you should be using
getaddrinfo.

Also, what was wrong with
	for(int count = 0; phe->h_addr_list[count] != NULL; count++)
for the loop?  
****
>>       count++; 
>>     } 
>>   } 
>>   MessageBox(IP);
>> 
>> 
>
>127.0.0.1 is the generic localhost address.  When I run code similar to 
>yours, I see my own IP address.  
>
>Run ipconfig from the command line.  What IP address does it return?
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
-- 
NewsGuy.Com 30Gb $9.95 Carry Forward and On Demand Bandwidth
0
newcomer (15974)
4/15/2006 4:32:10 AM
thank you for you help

0
john.ambou (29)
4/18/2006 9:31:36 AM
Reply:

Similar Artilces:

outlook 2002 problem with reply/forward message
HI all, I'm using OUtlook 2002 with Exchange 5.0. I recently changed my desktop from Win 95 to XP Pro with Office XP Standard. Now I cant reply or forward mails..they stay in the outbox and are not sent. I can only send mail as a new mail. Any help is appreciated.. :S ...

Using WindowsForms PropertyGrid in MFC
Hi , I am not sure if this is the right place. I did everything as in this link : http://blog.joycode.com/jiangsheng/archive/2005/03/19/46065.aspx So I succesfully created a managed property grid in my native MFC application. But when I run my application , I get an assertion in "...\atlmfc\src\mfc\occont.cpp" Line : 950 ASSERT(IsWindow(pTemp->m_hWnd)); When I comment the line ( and also other lines under it ) myPropertyGrid.CreateManagedControl( WS_VISIBLE|WS_CHILD, rect, this, IDC_STATIC_HOLDER ); assertion disappears but ofcourse property grid disappears at the s...

MFC by Borland C++ 5.0
Hello, I have Borland C++ 5.0 on my machine and i want to learn some basic MFC programming (not too complicated stuff) Is it possible to compile and debug MFC projects with BC++ 5.0 ? Thanks for suggestions. Jan, The earlier version of Borland (4.x ????) had a licenced MFC, if my memory doesn't fail me (something it frequently does). I can't remember if this was the case for Borland 5.x (if you have 5.0 you should upgrade - there were two patches created shortly after the initial release). If that is the case, you might already have a few example applications. You'll just hav...

Copy and Paste Problem
Using Publisher 2003. I am trying to copy and paste an Adobe page into a newsletter I am putting together. When I use either the "paste or Special paste" functions the file transfers to the designated page, however, when I try to save the newsletter file I get a blank balloon from the assistant. The only thing that shows in the balloon is an "OK", clicking on the box terminates the program and the just inserted page is not saved. Any ideas on how to work this problem, I really need that Adobe page, tried converting it to a word file, however the formatting and type ...

A strange problem
Hi there, I am compile the program on a english machine, the program can run normally, then I try to run the machine on a chinese machine, but it crashed. I try to debug the program on the chinese machine, such as it crashed at m_CurDateTime = COleDateTime::GetCurrentTime, after press F11, I found in fact it stepped into another function, and the class is not COleDateTime. Program can run on chinese machine normally but crashed on english machine if compile the program on chinese machine. How to solve this type problem? Many thanks for your help. Jack Try look the function HRESULT IDa...

Problem with OWA #4
Hello I have outlook web access setup so users don't need to enter the domain name, just the user name is needed. .... until today. Nothing has changed on the server, on the exchange virtual directory, directory security tab both "integrated windows authentication" and "basic authentication" are checked, with the default domain name being entered. It was working fine up until today, i've tried restarting IIS and restarting the server but it's still requiring the domain name to access webmail. Any ideas what could have gone wrong? Thanks Richard ...

Newsgroup Problems
Hi I use WLM for my two e-mail accounts no problems in or out. I have tried to use "usenet "and entered the account details and message says WLM can't connect to internet may be fire wall or anti virus,checked win fire wall gives it access, I used to have access, but have recently up dated to Win7 so had to reload WLM. Any ideas Please You're getting this from Microsoft Communities if you try to subscribe to a newsgroup? (Newsgroups icon at bottom left of WLMail, right click Microsoft Communities, select "Newsgroups", find your required group, "...

Problem displaying modal dialog box from a dynamically linked regular dll
hi All, i try to call an exported dll function which should create a modal dialog box from an *.exe file. the dll is a regular dynamically linked mfc based dll. my exported function looks like this void CreateDlg() { AFX_MANAGE_STATE(AfxGetStaticModuleState()); CDlg dlg(AfxGetMainWnd()); dlg.DoModal(); } the problem is, that i can see the dialog but it is stuck and the main Dialog window doesn't reponse either. so i have to kill the application. does the code lack of some function overloading ? or message processing ? thanks for any response. I just build a sample and I am not ab...

CStdioFile, MFC dialog, and a command window
I have a traditional MFC dialog-based app that I'm trying to adapt to run from the command line. Running it is no problem, of course; neither is parsing command line arguments and acting on them. The problem is a requirement that the program output a text line to stdout. To test it, I'm executing the program from a command window. I'm under the impression that any output I send to stdout should appear within that command window; is that correct? I'm attempting to use CStdioFile to output the text string; it seems as if it should be pretty straightforward, yet no output a...

user acct problem
I have the built in admin acct (which I never use) and the built in guest acct (which I also never use) and the All Users acct and my original acct Owner.RB. All 4 of these accts have been on my laptop since install. I tried creating a new acct awhile back with limited privileges and it froze which later I read that this was probably due to my broadcom wireless driver which was said to not function in an acct that was not admin privileges. So I gave up on that and deleted the new acct. Now some time later I decide to create another new acct but this time make it a admin privilege. H...

I need a solution to this problem. How do i fix this ?
MSPUB caused an invalid page fault in module MSPUB.EXE at 017f:0052eda5. Registers: EAX=00022755 CS=017f EIP=0052eda5 EFLGS=00010202 EBX=008df1dc SS=0187 ESP=008df0ac EBP=0000011e ECX=00000015 DS=0187 ESI=006d5f66 FS=5227 EDX=012c02cd ES=0187 EDI=008df0c0 GS=5216 Bytes at CS:EIP: f3 a5 66 a5 5f 5e c2 08 00 90 90 8b 4c 24 04 83 Stack dump: 00000020 0000002f 0052edc2 00001a41 008df0c0 00a2d180 00005168 004f1eb9 008df1dc 006c9624 00000008 008df130 00000002 0000011e 00000008 006ccbac What version Publisher? What are you doing when this error appears? Upgrade your drivers, more than likely it...

Excel Send To function problem
Hi!; I have one question regarding Excel send to function. When I want to send excel information through from email and click the send to menu that build in the excel, the default email editor is MS outlook. In case of I don't want MS outlook as default email editor and I want choose other email editor like Outlook express to do the job. How do I change the setting? Thanks & Regards Control Panel>Internet Options>Programs>Email. Pick OE from the drop-down list. Close Excel and re-start. Note: Outlook and Outlook Express are email clients, not editors. Gord Dibben ...

Outlook 2000's map display problem
Back in September I installed the latest (at that time) upgrade to Office 2000. It broke Outlook 2000's ability to display a map of a contact's location. Here is the fix they gave to me and it is working now: Use regedit and navigate to the following location: HKEY_CURRENT_USER\Software\Microsoft\Office\9.0\Outlook\Options\General and add the following string mapscripturl then modify the string and put in the following value: http://www.expediamaps.com/default.asp?street=<0s>&city=<1s>&state=<2s>&zip=<3s> Exit Regedit. Henry Winokur To u...

Smarthost relaying problem
We are trying to set up a Smarthost to have all outbound Internet email from our exchange server relayed via another Exchange Server at our parent company. This is necessary because of a reverse lookup issue that is preventing us from sending to AOL and related domains when we send directly from our server (long story). So, I set up their front end server as the Smarthost and it does resolve the issue and most mail goes out just fine. However, if you send messages to two or more addresses in the same domain (e.g. I send test message to 2 hotmail.com addresses that I have), the message...

Need help with a relaying problem
I am having a very obscure relaying problem and I am looking for help. I will do my best to layout the scenario. Their current setup is a Windows Server 2003 AD with Exchange Server 2003 there is only one administrative group and there are SMTP connectors for primary and secondary connections to the internet. On these connectors all mail is rejected to the internet unless you belong to a distribution list called "Internet Users". I have 3 SMTP domain names being utilized in the Exchange Server environment. All of these scenarios below are users that are part of the "Inte...

PDf problems
A couple of weeks ago, when trying to save a file to pdf in Publisher 2007, all the graphics had black around them. Worked fine before that. And it doesn't matter if the graphic/clip art is jpg or png or tiff - same result. I have run all the security/virus scans and Windows updates, deleted the entire Office Suite and reinstalled, but nothing seems to help. So, I have to put the documents on a memory stick and take to my home computer, convert to pdf, then put back on the memory stick to bring in to the office. HELP! What PDF converter are you using? Can you print the docume...

Distribution security problem
I have an issue where a single email account cannot send email to a distribution list. Other users are able to send to it just fine. I have confirmed the account has permission to the group, confirmed its not locked out, etc. Nothing seems to work. Any direction on this would be most appreciated. what happens when this account tries? any error message? -- Susan Conkey [MVP] "Jerry Cutshaw" <JerryCutshaw@discussions.microsoft.com> wrote in message news:934C752A-73FD-4676-82FB-FBAE8A482B2A@microsoft.com... > I have an issue where a single email account cannot sen...

Problem with MS Outlook 2003 and spelling
My MS Outlook 2003 don't perform spelling check, throws an error "error occurred while checkin spelling". The language is set to Lithuanian. "Micorosft Office 2003 Lithuania user interface pack" is installed. MS Word check spelling without any errors. If Use "Microsoft Office Word 2003 to edit e-mail messages" is checked in "Mail Format" tab and Outlook starts Word interface to compose an email, spell checker works fine, but I don't wanna use a Word as email composer. There are installed and other user interface packs as Norway etc. And with this...

Problem with refresh after search
We are using Outlook 2003 SP3 against Exchange 2000. Users are performing a search on their inbox and then attempting to move items that meet the criteria to a personal folders file. The move takes place but the search result does not reflect that the messages were moved and the user does not get an indication that the move was successful. The search results used to refresh pre SP3. Has anyone else experienced this and is a hot fix available? Thanks ...

Excel 2007 Crashing Problem #3
My company has been riddled with Excel 2007 crashing since we started migrating to the 07 suite. Two users who prominently use Excel for their job tasks have seen many issues mainly when converting .xls documents to .xlsx. The common theme i have seen here has been file size and weather or not the file contains links. Both users also work heavily with graphs. My question is, has there been reported problems with converting large excel files to the new format? Or has there been a graphing problem? ...

Re: Problem with Xpath query !!!
"Nakah" <nakahspam@free.fr> wrote in message news:... > Thanks, I've just tried it but it doesn't work :'( > > > "Derek Harmon" <loresayer@msn.com> wrote in message > news:uqmsyB%23HEHA.2300@tk2msftngp13.phx.gbl... > > "Nakah" <nakahspam@free.fr> wrote in message > news:OcqK488HEHA.1448@TK2MSFTNGP10.phx.gbl... > > > I have some troubles with Xpath when I'm using it with a XmlDocument > containing two namespaces. > > > Here is the XmlDocument : > > > > > > <grammar ...

Staring Excel Problem
Hi All! I have installed windows XP followed by SP2. So I installed Office 2003. Every time I start excel it shows me a message saying "Instalando : recurso do Microsoft Excel" (In English is "Installing : Component (or resource) of Microsoft Excel" I think). It happens every time I start excel and take a minute and it is annoying me very much. Any help is welcome. Thanks in advance Everton Brasil Everton Have you gone to Control Panel>Add/Remove and selected Office 2003 and "Change"? Perhaps Excel is not set up to "run all from my computer&quo...

Simple MFC ActiveX Control
Hello, I am using MS VSIDE 2003. I want to create a simple MFC ActiveX control. I create the control as follows, but it gives me Catastrophic Error. Please let me know where I am doing wrong. I create the project, name is SMath, the name of the control. I select "Invisible at runtime" checked, and all other options unchecked. After creating the project, in the Class View write click on the Interface node, and add the method, say LONG AddVal(LONG v1, LONG v2); class wizard create the method, and the method in the control defination, I just add the code... return v1 + v2; and compile...

Problem sending BCC's
I'm a photographer and I have lead emails that I can cut and paste from an excel program and just paste it into BCC so I can mass mail brides, and it loads them all with no problems. But now that I've switched to Windows 7 mail I can't do it. It acts weird and won't load the email addresses in BCC. The names are there when I paste them in, but it won't send. It says the name isn't in the contact. How can I get around this? thanks! Assuming 'lead' means potential customers vs. the period table - How many addresses are you mass mailing ? Windows 7...

Using Visual C++ 6.0 MFC Application
01/19/2004 Using a single document or a dialog application, I am able to use my own variables that are declared in the same source file, however, if I try to declare a global variable in a header file or a source file included before the code I am using, I get the error "Undeclared Identifier". Example in Old C: "First.h" int i; "Main.cpp" #include "First.h" main() { i=5; printf("%d",i); } When I declare a variable in a header file, in Class View - Globals, my variable does show up, b...