FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin for Harbour/xHarbour Internet access
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Internet access
Posted: Sun Sep 14, 2025 06:58 AM

Hi,

How to find out which internet access is using wired or WIFI ?

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Internet access
Posted: Sun Sep 14, 2025 07:06 AM
#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;
}
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 1392
Joined: Mon May 14, 2007 09:49 AM
Re: Internet access
Posted: Sun Sep 14, 2025 07:12 AM

Thanks, Antonio, I'll try !

Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
Re: Internet access
Posted: Thu Sep 18, 2025 03:24 AM

Dear Mr.Antonio,

Can you please sample programe using the 'C' Function

you have published here.

-Ramesh Babu P

Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Internet access
Posted: Thu Sep 18, 2025 05:26 AM
Dear Ramesh,

Here you have it:
#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
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 44158
Joined: Thu Oct 06, 2005 05:47 PM
Re: Internet access
Posted: Thu Sep 18, 2025 05:28 AM
This version will only check for active ones:
#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 ENDDUMP
regards, saludos

Antonio Linares
www.fivetechsoft.com
Posts: 654
Joined: Fri Oct 21, 2005 05:54 AM
Re: Internet access
Posted: Thu Sep 18, 2025 03:05 PM

Dear Antonio,

Thank you very much.

-Ramesh Babu P

Continue the discussion