Support
Joined: 18 Jul 2005 Posts: 731
|
Posted: Mon Sep 20, 2010 7:18 am Post subject: HOWTO: Use custom machine codes with a license |
|
|
LogicNP CryptoLicensing For MFC uses a simple yet relatively foolproof way to uniquely identify machines. Each machine is identified by its computer name. To provide your own machine codes, derive your own class from CCryptoLicense and override the GetLocalMachineCode method as follows:
Code: | class CCustomCryptoLicense : public CCryptoLicense
{
// Return the MAC address as the machine code
virtual BYTE* GetLocalMachineCode(int& machineCodeLength)
{
machineCodeLength=0;
BYTE* ret=NULL;
ULONG BufferLength = 0;
BYTE* pBuffer = 0;
GetAdaptersInfo( NULL, &BufferLength );
// Now the BufferLength contain the required buffer length.
// Allocate necessary buffer.
pBuffer = new BYTE[ BufferLength ];
// Get the Adapter Information.
PIP_ADAPTER_INFO pAdapterInfo = reinterpret_cast<PIP_ADAPTER_INFO>(pBuffer);
GetAdaptersInfo( pAdapterInfo, &BufferLength );
machineCodeLength = pAdapterInfo->AddressLength;
ret = (BYTE*)CoTaskMemAlloc(machineCodeLength);
CopyMemory(ret,pAdapterInfo->Address,machineCodeLength);
// deallocate the buffer.
delete[] pBuffer;
return ret;
}
};
...
...
...
// Validating the license...
CCustomCryptoLicense appLicense;
appLicense.SetValidationKey(_T("<<validation key for your project>>"));
appLicense.SetLicenseCode(_T("<<license code>>"));
// Validate license...
if(appLicense.GetStatus()!=LS_Valid) //LicenseStatus.LS_Valid = 0
{
_stprintf( str, _T("Invalid license: License Status %ld"), appLicense.GetStatus() );
AfxMessageBox(str);
}
|
Notes
When a machine-locked is being validated, it byte-compares the machine code embedded in the license with the code returned by the GetLocalMachineCode method. To have more control over this, override the CCryptoLicense::IsMachineCodeValid method. This can be useful when you want to implement a somewhat lenient machine code comparison method instead of the default strict byte-comparison. For example, your machine code could consist of a combination of the CPU-ID, BIOS serial number, amount of memory, hard-disk serial number and the MAC address. You may want to allow one or more components of the machine code to change so that every small hardware change does not make the license invalid. |
|