Sample Identity DLL (Pascal)

A C version of this DLL is also available

{************************************************************}
{                                                            }
{        DeployMaster Sample Identity Manager DLL            }
{                                                            }
{************************************************************}

library Identity;

uses
  Windows, SysUtils;

{$R *.RES}

var
  Key: HKey;

// This routine is called after the DLL is loaded.
// It allows it to figure out where the processed information should be stored in the Windows registry.
// CompanyName and AppVersion may be nil, AppName will always have a value.
// The strings must not be modified by the DLL, and their length is arbitrary.
procedure InitIdentity(CompanyName, AppName, AppVersion: PChar); stdcall;
var
  ZStr: array[0..255] of Char;
begin
  ZStr := ‘Software’;
  if CompanyName <> nil then begin
    StrCat(ZStr, ‘\’);
    StrCat(ZStr, CompanyName);
  end;
  StrCat(ZStr, ‘\’);
  StrCat(ZStr, AppName);
  if RegCreateKeyEx(HKEY_CURRENT_USER, ZStr, 0, nil, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, nil, Key, nil) <> 0 then
    Key := 0;
end;

// ValidIdentity() is called when the user clicks on the Proceed button.
// Should return True if the information is valid and the user may proceed.  
// If it returns False, the user will have to retry.
// The parameters for items the user is not requested to specify will be nil
// ValidIdentity() is allowed to modify the contents of the strings; e.g. to clear out an invalid registration code
function ValidIdentity(Name, Company, Serial, RegCode: PChar): Bool; stdcall;
begin
  Result := True;
end;

// LoadIdentity() is called right before the user is allow to supply his information
// If (another version of) the application is already installed, 
// it should set the strings to the previously entered data
// If not, the DLL has a chance to provide default data (e.g. a time-limited trial mode registration key)
// The parameters for items the user is not requested to specify will be nil
procedure LoadIdentity(Name, Company, Serial, RegCode: PChar); stdcall;
var
  BufType, BufSize: Integer;
begin
  if Key <> 0 then begin
    BufSize := 128;
    if Name <> nil then RegQueryValueEx(Key, ‘Name’, nil, @BufType, PByte(Name), @BufSize);
    BufSize := 128;
    if Company <> nil then RegQueryValueEx(Key, ‘Company’, nil, @BufType, PByte(Company), @BufSize);
    BufSize := 128;
    if Serial <> nil then RegQueryValueEx(Key, ‘Serial’, nil, @BufType, PByte(Serial), @BufSize);
    BufSize := 128;
    if RegCode <> nil then RegQueryValueEx(Key, ‘RegCode’, nil, @BufType, PByte(RegCode), @BufSize);
  end;
end;

// SaveIdentity() is called after the user clicked the proceed button and ValidIdentity() returned true.
// It should write the data to the Windows registry, so that the application, once installed, can use it.
// The parameters for items the user is not requested to specify will be nil
procedure SaveIdentity(Name, Company, Serial, RegCode: PChar); stdcall;
begin
  if Key <> 0 then begin
    // Save data
    if Name <> nil then RegSetValueEx(Key, ‘Name’, 0, REG_SZ, Name, StrLen(Name)+1);
    if Company <> nil then RegSetValueEx(Key, ‘Company’, 0, REG_SZ, Company, StrLen(Company)+1);
    if Serial <> nil then RegSetValueEx(Key, ‘Serial’, 0, REG_SZ, Serial, StrLen(Serial)+1);
    if RegCode <> nil then RegSetValueEx(Key, ‘RegCode’, 0, REG_SZ, RegCode, StrLen(RegCode)+1);
    // Clean up
    RegCloseKey(Key);
  end;
end;

exports
  // Make our support routines visible to the world
  // If we’re using Unicode, we need to add W to the names of the exported functions
  InitIdentity {$IFDEF UNICODE}name ‘InitIdentityW’{$ENDIF},
  ValidIdentity {$IFDEF UNICODE}name ‘ValidIdentityW’{$ENDIF},
  LoadIdentity {$IFDEF UNICODE}name ‘LoadIdentityW’{$ENDIF},
  SaveIdentity {$IFDEF UNICODE}name ‘SaveIdentityW’{$ENDIF};

end.