Enumerate Network Interfaces:
Use the GetAdaptersInfo or GetAdaptersAddresses function to enumerate all network interfaces on your system. These functions provide information about each network adapter, including IP addresses.
Filter by Subnet:
Once you have the information about each network adapter, filter the results to get the one that belongs to the subnet you are interested in. You can compare the subnet of each adapter's IP address with the subnet of your system.
#include <WinSock2.h>
#include <IPHlpApi.h>
#pragma comment(lib, "IPHLPAPI.lib")
int main() {
// Initialize Winsock
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
// Handle error
return 1;
}
// Buffer for adapter information
ULONG outBufLen = 15000;
PIP_ADAPTER_ADDRESSES pAddresses = (IP_ADAPTER_ADDRESSES*)malloc(outBufLen);
// Get adapter information
if (GetAdaptersAddresses(AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, pAddresses, &outBufLen) != ERROR_SUCCESS) {
// Handle error
free(pAddresses);
WSACleanup();
return 1;
}
// Iterate through the list of adapters
for (PIP_ADAPTER_ADDRESSES pCurrAddresses = pAddresses; pCurrAddresses != NULL; pCurrAddresses = pCurrAddresses->Next) {
// Check if the adapter is active and not a loopback adapter
if ((pCurrAddresses->OperStatus == IfOperStatusUp) && (pCurrAddresses->IfType != IF_TYPE_SOFTWARE_LOOPBACK)) {
// Filter by subnet or any other criteria you need
// For example, check if the adapter's IP address is on a specific subnet
// ...
// Print the IP address
printf("Local IP Address: %s\n", pCurrAddresses->FirstUnicastAddress->Address.lpSockaddr->sa_data);
}
}
// Clean up
free(pAddresses);
WSACleanup();
return 0;
}
The behavior you're experiencing with GetHostByName returning an IP address different from the one on your desired subnet is not necessarily a bad configuration. Here's why:
Function Behavior: GetHostByName typically returns the primary IP address associated with the hostname. This might not always be the IP address you're expecting, especially when your system has multiple network adapters.
Multiple Adapters: With Ethernet, Hamachi, and VMware adapters present, your system can have multiple IP addresses assigned, each potentially belonging to different subnets. GetHostByName might pick the address associated with a different adapter than the one you intend to use.
Here are some approaches to get the local IP address on the desired subnet:
1. Enumerate Adapters:
Use WSAEnumNetworkAdapters to enumerate all network adapters on your system.
Loop through each adapter and check its network mask and IP address using GetIpAddrTable.
Look for an adapter where the network mask matches the subnet mask of your desired network (usually obtained through other means).
The IP address associated with this adapter will be the local IP on the desired subnet.
Consider using platform-specific APIs like ifconfig (Linux/macOS) or ipconfig (Windows) through system calls. These tools often provide more granular control over retrieving information specific to a particular adapter.
Return to FiveWin for Harbour/xHarbour
Users browsing this forum: No registered users and 78 guests