Salut a tous, voila j'ai voulu savoir est ce qu'il y a une fonction en C++ par quoi je peux savoir l'adresse IP de la machine??
Version imprimable
Salut a tous, voila j'ai voulu savoir est ce qu'il y a une fonction en C++ par quoi je peux savoir l'adresse IP de la machine??
En C++ standard? Non.
Bas non en C++ standard ça existe pas... Par contre avec du win32 j'ai pu trouvé ça sur le mdsn :
http://msdn.microsoft.com/en-us/libr...49(VS.85).aspx
J'ai rien comprit dans ce codeCode:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84 #include <winsock2.h> #include <ws2tcpip.h> #include <iphlpapi.h> #include <stdio.h> #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x)) #define FREE(x) HeapFree(GetProcessHeap(), 0, (x)) /* Note: could also use malloc() and free() */ int __cdecl main() { int i; /* Variables used by GetIpAddrTable */ PMIB_IPADDRTABLE pIPAddrTable; DWORD dwSize = 0; DWORD dwRetVal = 0; IN_ADDR IPAddr; /* Variables used to return error message */ LPVOID lpMsgBuf; // Before calling AddIPAddress we use GetIpAddrTable to get // an adapter to which we can add the IP. pIPAddrTable = (MIB_IPADDRTABLE *) MALLOC(sizeof (MIB_IPADDRTABLE)); if (pIPAddrTable) { // Make an initial call to GetIpAddrTable to get the // necessary size into the dwSize variable if (GetIpAddrTable(pIPAddrTable, &dwSize, 0) == ERROR_INSUFFICIENT_BUFFER) { FREE(pIPAddrTable); pIPAddrTable = (MIB_IPADDRTABLE *) MALLOC(dwSize); } if (pIPAddrTable == NULL) { printf("Memory allocation failed for GetIpAddrTable\n"); exit(1); } } // Make a second call to GetIpAddrTable to get the // actual data we want if ( (dwRetVal = GetIpAddrTable( pIPAddrTable, &dwSize, 0 )) != NO_ERROR ) { printf("GetIpAddrTable failed with error %d\n", dwRetVal); if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, dwRetVal, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language (LPTSTR) & lpMsgBuf, 0, NULL)) { printf("\tError: %s", lpMsgBuf); LocalFree(lpMsgBuf); } exit(1); } printf("\tNum Entries: %ld\n", pIPAddrTable->dwNumEntries); for (i=0; i < (int) pIPAddrTable->dwNumEntries; i++) { printf("\n\tInterface Index[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwIndex); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwAddr; printf("\tIP Address[%d]: \t%s\n", i, inet_ntoa(IPAddr) ); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwMask; printf("\tSubnet Mask[%d]: \t%s\n", i, inet_ntoa(IPAddr) ); IPAddr.S_un.S_addr = (u_long) pIPAddrTable->table[i].dwBCastAddr; printf("\tBroadCast[%d]: \t%s (%ld%)\n", i, inet_ntoa(IPAddr), pIPAddrTable->table[i].dwBCastAddr); printf("\tReassembly size[%d]:\t%ld\n", i, pIPAddrTable->table[i].dwReasmSize); printf("\tType and State[%d]:", i); if (pIPAddrTable->table[i].wType & MIB_IPADDR_PRIMARY) printf("\tPrimary IP Address"); if (pIPAddrTable->table[i].wType & MIB_IPADDR_DYNAMIC) printf("\tDynamic IP Address"); if (pIPAddrTable->table[i].wType & MIB_IPADDR_DISCONNECTED) printf("\tAddress is on disconnected interface"); if (pIPAddrTable->table[i].wType & MIB_IPADDR_DELETED) printf("\tAddress is being deleted"); if (pIPAddrTable->table[i].wType & MIB_IPADDR_TRANSIENT) printf("\tTransient address"); printf("\n"); } if (pIPAddrTable) { FREE(pIPAddrTable); pIPAddrTable = NULL; } exit(0); }
Sachant qu'on est en ... 2009
Je te conseille d'utiliser plutot GetAdaptersAddresses qui a l'avantage de savoir retourner les addresses IPv6 (éspérons qu'IPv4 disparaisse vite !!!!)
L'utilisation est similaire à GetIPAddresses, on fait un premier appel pour connaitre la taille mémoire du buffer nécessaire...
On alloue le buffer, et on rappelle la fonction avec ce buffer et cette taille.
Et ensuite on utilise les données ainsi collectées.
A noter que cette derniere fonction permet de 'skipper' les addresses multicast et autres...
Si tu veux dire une fonction défini dans la STL, non elle n'est pas standard. Il n'existe pas de fonction standard pour ce que tu demande a ma connaissance.
Peut etre des framworks existant sur plusieurs plateformes peuvent faire cela comme Qt, GTK et autres.
GetAdaptersAddresses fonction C API Win32 donc windows only... ;)
Doc MSDN ici. L'exemple est pas super évident comme souvent dans la doc MSDN mais tu devrais au moins pouvoir avancer un peu.