MAC changing utility that can be used on Windows from the command line.
90c5fbc6757814acbd1f1a07456780bb3a9a61b9ef64a246eb092af41bd2f1e8
#include <windows.h>
#include <setupapi.h>
#include <stdio.h>
#include "devguid.h" //
http://doc.ddart.net/msdn/header/include/devguid.h.html
/*
Simple c file for changing the MAC address on a windows system from the
command line.
Consists solely of changing the below reg value, and restarting the network
adapter
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Class\{4D36E972-E325-11CE-BFC1-08002bE10318}\....
Yes, NIC can be restarted using devcon.exe
Yes, code is bloathed
TODO:
- code cleanup
- make the listing of adapters better (only show network cards)
- reset MAC address option
Quickly whipped this up yesterday afternoon after seeing several programs
that do the
same thing (for 20 bucks)
Robbe De Keyzer, December 2005
*/
// This is where the MAC address is kept in all NT variants
const char *subkey =
"SYSTEM\\CurrentControlSet\\Control\\Class\\{4D36E972-E325-11CE-BFC1-08002bE10318}\\";
void list_adapters(void);
char *get_adapter_description(int);
void set_mac(int, char *);
void restart_adapter(int);
int main(int argc, char *argv[])
{
int adapter_id;
if (argc < 2)
{
printf("Usage: %s [-l] [-s adapter_id MAC]\n", argv[0]);
printf("Where\n");
printf("\t -l gives a list of all adapters and their id\n");
printf("\t -s will set the supplied MAC address for the chosen
adapter\n");
printf("\nWarning! This will restart your internet connection.\n");
return(0);
}
if (!strncmp(argv[1], "-l", 2))
{
list_adapters();
}
if (!strncmp(argv[1], "-s", 2))
{
adapter_id = atoi(argv[2]);
set_mac(adapter_id, argv[3]);
restart_adapter(adapter_id);
printf("All done. MAC address should be changed.\n");
printf("Wait a few seconds and try ipconfig /all\n");
}
return(0);
}
/* Print a list of all network adapters */
void list_adapters()
{
int i;
char *descr;
printf("\nList of available network adapters\n");
/* Get a list of all network adapters from the registry */
for (i=0; i<=12; i++)
{
descr = get_adapter_description(i);
if (descr != NULL)
{
printf("%d: %s\n", i, descr);
}
}
}
/* Get the description of an adapter by its number in the registry*/
char *get_adapter_description(int regNum)
{
DWORD size;
char buf[256], value[256];
HKEY hKey;
strcpy(buf, "");
snprintf(buf, sizeof(buf), "%s%04d", subkey, regNum);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_QUERY_VALUE, &hKey) ==
ERROR_SUCCESS)
{
/* First get the size of the data that's waiting using a NULL lpData
parameter */
RegQueryValueEx(hKey, "DriverDesc", NULL, NULL, NULL, &size);
/* Then we print the name of the network adapter */
if(RegQueryValueEx(hKey, "DriverDesc", NULL, NULL, value, &size) ==
ERROR_SUCCESS)
{
RegCloseKey(hKey);
return(value);
}
RegCloseKey(hKey);
}
return(NULL);
}
/*
Write our supplied MAC to the NetworkAddress key in the registry
*/
void set_mac(int regNum, char *mac)
{
DWORD size, nRet;
char buf[256], value[256];
HKEY hKey;
strcpy(buf, "");
snprintf(buf, sizeof(buf), "%s%04d", subkey, regNum);
/* If the NetworkAddress key does not exist, create it */
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, buf, 0, KEY_ALL_ACCESS, &hKey) ==
ERROR_SUCCESS)
{
RegSetValueEx(hKey,"NetworkAddress",0,REG_SZ,mac,strlen(mac));
RegCloseKey(hKey);
}
}
/*
Copied from
http://groups.google.com/group/microsoft.public.vb.winapi.networks/browse_thread/thread/e8d7e5f627e57c11/58bdcdc53ce70e0f
*/
BOOL StateChange(DWORD NewState, DWORD SelectedItem,HDEVINFO hDevInfo)
{
SP_PROPCHANGE_PARAMS PropChangeParams ={sizeof(SP_CLASSINSTALL_HEADER)};
SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
//
// Get a handle to the Selected Item.
//
if (!SetupDiEnumDeviceInfo(hDevInfo,SelectedItem,&DeviceInfoData))
return FALSE;
//
// Set the PropChangeParams structure.
//
PropChangeParams.ClassInstallHeader.InstallFunction =
DIF_PROPERTYCHANGE;
PropChangeParams.Scope = DICS_FLAG_GLOBAL;
PropChangeParams.StateChange = NewState;
if (!SetupDiSetClassInstallParams(hDevInfo,
&DeviceInfoData,
(SP_CLASSINSTALL_HEADER *)&PropChangeParams,
sizeof(PropChangeParams)))
return FALSE;
//
// Call the ClassInstaller and perform the change.
//
if (!SetupDiCallClassInstaller(DIF_PROPERTYCHANGE,
hDevInfo,
&DeviceInfoData))
return FALSE;
return TRUE;
}
void restart_adapter(int adapter_id)
{
int i;
HDEVINFO hdi;
SP_DEVINFO_DATA DeviceInfoData;
// get a list of all devices of class 'GUID_DEVCLASS_NET'
hdi = SetupDiGetClassDevs(&GUID_DEVCLASS_NET, NULL, NULL, DIGCF_PRESENT);
if (hdi == INVALID_HANDLE_VALUE)
{
printf("Invalid handle\n");
return;
}
/*
We can just use the first entry in the hdi list as our adapter but we loop
through
them all to make sure we are working with the right one
Copied from
http://msdn.microsoft.com/library/en-us/devio/base/displaying_the_installed_devices.asp
*/
// Enumerate through all devices in Set.
DeviceInfoData.cbSize = sizeof(SP_DEVINFO_DATA);
for (i=0; SetupDiEnumDeviceInfo(hdi,i,&DeviceInfoData); i++)
{
LPTSTR buffer = NULL;
DWORD buffersize = 0;
while (!SetupDiGetDeviceRegistryProperty(hdi,&DeviceInfoData,
SPDRP_DEVICEDESC,NULL,(PBYTE)buffer,buffersize,&buffersize))
{
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
{
// Change the buffer size.
if (buffer) LocalFree(buffer);
buffer = LocalAlloc(LPTR,buffersize);
}
else
{
break;
}
}
/* Do we have the right adapter? */
if (!strncmp(buffer, get_adapter_description(adapter_id), strlen(buffer)))
{
/* First disable it, then enable it again */
StateChange(DICS_DISABLE, 0, hdi);
StateChange(DICS_ENABLE, 0, hdi);
SetupDiDestroyDeviceInfoList(hdi);
if (buffer) LocalFree(buffer);
return;
}
if (buffer) LocalFree(buffer);
}
}
_________________________________________________________________
Gratis bloggen op MSN Spaces http://spaces.msn.com/?mkt=nl-be