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,609 questions
{count} votes

Accepted answer
  1. Castorix31 85,116 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. David Lowndes 2,535 Reputation points MVP
    2024-06-24T14:05:25.3133333+00:00

    You just need to do a Http get operation. Here's an example:

    static LPCWSTR AcceptTypes[]{ L"text/*", NULL };
    
    static std::string GetIPAddressFromService()
    {
    	std::string sMyPublicAddress;
    	unique_HINTERNET hOpen{ InternetOpen( L"", INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0 ) };
    	if ( hOpen != NULL )
    	{
    		unique_HINTERNET hConn{ InternetConnect( hOpen.get(), L"api.ipify.org", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 1 ) };
    		if ( hConn != NULL )
    		{
    			// Try 3x before giving up so as not to wait excessively
    			for ( int rcnt = 0; (rcnt < 3) && sMyPublicAddress.empty(); ++rcnt )
    			{
    				// GET is the default; use https
    				unique_HINTERNET hReq{ HttpOpenRequest( hConn.get(), NULL, L"", NULL, NULL, AcceptTypes, INTERNET_FLAG_NO_UI | INTERNET_FLAG_SECURE, 1 ) };
    				if ( hReq != NULL )
    				{
    					if ( HttpSendRequest( hReq.get(), NULL, 0, NULL, 0 ) )
    					{
    						// The expected response is the IP (v4) address as a string
    						char ipAddrStr[sizeof( "255.255.255.255" )];
    						DWORD BytesRead;
    						if ( InternetReadFile( hReq.get(), ipAddrStr, sizeof( ipAddrStr ), &BytesRead ) )
    						{
    							sMyPublicAddress = std::string( ipAddrStr, BytesRead );
    							// Check what we have is an ipv4 address by converting it
    							IN_ADDR inaddr;
    							int rv = inet_pton( AF_INET, sMyPublicAddress.c_str(), &inaddr );
    							// 1 means OK!
    							if ( rv != 1 )
    							{
    								// We got an invalid string for the address, clear it and retry
    								sMyPublicAddress.clear();
    							}
    						}
    					}
    				}
    			}
    		}
    	}
    	return sMyPublicAddress;
    }
    
    
    1 person found this answer helpful.
    0 comments No comments

  2. Xiaopo Yang - MSFT 12,726 Reputation points Microsoft Vendor
    2024-06-24T08:04:14.6666667+00:00

    Hello @Participant, A way is GetAdaptersAddresses(). Also see the question as a result of quick search.

    0 comments No comments

  3. Participant 41 Reputation points
    2024-06-24T08:49:53.51+00:00

    XiaoYang - MSFT, the link you provided discusses the topic: How to get the local IP address of Windows system? Pay attention to "local". I use the term "public" in my first post. Be careful when you give advice.

    Since you do not see the difference between "local" and "public", I assume that the "GetAdaptersAddresses" function you proposed will not give the desired result.

    0 comments No comments

  4. Participant 41 Reputation points
    2024-06-24T13:02:43.43+00:00

    RLWA32, I'm accessing the Internet through a router.

    David Lowndes, are you suggesting using the Win32 API or a website that will tell me my ip address?


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.