Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<WinSock2.h>
- #include<iphlpapi.h>
- #include<stdio.h>
- #include<stdlib.h>
- #include<Windows.h>
- #pragma comment(lib, "IPHLPAPI.lib")
- #define BUFF_SIZE 15000
- int main(void )
- {
- PIP_ADAPTER_ADDRESSES pAddresses = NULL;
- //DWORD len;
- DWORD retval=0;
- ULONG iterations=0;
- ULONG outBufLen=BUFF_SIZE;
- ULONG size=0;
- DWORD size2=0;
- WSADATA wsaData;
- char op[256];
- LPSOCKADDR lp=NULL;
- do
- {
- pAddresses = (IP_ADAPTER_ADDRESSES *) malloc(outBufLen);
- if(pAddresses==NULL)
- {
- printf("\n The buffer memory could not be allocated ");
- return 1;
- }
- retval=GetAdaptersAddresses(AF_UNSPEC,GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &outBufLen);
- if(retval==ERROR_BUFFER_OVERFLOW)
- {
- free(pAddresses);
- pAddresses=NULL;
- }
- else
- break;
- iterations++;
- }while((retval==ERROR_BUFFER_OVERFLOW)&&(iterations<3));
- printf(" Return value = %d ",retval);
- if(retval==NO_ERROR)
- {
- while (pAddresses) {
- printf("\tAdapter name: %s\n", pAddresses->AdapterName);
- printf("Description: %wS\n", pAddresses->Description);
- retval = WSAStartup(MAKEWORD(2, 2), &wsaData);
- if (retval != 0)
- {
- printf("WSAStartup() failed with error code %d\n", WSAGetLastError());
- return 1;
- }
- else
- printf("WSAStartup() is OK...\n");
- size=256;
- lp=pAddresses->FirstUnicastAddress->Address.lpSockaddr;
- size2=pAddresses->FirstUnicastAddress->Address.iSockaddrLength;
- if(WSAAddressToStringA(lp,size2,NULL,op,&size)!= 0)
- {
- printf("This thing has failed \n");
- printf("errordetail: %i\n", WSAGetLastError());
- return 1;
- }
- else
- printf("\t The ip address is = %s\n", op);
- pAddresses = pAddresses->Next;
- }
- }
- else
- {
- printf("\n We have attempted thrice and there has been no output :( ");
- free(pAddresses);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement