Click to See Complete Forum and Search --> : Resolving MAC of a remote machine C++


thelonesquirrely
January 21st, 2009, 02:24 PM
Hey,

So I am working in an embedded environment (on a virtex 5 for those who care) and I need a way that I can find the MAC address of a computer given its IP/port from C++ code. I need to pass it to a different module.

Is there some standard c++ library that will let me retrieve this as a string/int/etc...I can handle the parsing afterward.

Thanks!

BobS0327
January 30th, 2009, 10:20 AM
One possible solution would be to query the remote machine for the MAC...

#define _WIN32_DCOM
#include <iostream>

#include <comdef.h>
#include <Wbemidl.h>
# pragma comment(lib, "wbemuuid.lib")
# pragma comment(lib, "credui.lib")
#include <wincred.h>

using namespace std;

int main(void)
{
HRESULT hres;
hres = CoInitializeEx(0, COINIT_MULTITHREADED);
if (FAILED(hres))
{
cout << "Failed to initialize COM library. Error code = 0x"
<< hex << hres << endl;
return 1;
}
hres = CoInitializeSecurity(
NULL,
-1,
NULL,
NULL,
RPC_C_AUTHN_LEVEL_DEFAULT,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE,
NULL
);
if (FAILED(hres))
{
cout << "Failed to initialize security. Error code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1;
}
IWbemLocator *pLoc = NULL;
hres = CoCreateInstance(
CLSID_WbemLocator,
0,
CLSCTX_INPROC_SERVER,
IID_IWbemLocator, (LPVOID *) &pLoc);
if (FAILED(hres))
{
cout << "Failed to create IWbemLocator object."
<< " Err code = 0x"
<< hex << hres << endl;
CoUninitialize();
return 1;
}
IWbemServices *pSvc = NULL;
hres = pLoc->ConnectServer(
_bstr_t(L"\\\\Net0\\root\\cimv2"),
_bstr_t(L"Net0\\Joe"), // Local Admin User name
_bstr_t(L"ABC123"), // Local Admin User password
_bstr_t(L"MS_409"), // Locale
NULL, // Security flags
_bstr_t(L""), // Authority
0, // Context object
&pSvc // IWbemServices proxy
);
if (FAILED(hres))
{
cout << "Could not connect. Error code = 0x"
<< hex << hres << endl;
pLoc->Release();
CoUninitialize();
return 1;
}
cout << "Connected to ROOT\\CIMV2 WMI namespace" << endl;
hres = CoSetProxyBlanket(
pSvc,
RPC_C_AUTHN_WINNT,
RPC_C_AUTHZ_NONE,
NULL,
RPC_C_AUTHN_LEVEL_CALL,
RPC_C_IMP_LEVEL_IMPERSONATE,
NULL,
EOAC_NONE
);
if (FAILED(hres))
{
cout << "Could not set proxy blanket. Error code = 0x"
<< hex << hres << endl;
pSvc->Release();
pLoc->Release();
CoUninitialize();
return 1;
}
IEnumWbemClassObject* pEnumerator = NULL;
IWbemClassObject *pclsObj;
ULONG uReturn = 0;
BSTR strQuery = (L"SELECT * FROM Win32_NetworkAdapterConfiguration WHERE IPEnabled = True");
BSTR strQL = (L"WQL");
hres = pSvc->ExecQuery(strQL, strQuery,
WBEM_FLAG_RETURN_IMMEDIATELY,NULL,&pEnumerator);

while (pEnumerator)
{
HRESULT hr = pEnumerator->Next(WBEM_INFINITE, 1,
&pclsObj, &uReturn);

if(0 == uReturn) break;
VARIANT vtProp;
hr = pclsObj->Get(L"MacAddress", 0, &vtProp, 0, 0);
wcout << " Mac Address : " << vtProp.bstrVal << endl;
VariantClear(&vtProp);
}
pSvc->Release();
pLoc->Release();
pEnumerator->Release();
pclsObj->Release();
CoUninitialize();
return 0;
}

Note that you'll have to use an account with Admin privileges on the targeted computer.