Programmatically get the MAC address in C

This is a small example on how you can get the MAC address in C both in Linux and Windows. The example searches in fact the MAC address corresponding to a given IP address and returns it. /* * mac.cpp * Created on: May 25, 2010 * Author: len */ #ifdef WIN32 #include <stdio.h> #include <Windows.h> #include <Iphlpapi.h> #include <Assert.h> #pragma comment(lib, "iphlpapi.lib") /** * @return the mac address of the interface with the given ip */ char* getMAC(const char *ip){ PIP_ADAPTER_INFO AdapterInfo; DWORD dwBufLen = sizeof(AdapterInfo); char *mac_addr = (char*)malloc(17); AdapterInfo = (IP_ADAPTER_INFO *) malloc(sizeof(IP_ADAPTER_INFO)); if (AdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersinfo\n"); return NULL; } // Make an initial call to GetAdaptersInfo to get the necessary size into the dwBufLen variable if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == ERROR_BUFFER_OVERFLOW) { FREE(AdapterInfo); AdapterInfo = (IP_ADAPTER_INFO *) malloc(dwBufLen); if (AdapterInfo == NULL) { printf("Error allocating memory needed to call GetAdaptersinfo\n"); return NULL; } } if (GetAdaptersInfo(AdapterInfo, &dwBufLen) == NO_ERROR) { PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;// Contains pointer to current adapter info do { sprintf(mac_addr, "%02X:%02X:%02X:%02X:%02X:%02X", pAdapterInfo->Address[0], pAdapterInfo->Address[1], pAdapterInfo->Address[2], pAdapterInfo->Address[3], pAdapterInfo->Address[4], pAdapterInfo->Address[5]); printf("Address: %s, mac: %s", pAdapterInfo->IpAddressList....

June 7, 2010 · len

The conflicting ssleay32 and libeay32

Since everybody seemed to be stuck with using the library on windows and yet on linux it worked from the start I’ve decided to dive into the wonderful world of windoze. The problem: while trying to use a java, jni-based library the error was unavoidable: java.lang.UnsatisfiedLinkError: C:\sw_api\sw_api\windows\sw_api.dll: The operating system cannot run %1 at java.lang.ClassLoader$NativeLibrary.load(Native Method) at java.lang.ClassLoader.loadLibrary0(Unknown Source) at java.lang.ClassLoader.loadLibrary(Unknown Source) at java.lang.Runtime.loadLibrary0(Unknown Source) at java.lang.System.loadLibrary(Unknown Source) As such I decided to wrote this simple program:...

February 3, 2009 · len