Beginner want to advance (Network Programming in Windows)

Hallo.
I have a PacktPublishing subscription. I learned & watched 2 courses
about C and thought I can now go to some advanced stuff. Maybe I am wrong.
Now I started a course for "Network Programming with C".
I can't get the FIRST code working.

I am using the MINGW64 gcc compiler in Windows 10.
I get this error message:
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
.\winsock2.c: In function 'main':
.\winsock2.c:38:15: warning: passing argument 1 of 'free' makes pointer from integer without a cast [-Wint-conversion]
   38 |          free(adapter_size);
      |               ^~~~~~~~~~~~
      |               |
      |               DWORD {aka long unsigned int}
In file included from C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/10.2.0/include/mm_malloc.h:27,
                 from C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/10.2.0/include/xmmintrin.h:34,
                 from C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/10.2.0/include/immintrin.h:29,
                 from C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/10.2.0/include/x86intrin.h:32,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/winnt.h:1555,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/minwindef.h:163,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/windef.h:9,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/windows.h:69,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/WinSock2.h:23,
                 from .\winsock2.c:5:
C:/msys64/mingw64/x86_64-w64-mingw32/include/stdlib.h:530:27: note: expected 'void *' but argument is of type 'DWORD' {aka 'long unsigned int'}
  530 |   void __cdecl free(void *_Memory);
      |                     ~~~~~~^~~~~~~
.\winsock2.c:45:15: warning: passing argument 1 of 'free' makes pointer from integer without a cast [-Wint-conversion]
   45 |          free(adapter_size);
      |               ^~~~~~~~~~~~
      |               |
      |               DWORD {aka long unsigned int}
In file included from C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/10.2.0/include/mm_malloc.h:27,
                 from C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/10.2.0/include/xmmintrin.h:34,
                 from C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/10.2.0/include/immintrin.h:29,
                 from C:/msys64/mingw64/lib/gcc/x86_64-w64-mingw32/10.2.0/include/x86intrin.h:32,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/winnt.h:1555,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/minwindef.h:163,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/windef.h:9,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/windows.h:69,
                 from C:/msys64/mingw64/x86_64-w64-mingw32/include/WinSock2.h:23,
                 from .\winsock2.c:5:
C:/msys64/mingw64/x86_64-w64-mingw32/include/stdlib.h:530:27: note: expected 'void *' but argument is of type 'DWORD' {aka 'long unsigned int'}
  530 |   void __cdecl free(void *_Memory);
      |                     ~~~~~~^~~~~~~
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/10.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\mksh\AppData\Local\Temp\ccfa6YAH.o:winsock2.c:(.text+0x105): undefined reference to `GetAdaptersAddresses'
collect2.exe: error: ld returned 1 exit status
 


I wrote the code in VisualCode.

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
#ifndef _WIN32_WINNT
#define _WIN32_WINNT 0x0600
#endif

#include <WinSock2.h>
#include <iphlpapi.h>
#include <WS2tcpip.h>
#include <stdio.h>
#include <stdlib.h>

int main(void)
{
   WSADATA data;
   if (WSAStartup(MAKEWORD(2, 2), &data))
   {
      printf("Socket failure.\n");
      return -1;
   }

   DWORD adapter_size = 20000;
   PIP_ADAPTER_ADDRESSES adapters;

   do
   {
      adapters = (PIP_ADAPTER_ADDRESSES)malloc(adapter_size);

      if (!adapters)
      {
         printf("Could not allocate memory for adapter(s)! (%ld bytes)\n", adapter_size);
         WSACleanup();
         return -1;
      }

      int r = GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, 0, adapters, &adapter_size);
      if (r == ERROR_BUFFER_OVERFLOW)
      {
         printf("GetAdaptersAddresses wants more memory: %ld bytes\n", adapter_size);
         free(adapter_size);
      }
      else if (r == ERROR_SUCCESS)
         break;
      else
      {
         printf("Error-Code from GetAdaptersAddresses: %d\n", r);
         free(adapter_size);
         WSACleanup();
         return -1;
      }
   } while (!adapters);
}


Thank you.
Last edited on
Free the thing you allocated.
free(adapter_size);

Compare with
adapters = (PIP_ADAPTER_ADDRESSES)malloc(adapter_size);

See the difference?
Of course. Ok. Thank you. Didn't see this. :-)
Topic archived. No new replies allowed.