Hi,
How to find out which internet access is using wired or WIFI ?
Hi,
How to find out which internet access is using wired or WIFI ?
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#pragma comment(lib, "IPHLPAPI.lib") // For Visual Studio; use -liphlpapi for MinGW
int main() {
ULONG bufferSize = 0;
MIB_IFTABLE *ifTable = NULL;
DWORD result;
// Get required buffer size
GetIfTable(NULL, &bufferSize, FALSE);
// Allocate buffer
ifTable = (MIB_IFTABLE *)malloc(bufferSize);
if (ifTable == NULL) {
printf("Memory allocation failed.\n");
return 1;
}
// Get the interface table
result = GetIfTable(ifTable, &bufferSize, FALSE);
if (result != NO_ERROR) {
printf("GetIfTable failed with error: %d\n", result);
free(ifTable);
return 1;
}
// Iterate through interfaces
for (DWORD i = 0; i < ifTable->dwNumEntries; i++) {
MIB_IFROW *row = &ifTable->table[i];
// Check if interface is operational (up)
if (row->dwOperStatus == IF_OPER_STATUS_UP) {
char *typeStr;
switch (row->dwType) {
case 6: // IF_TYPE_ETHERNET_CSMACD
typeStr = "Wired (Ethernet)";
break;
case 71: // IF_TYPE_IEEE80211
typeStr = "WiFi";
break;
default:
typeStr = "Other/Unknown";
break;
}
// Print interface name and type (convert wide char to char)
char name[256];
wcstombs(name, row->wszName, sizeof(name));
printf("Active Interface: %s | Type: %s\n", name, typeStr);
}
}
free(ifTable);
return 0;
}Thanks, Antonio, I'll try !
Dear Mr.Antonio,
Can you please sample programe using the 'C' Function
you have published here.
-Ramesh Babu P
#include "FiveWin.ch"
PROCEDURE Main()
LOCAL aInterfaces
LOCAL aInt
// Call the C function from Harbour
aInterfaces := HB_GETACTIVEINTERFACES()
// Check if there was an error (if it's a string, it's an error message)
IF ValType(aInterfaces) == "C"
? "Error: " + aInterfaces
RETURN
ENDIF
// Display active interfaces
? "Active interfaces:"
FOR EACH aInt IN aInterfaces
? "Active Interface: " + aInt[1] + " | Type: " + aInt[2]
NEXT
? "End of program."
RETURN
#pragma BEGINDUMP
#include <hbapi.h>
#include <hbapiitm.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#define IF_OPER_STATUS_UP 1
HB_FUNC( HB_GETACTIVEINTERFACES )
{
ULONG bufferSize = 0;
MIB_IFTABLE *ifTable;
DWORD result;
PHB_ITEM pArray;
DWORD i;
MIB_IFROW *row;
char *typeStr;
char name[256];
PHB_ITEM pSubArray;
// Get required buffer size
GetIfTable(NULL, &bufferSize, FALSE);
// Allocate buffer
ifTable = (MIB_IFTABLE *) hb_xgrab(bufferSize);
if (ifTable == NULL) {
hb_retc("Memory allocation failed.");
return;
}
// Get the interface table
result = GetIfTable(ifTable, &bufferSize, FALSE);
if (result != NO_ERROR) {
hb_xfree(ifTable);
hb_retc("GetIfTable failed.");
return;
}
// Create Harbour array for results
pArray = hb_itemArrayNew(0);
// Iterate through interfaces
for (i = 0; i < ifTable->dwNumEntries; i++) {
row = &ifTable->table[i];
switch (row->dwType) {
case 6: // IF_TYPE_ETHERNET_CSMACD
typeStr = "Wired (Ethernet)";
break;
case 71: // IF_TYPE_IEEE80211
typeStr = "WiFi";
break;
default:
typeStr = "Other/Unknown";
break;
}
// Convert wide char name to char
wcstombs(name, row->wszName, sizeof(name));
// Create subarray [name, type]
pSubArray = hb_itemArrayNew(2);
hb_arraySetC(pSubArray, 1, name);
hb_arraySetC(pSubArray, 2, typeStr);
// Add to main array
hb_arrayAdd(pArray, pSubArray);
hb_itemRelease(pSubArray);
}
hb_xfree((void *) ifTable);
// Return the array
hb_itemReturnRelease(pArray);
}
#pragma ENDDUMP#include "FiveWin.ch"
PROCEDURE Main()
LOCAL aInterfaces
LOCAL aInt
// Call the C function from Harbour
aInterfaces := HB_GETACTIVEINTERFACES()
// Check if there was an error (if it's a string, it's an error message)
IF ValType(aInterfaces) == "C"
? "Error: " + aInterfaces
RETURN
ENDIF
// Display active interfaces
? "Active interfaces:"
FOR EACH aInt IN aInterfaces
? "Active Interface: " + aInt[1] + " | Type: " + aInt[2]
NEXT
? "End of program."
RETURN
#pragma BEGINDUMP
#include <hbapi.h>
#include <hbapiitm.h>
#include <winsock2.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <stdlib.h>
#define IF_OPER_STATUS_UP 1
HB_FUNC( HB_GETACTIVEINTERFACES )
{
ULONG bufferSize = 0;
MIB_IFTABLE *ifTable;
DWORD result;
PHB_ITEM pArray;
DWORD i;
MIB_IFROW *row;
char *typeStr;
char name[256];
PHB_ITEM pSubArray;
// Get required buffer size
GetIfTable(NULL, &bufferSize, FALSE);
// Allocate buffer
ifTable = (MIB_IFTABLE *) hb_xgrab(bufferSize);
if (ifTable == NULL) {
hb_retc("Memory allocation failed.");
return;
}
// Get the interface table
result = GetIfTable(ifTable, &bufferSize, FALSE);
if (result != NO_ERROR) {
hb_xfree(ifTable);
hb_retc("GetIfTable failed.");
return;
}
// Create Harbour array for results
pArray = hb_itemArrayNew(0);
// Iterate through interfaces
for (i = 0; i < ifTable->dwNumEntries; i++) {
row = &ifTable->table[i];
// Check if the interface is operational (up)
if (row->dwOperStatus == IF_OPER_STATUS_UP) {
switch (row->dwType) {
case 6: // IF_TYPE_ETHERNET_CSMACD
typeStr = "Wired (Ethernet)";
break;
case 71: // IF_TYPE_IEEE80211
typeStr = "WiFi";
break;
default:
typeStr = "Other/Unknown";
break;
}
// Convert wide char name to char
wcstombs(name, row->wszName, sizeof(name));
// Create subarray [name, type]
pSubArray = hb_itemArrayNew(2);
hb_arraySetC(pSubArray, 1, name);
hb_arraySetC(pSubArray, 2, typeStr);
// Add to main array
hb_arrayAdd(pArray, pSubArray);
hb_itemRelease(pSubArray);
}
}
hb_xfree((void *) ifTable);
// Return the array
hb_itemReturnRelease(pArray);
}
#pragma ENDDUMPDear Antonio,
Thank you very much.
-Ramesh Babu P