Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi All,

I am writing an application in pure unmanaged C/C++.

I am looking for a way to retrieve the first local MAC Address and use it both as text within a TextBox as well as within a configuration file/Database.

I am able to retrieve the MAC Address within a console application using the GetAdaptersInfo() function. That's all fine and well, but i am unable to find a way to set the text of a TextBox with the use of this function inside a Win32 GUI Application.

I would like to find a way to set a variable or such with the MAC Address as either 00BF67A25E88 or 00-BF-67-A2-5E-88 formats, so that it can then be displayed with a function of the likes of:

?type? MACAddr = GetMACAddress();
SendMessage(hwndMACLabel, WM_SETTEXT, 0, (LPARAM)(?type?)MACAddr);


GetMACAddress() should return the address in one of the above formats and be type compatible with SendMessage().

Please do not post suggestions about using MFC, ATL, WTL, STL, VC++.NET, etc. as i limited to pure unmanaged C/C++.

Your help is very much appreciated :)
Posted
Updated 11-Jan-11 4:38am
v3
Comments
Aescleal 11-Jan-11 10:39am    
Got rid of the C++ tag as the questioner doesn't want a C++ solution.

Try something like this;

char total[100] = "";
char part[4];
for(int i=0;i<padapter->AddressLength;i++){
    sprintf(part,"%02x-",padapter->Address[i]);
    strcat(total,part);
}
// Remove the last '-', we added one too many.
total[strlen(total)-1] = 0;


The variable total now has the requested string, you can pass that to SendMessage.

edit: For some reason my pre-block uses all the wrong colours, but the code doesn't seem to be wrong. Maybe someone else can help fix this.
 
Share this answer
 
v3
Comments
Trance741 11-Jan-11 7:24am    
i have come up with the following:

<pre>char GetMACAddress(void) {
char total[100] = "";
char part[4];
char *pFirstAddr;
unsigned int i;

IP_ADAPTER_INFO AdapterInfo[16];
DWORD dwBufLen = sizeof(AdapterInfo);

DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
assert(dwStatus == ERROR_SUCCESS);

PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
do{
for(i = 0;i < (pAdapterInfo->AddressLength); i++) {
sprintf_s(part, "%02x-", pAdapterInfo->Address[i]);
strcat_s(total, part);
};
//total[strlen(total)-1] = 0; commented for sake of workability

if(i == 0) {
pFirstAddr = total; // grab first MAC address for return of data
// since a return statement would end this function early.
};

pAdapterInfo = pAdapterInfo->Next;

}while(pAdapterInfo);

return pFirstAddr;
}</pre>

...which isn't working atm.

but it is then called by the following code:

<pre>char MACAddr = GetMACAddress();
SendMessageA(hwndLabel, WM_SETTEXT, 0, (LPARAM)MACAddr);</pre> I think there may still be a type compatibility issue with '(LPARAM)MACAddr'??


For the moment I am just trying to get this working with a standard 350*120 window, and a STATIC control named 'hwndLabel'.

Not sure if one would consider the above code as progress, since the only time the program ran successfully the label wasn't actually changed, but i have been messing with the code since then and forgot what i changed.

Cheers
The string pointed to by pFirstAddr is a local variable, it won't be preserved when your function exits.

To solve this, you could make the total variable static, in which case it will be preserved after the function exits.
 
Share this answer
 
v2
Comments
Trance741 11-Jan-11 23:38pm    
Okay, so I've come to the following piece of code:

char GetMACAddress(void) {
static char MACAddressStr[100] = "";
char part[4];
unsigned int i;

IP_ADAPTER_INFO AdapterInfo[16];
DWORD dwBufLen = sizeof(AdapterInfo);

DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
assert(dwStatus == ERROR_SUCCESS);

PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
do{
for(i = 0;i < (pAdapterInfo->AddressLength); i++) {
sprintf_s(part, "%02x-", pAdapterInfo->Address[i]);
strcat_s(MACAddressStr, part);
};

MACAddressStr[strlen(MACAddressStr)-1] = 0;

pAdapterInfo = pAdapterInfo->Next;

}while(pAdapterInfo);

return MACAddressStr[100];
}

...

char MACAddr = GetMACAddress();
SendMessage(hwndLabel, WM_SETTEXT, 0, MACAddr);


Only problem now, is that MACAddr gets Zero'd out when returning the character array from GetMACAddress(). So now i have a TextBox that has nothing in it, not even the text that was set in CreateWindowEx().
[no name] 12-Jan-11 4:24am    
You return only one character from MACAddressStr now (and one that is out of range even).
The function should be made to return a string like so:
char *GetMACAddress(void){...}
and it should return MACAddressStr like so:
return MACAddressStr;

Previously, you had a char *pFirstAddr pointing to total; returning that would work fine too.
You're confused with the meaning of unmanaged C/C++.
MFC, ATL, WTL, and STL are all unmanaged C++.

You probably only want to use the C runtime functions in which case it would be a little more work.

To get the MAC address you would need to do -
BYTE MACAddr[MAX_ADAPTER_ADDRESS_LENGTH];

The signature of the function must be -
void GetMACAddress(BYTE* MAXAddr);

And the function must be called -
GetMACAddress(MACAddr);

Now you can use the BYTE array in a call to SendMesage with WM_SETTEXT.
 
Share this answer
 
Comments
Trance741 11-Jan-11 23:47pm    
Thanks for clearing that up for me, I just know that i am unable to use any of them, just C/C++ with the Win32 API.

Also I have tried your suggestion, but from what i can make out, your suggestion is based on me giving the function an address and going from there. But I'm probably mis-reading it.

What i am looking for is for GetMACAddress() to return a character array created with the use of GetAdaptersInfo() so that the array can be used in a variable primarily to change the text within a Text Box. I have come to the point where GetMACAddress() creates the MAC Address (you can see the code in the comment below) but I am now stuck on my function not being able to successfully return the character array for use with SendMessage.
Following is the Code that worked for me:

Global Variable:
char MACAddressStr[20] = "";


GetMACAddress() Function:
char GetMACAddress(void) {
	char part[4];
	unsigned int i;

	IP_ADAPTER_INFO AdapterInfo[16];
	DWORD dwBufLen = sizeof(AdapterInfo);

	DWORD dwStatus = GetAdaptersInfo(AdapterInfo, &dwBufLen);
	assert(dwStatus == ERROR_SUCCESS);
	
	PIP_ADAPTER_INFO pAdapterInfo = AdapterInfo;
	do{
		for(i = 0;i < (pAdapterInfo->AddressLength); i++) {
			sprintf_s(part, "%02x-", pAdapterInfo->Address[i]);
			strcat_s(MACAddressStr, part);
		};

		MACAddressStr[strlen(MACAddressStr)-1] = 0;
		
		pAdapterInfo = pAdapterInfo->Next;
	}while(pAdapterInfo);
	
	return MACAddressStr[20];
}


And call the following either in a switch or after a call to CreateWindow/Ex() in WinMain or in a separate function:
GetMACAddress();

char *pMACAddressStr[sizeof(MACAddressStr)];

for(int i = 0; i < sizeof(MACAddressStr); i++) {
    pMACAddressStr[i] = &MACAddressStr[i];
}

SendMessageA(hwndLabel, WM_SETTEXT, 0, (int)pMACAddressStr[0]);


Thanks '<<_Superman_>>' and 'Thaddeus Jones' for the help!
Much Appreciated :)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900