1

I have this function is C++:

#include <windows.h>
#include <iphlpapi.h>
#include <stdio.h>
#include <iostream>


#pragma comment(lib, "iphlpapi.lib")
void printInterfaces(){

    ULONG buflen = sizeof(IP_ADAPTER_INFO);
    IP_ADAPTER_INFO *pAdapterInfo = (IP_ADAPTER_INFO *)malloc(buflen);

    if (GetAdaptersInfo(pAdapterInfo, &buflen) == ERROR_BUFFER_OVERFLOW) {
        free(pAdapterInfo);
        pAdapterInfo = (IP_ADAPTER_INFO *)malloc(buflen);
    }

    if (GetAdaptersInfo(pAdapterInfo, &buflen) == NO_ERROR) {
        for (IP_ADAPTER_INFO *pAdapter = pAdapterInfo; pAdapter; pAdapter = pAdapter->Next) {

            if (pAdapter -> IpAddressList.IpAddress.String != "0.0.0.0"){
                std::cout << "IP: " <<
                    pAdapter->IpAddressList.IpAddress.String <<
                    " Description: " <<
                    pAdapter-> Description << std::endl;

            }


        }
    }

    if (pAdapterInfo) free(pAdapterInfo);

}

I am writing a sniffer with python and I want to get the names of interfaces on windows, so this c++ code prints the IP address and the Description, Is there a way to call this function from python and to make it return the interfaces as a list with tuples? and also I have a problem there when doing != "0.0.0.0" it runs but doesn't filter the interfaces with ip "0.0.0.0". what is the correct way to do it? Also I am more familiar with C#, Is importing C# is easier then C++?

ori
  • 369
  • 2
  • 6
  • 17

2 Answers2

1

Quoted from Python.Boost:

The Boost Python Library is a framework for interfacing Python and C++. It allows you to quickly and seamlessly expose C++ classes functions and objects to Python, and vice-versa, using no special tools -- just your C++ compiler. It is designed to wrap C++ interfaces non-intrusively, so that you should not have to change the C++ code at all in order to wrap it, making Boost.Python ideal for exposing 3rd-party libraries to Python. The library's use of advanced metaprogramming techniques simplifies its syntax for users, so that wrapping code takes on the look of a kind of declarative interface definition language (IDL).

You can see another good and great solution depicted with example here.

Community
  • 1
  • 1
Ebrahim Ghasemi
  • 5,850
  • 10
  • 52
  • 113
1

typically the python wrappers, like swig: http://www.swig.org/papers/PyTutorial98/PyTutorial98.pdf or Python.Boost actually wraps the functions.

so a c++ function with type void, will never return a list. it prints to standardout, so you need to capture that and parse it to generate a python list..

You may want to return something in that c++ function that can actually be interpreted by a python wrapper to a list.

try returning a std::vector and read this: https://wiki.python.org/moin/boost.python/StlContainers

Henrik
  • 2,180
  • 16
  • 29