1

I use TurboPower onGuard for licensing and there is an option to specify and generate the machine specific keys for license validation. With following code I create machine modifier based on machineID

const
    Ckey :TKey = ($56,$1B,$B0,$48,$2D,$AF,$F4,$E5,$30,$6C,$E5,$3B,$A7,$8E,$1A,$14);

procedure TForm1.FormCreate(Sender: TObject);
var
  InfoSet : TEsMachineInfoSet;
  MachineID : Longint;
begin
  { initialize the machine information set }
  InfoSet := [midUser, midSystem, midNetwork, midDrives];
  { create the machine ID and display in hex }
  try
  MachineID := CreateMachineID(InfoSet);
  Edit1.Text := '$' + BufferToHex(MachineID, SizeOf(MachineID));
  except on E:Exception do
   ShowMessage(E.Message);
  end;

end;

procedure TForm1.Button1Click(Sender: TObject);
var
   modifier :LongInt;
   key :TKey;
   releasecode,unlockcode :TCode;
   inexpirydate: TdateTime;
begin
  modifier := GenerateMachineModifierPrim ;
  ApplyModifierToKeyPrim(Modifier, CKey, SizeOf(CKey));

  //HexToBuffer(Ckey, key, SizeOf(TKey));
  InitRegCode(Ckey, edit1.Text, inExpiryDate, releaseCode);
  Edit2.text :=  BufferToHex(unlockCode, SizeOf(releaseCode));
end;    

On the other hand to validate serial number and relase code I use the following code on client side to validate application, I place SerialNumberCode component to implement that.

 Ckey :TKey = ($56,$1B,$B0,$48,$2D,$AF,$F4,$E5,$30,$6C,$E5,$3B,$A7,$8E,$1A,$14);

procedure TForm1.OgSerialNumberCode1GetKey(Sender: TObject; var Key: TKey);
begin
  Key := Ckey;
end;

procedure TForm1.OgSerialNumberCode1GetModifier(Sender: TObject;
  var Value: LongInt);
begin
 Value := GenerateMachineModifierPrim;

end;

function TForm1.GetSNData(var S: string): integer;
var
  TC  : TCode;
  SNC : string;
  modifier :LOngInt;
begin
  try
    modifier := GenerateMachineModifierPrim;
    ApplyModifierToKeyPrim(Modifier, CKey, SizeOf(CKey));

    IniSNVal := StrToInt(InputBox('Serial number', 'Enter serial Value ', '12345678'));
    SNC :=  (InputBox('release code', 'Enter code ', ''));
    {Check that Release Code was entered correctly}
    HexToBuffer(SNC, TC, SizeOf(TCode));
    if not (IsSerialNumberCodeValid(CKey, TC)) then begin
      S := 'Release code not entered correctly';
      Result := mrCancel;
    end else begin
      IniFile := TIniFile.Create(TheDir + 'test.INI');
      try
        IniFile.WriteInteger('Codes', 'SN', IniSNVal);
        IniFile.WriteString('Codes', 'SNCode', SNC);
      finally
        IniFile.Free;
      end;
    end;
  finally
  end;

end;

procedure TForm1.OgSerialNumberCode1GetCode(Sender: TObject; var Code: TCode);
var
  S1  : string;
  L   : integer;
begin
     if not (FileExists(TheDir + 'test.INI')) then
    Exit;

  {open Ini File}
  IniFile := TIniFile.Create(TheDir + 'test.INI');
  try
    {try to read release code}
    S1 := IniFile.ReadString('Codes', 'SNCode', '');
    IniSNVal := IniFile.ReadInteger('Codes', 'SN', 0);

    {convert retrieved string to a code}
    HexToBuffer(S1, Code, SizeOf(Code));
  finally
    IniFile.Free;
  end;

end;

procedure TForm1.OgSerialNumberCode1Checked(Sender: TObject; Status: TCodeStatus);
var
  S,
  C1,
  C2  : string;
  TC  : TCode;
  LI  : longint;
begin
  case Status of
    ogValidCode   : begin
                      {check if retrieved Serial Number matches Code}
                      LI := OgSerialNumberCode1.GetValue;

                      if (LI <> IniSNVal) then begin
                        Status := ogInvalidCode;
                        S := 'The serial number has been changed';
                      end else begin
                          ShowMessage('Serial #: ' + IntToStr(IniSNVal));
                        Exit;
                      end;
                    end;

    ogInvalidCode : begin
                      {if INI file doesn't exist, presume this is first run}
                      if not (FileExists(TheDir + 'test.INI')) then
                      begin
                        if not (GetSNData(S) = mrCancel) then begin
                          {Check the SN/ReleaseCode}
                          OgSerialNumberCode1.CheckCode(True);
                          {must Exit since line above began a recursive call}
                          Exit;
                        end;
                      end else
                        S := 'Invalid Code';
                    end;

    ogCodeExpired : S := 'Evaluation period expired';
  end;

  ShowMessage(S);
  Application.Terminate;    
end;

But at the client side machine never accepts the validation and throws Release code not entered correctly. Probably I don't understand usage of machineID exmaple in onguard folder. Please also note that the key for client side and generation for relaease code is same so there shouldn't be any mismatch.

Sonya Blade
  • 399
  • 7
  • 24
  • Take a look at the source of GenerateMachineModifierPrim, its a wrapper for CreateMachineID and in my SourceForge version GenerateMachineModifierPrim uses: [midUser, midSystem, {midNetwork,} midDrives]. midNetwork is commented out. I may be best to avoid GenerateMachineModifierPrim and use CreateMachineID instead this way you can be sure both modifiers match. – FredS Feb 01 '16 at 21:22

0 Answers0