How do I find out a public IP address on the Internet using the Win32 API?

Participant 41 Reputation points
2024-06-24T07:02:48.6033333+00:00

How do I find out a public IP address on the Internet using the Win32 API?

I am interested in the public ip address, not the IP address of the local network. Interested in IPv4 and IPv6.

Now I find out my ip address like this: I go to some site that determines the ip address, and the site tells me my ip address. I need to do this using the Win32 API.

I am interested in the Win32 API, and not any programming languages (C++, C#, etc.).

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,650 questions
{count} votes

Accepted answer
  1. Castorix31 85,706 Reputation points
    2024-06-24T14:39:24.2666667+00:00

    Yes, you need to use an external website.

    For example with Wininet on my PC :

    					HINTERNET hInternet = InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY, NULL, NULL, 0);
    					if (hInternet != NULL)
    					{
    						HINTERNET hFile = InternetOpenUrl(hInternet, _T("https://api.ipify.org"), NULL, 0, INTERNET_FLAG_RELOAD, 0);
    						if (hFile != NULL)
    						{
    							CHAR sBuffer[MAX_PATH];
    							TCHAR wsBuffer[MAX_PATH];
    							DWORD nRead;
    							BOOL bRet = InternetReadFile(hFile, sBuffer, sizeof(sBuffer), &nRead);
    							if (bRet)
    							{
    								sBuffer[nRead] = '\0';
    								MultiByteToWideChar(CP_ACP, 0, sBuffer, -1, wsBuffer, MAX_PATH);
    								TCHAR wsMessage[MAX_PATH] = _T("");
    								wsprintf(wsMessage, _T("External IP : %s"), wsBuffer);
    								MessageBox(NULL, wsMessage, _T("Information"), MB_OK | MB_ICONINFORMATION);
    							}
    							InternetCloseHandle(hFile);							
    						}
    						InternetCloseHandle(hInternet);
    					}
    
    
    1 person found this answer helpful.
    0 comments No comments

7 additional answers

Sort by: Most helpful
  1. Participant 41 Reputation points
    2024-06-24T13:57:27.7166667+00:00

    David Lowndes, what is a web service? A regular website or something else? I'm just not an expert in this.

    RLWA32, is it impossible to find out the public api address using the Win32 API?


  2. Participant 41 Reputation points
    2024-06-24T14:20:34.9233333+00:00

    The conclusion is that using the Win32 API, it is impossible to find out the IP address of a computer if the computer is connected to the Internet through a router. In this case, you need to use a website that identifies the ip address. Right?


  3. Participant 41 Reputation points
    2024-06-24T18:57:48.3+00:00

    Gary Nebbett, are you suggesting using Win32 API functions or something else?


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.