请问如何采用判断MAC地址的方式设置程序运行许可?
我编的一个程序,希望用户运行时,判断一下所运行机器的mac地址,如果在列表范围内则运行,否则不运行,防止未经许可的用户使用。
请问如何在程序设置代码?
[解决办法]
//---------------------------------------
#include <vcl.h>
#include <conio.h>
#pragma argsused
//---------------------------------------
#include <windows.h>
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <iostream.h>
#define MAX_ADAPTER_DESCRIPTION_LENGTH 128 // arb.
#define MAX_ADAPTER_NAME_LENGTH 256 // arb.
#define MAX_ADAPTER_ADDRESS_LENGTH 8 // arb.
typedef struct {
char String[4 * 4];
} IP_ADDRESS_STRING, *PIP_ADDRESS_STRING, IP_MASK_STRING, *PIP_MASK_STRING;
typedef struct _IP_ADDR_STRING {
struct _IP_ADDR_STRING* Next;
IP_ADDRESS_STRING IpAddress;
IP_MASK_STRING IpMask;
DWORD Context;
} IP_ADDR_STRING, *PIP_ADDR_STRING;
typedef struct _IP_ADAPTER_INFO {
struct _IP_ADAPTER_INFO* Next;
DWORD ComboIndex;
char AdapterName[MAX_ADAPTER_NAME_LENGTH + 4];
char Description[MAX_ADAPTER_DESCRIPTION_LENGTH + 4];
UINT AddressLength;
BYTE Address[MAX_ADAPTER_ADDRESS_LENGTH];
DWORD Index;
UINT Type;
UINT DhcpEnabled;
PIP_ADDR_STRING CurrentIpAddress;
IP_ADDR_STRING IpAddressList;
IP_ADDR_STRING GatewayList;
IP_ADDR_STRING DhcpServer;
BOOL HaveWins;
IP_ADDR_STRING PrimaryWinsServer;
IP_ADDR_STRING SecondaryWinsServer;
time_t LeaseObtained;
time_t LeaseExpires;
} IP_ADAPTER_INFO, *PIP_ADAPTER_INFO;
typedef DWORD (WINAPI* GetAdaptersInfoFunc)(
PIP_ADAPTER_INFO pAdapterInfo, // buffer to receive data
PULONG pOutBufLen // size of data returned
);
///////////////////////////////////////////////////////////////////////////////
void MacAddressToString(const LPBYTE Address, LPSTR lpsz, int nAddressLength = 6)
{
int n;
LPSTR p = lpsz;
for (n = 0; n < nAddressLength; n++)
{
p += sprintf(p, n ? "-%02x" : "%02x", Address[n]);
}
}
void main()
{
HMODULE hLib;
GetAdaptersInfoFunc GetAdaptersInfo = NULL;
PIP_ADAPTER_INFO pai = NULL;
DWORD dwSize = 0;
CHAR szMac[64];
hLib = LoadLibrary("Iphlpapi.dll");
if (!hLib)
{
puts("Failed to load Iphlpapi.dll");
exit(1);
}
GetAdaptersInfo = (GetAdaptersInfoFunc)GetProcAddress(hLib, "GetAdaptersInfo");
if (GetAdaptersInfo == NULL)
{
puts("Failed to load GetAdaptersInfo in Iphlpapi.dll");
}
// Get size of buffer needed:
GetAdaptersInfo(NULL, &dwSize);
pai = (PIP_ADAPTER_INFO)GlobalAlloc(GPTR, dwSize);
GetAdaptersInfo(pai, &dwSize);
PIP_ADAPTER_INFO p = pai;
cout < <p->AdapterName < <endl;
cout < <p->DhcpEnabled < <endl;
MacAddressToString(p->Address, szMac, p->AddressLength);
cout < <"mac地址:" < <szMac < <endl;
GlobalFree(pai);
FreeLibrary(hLib);
}