Below is the code that I have to find the Mac address of any pc (physical host ID) I struggle to make it print to a file but now i have a problem... It prints MAC Address: XX-XX-XX-XX-XX-XX to C:\\Physical-Address.txt but I only want it to print the XX-XX-XX etc part... and since I cant get it to do that I decided I want to erase the "MAC Address: " part of the file and print the XX-XX to a new file inorder to put it into a string... ive been working on this for 3 days now and cant find an answer =( I tried getline, fstream commands so i left those headers in the code still incase they are needed... Also I had to redirect the mac address to a text file since i dont know how to get it into a string any help of any sort is appreciated but pls remember Im very new to programming and i self teach... been about a 3 months of straight studying...
#include "stdafx.h"
#include <Windows.h>
#include <lm.h>
#include <assert.h>
#include <iostream>
#include <string>
#include <stdio.h>
#include <winuser.h>
#include <fstream>
#pragma comment(lib, "Netapi32.lib")
using namespace std;
// Prints the MAC address stored in a 6 byte array to stdout
static void PrintMACaddress(unsigned char MACData[])
{
printf("MAC Address: %02X-%02X-%02X-%02X-%02X-%02X\n",
MACData[0], MACData[1], MACData[2], MACData[3], MACData[4], MACData[5]);
}
// Fetches the MAC address and prints it
static void GetMACaddress(void)
{
unsigned char MACData[8]; // Allocate data structure for MAC (6 bytes needed)
WKSTA_TRANSPORT_INFO_0 *pwkti; // Allocate data structure for Netbios
DWORD dwEntriesRead;
DWORD dwTotalEntries;
BYTE *pbBuffer;
// Get MAC address via NetBios's enumerate function
NET_API_STATUS dwStatus = NetWkstaTransportEnum(
NULL, // [in] server name
0, // [in] data structure to return
&pbBuffer, // [out] pointer to buffer
MAX_PREFERRED_LENGTH, // [in] maximum length
&dwEntriesRead, // [out] counter of elements actually enumerated
&dwTotalEntries, // [out] total number of elements that could be enumerated
NULL); // [in/out] resume handle
assert(dwStatus == NERR_Success);
pwkti = (WKSTA_TRANSPORT_INFO_0 *)pbBuffer; // type cast the buffer
for(DWORD i=1; i< dwEntriesRead; i++) // first address is 00000000, skip it
{ // enumerate MACs and print
swscanf((wchar_t *)pwkti[i].wkti0_transport_address, L"%2hx%2hx%2hx%2hx%2hx%2hx",
&MACData[0], &MACData[1], &MACData[2], &MACData[3], &MACData[4], &MACData[5]);
PrintMACaddress(MACData);
}
// Release pbBuffer allocated by above function
dwStatus = NetApiBufferFree(pbBuffer);
assert(dwStatus == NERR_Success);
}
//MAIN FUNCTION
int _tmain(int argc, _TCHAR* argv[])
{
FILE *stream ; //Redirect Output from console to a TextFile
if((stream = freopen("C:\\Physical-Address.txt", "w", stdout)) == NULL)
exit(-1);
GetMACaddress(); // Obtain MAC address of adapters
stream = freopen("CON", "w", stdout); //Redirect Output to the Screen
cout<<"Physical Address Found, C:\\Physical-Address.txt\n\n";
//dont know what to put here
Sleep(2000);
return(0);
}