Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


winsock struct addrinfo

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Sat Mar 28, 2009 4:14 pm    Post subject: winsock struct addrinfo Reply with quote

How do I retrieve my IP address with the addrinfo struct?
Back to top
View user's profile Send private message
BanMe
Master Cheater
Reputation: 0

Joined: 29 Nov 2005
Posts: 375
Location: Farmington NH, USA

PostPosted: Sat Mar 28, 2009 5:10 pm    Post subject: Reply with quote

http://msdn.microsoft.com/en-us/library/ms738520(VS.85).aspx
...
GetAddrInfo() sample is exactly what u need....

regards BanMe

_________________
don't +rep me..i do not wish to have "status" or "recognition" from you or anyone.. thank you.
Back to top
View user's profile Send private message MSN Messenger
LolSalad
Grandmaster Cheater
Reputation: 1

Joined: 26 Aug 2007
Posts: 988
Location: Australia

PostPosted: Sat Mar 28, 2009 5:18 pm    Post subject: Reply with quote

I'll assume you want your LAN IP(s), not external. Here's how I did it in the past, it retrieves all of your LAN IPs into ipArray.

Code:
   char hostName[256];
   if (gethostname(hostName, sizeof(hostName)) != 0) {
      // Error
   }

   hostent* rHost = NULL;
   rHost = gethostbyname(hostName);
   if (!rHost) {
      // Error
   }

   unsigned int ipCount = sizeof(rHost->h_addr_list) / rHost->h_length;
   char** ipArray = NULL;
   ipArray = new char*[ipCount];
   in_addr rInAddr;
   for (unsigned int i = 0; i < ipCount; i++) {
      ipArray[i] = new char[15];
      memcpy(&rInAddr, rHost->h_addr_list[i], rHost->h_length);
      strcpy_s(ipArray[i], 15, inet_ntoa(rInAddr));
      printf("IP retrieved: %s\n", ipArray[i]);
   }


Once used, you should delete ipArray.
Code:
   delete [] *ipArray;
   delete [] ipArray;

_________________
Back to top
View user's profile Send private message MSN Messenger
nwongfeiying
Grandmaster Cheater
Reputation: 2

Joined: 25 Jun 2007
Posts: 695

PostPosted: Sat Mar 28, 2009 5:22 pm    Post subject: Reply with quote

Thank you LolSalad. I will now give you rep.
Back to top
View user's profile Send private message
LolSalad
Grandmaster Cheater
Reputation: 1

Joined: 26 Aug 2007
Posts: 988
Location: Australia

PostPosted: Sat Mar 28, 2009 5:51 pm    Post subject: Reply with quote

After messing around with it a bit I noticed that ipCount is incorrect (always 1), and I don't know of any way to get the correct number. Neutral

Edit: This seems to work fine.
Code:
   char hostName[256];
   if (gethostname(hostName, sizeof(hostName)) != 0) {
      error("gethostname failed");
      return;
   }

   hostent* rHost = NULL;
   rHost = gethostbyname(hostName);
   if (!rHost) {
      error("gethostbyname failed");
      return;
   }

   unsigned int ipCount = 0;
   char** ipArray = NULL;
   in_addr rInAddr;
   for (unsigned int i = 0; rHost->h_addr_list[i] != 0; i++) {
      ipCount++;
      char** nIPArray = new char*[ipCount];
      for (unsigned int j = 0; j < i; j++) nIPArray[j] = ipArray[j];
      delete [] ipArray;
      ipArray = nIPArray;
      ipArray[i] = new char[15];
      memcpy(&rInAddr, rHost->h_addr_list[i], rHost->h_length);
      strcpy_s(ipArray[i], 15, inet_ntoa(rInAddr));
      printf("IP retrieved: %s\n", i + 1, ipArray[i]);
   }


Also, the delete should probably be
Code:
   for (unsigned int i = 0; i < ipCount; i++) delete [] ipArray[i];
   delete [] ipArray;

_________________
Back to top
View user's profile Send private message MSN Messenger
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites