Sample Identity DLL (C)

A Pascal version of this DLL is also available

This code assumes that UNICODE is not defined when compiling your DLL. If it is defined, append a W to all functions.

/*************************************************************
*                                                            *
*        DeployMaster Sample Identity Manager DLL            *
*                                                            *
*************************************************************/

#include <windows.h>

HKEY Key;

// 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 NULL, AppName will always have a value.
// The strings must not be modified by the DLL, and their length is arbitrary.
void __export __stdcall InitIdentity(LPCTSTR CompanyName, LPCTSTR AppName, LPCTSTR AppVersion)
{
  TCHAR ZStr[256];
  strcpy(ZStr, “Software”);
  if (CompanyName != NULL) {
    strcat(ZStr, “\\”);
    strcat(ZStr, CompanyName);
  }
  strcat(ZStr, “\\”);
  strcat(ZStr, AppName);
  if (RegCreateKeyEx(HKEY_CURRENT_USER, &ZStr, 0, NULL, REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, NULL, &Key, NULL) != 0)
    Key = 0;
}

// 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 NULL
// ValidIdentity() is allowed to modify the contents of the strings; e.g. to clear out an invalid registration code
BOOL __export __stdcall ValidIdentity(LPTSTR Name, LPTSTR Company, LPTSTR Serial, LPTSTR RegCode)
{
  return TRUE;
}

// 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 NULL
void __export __stdcall LoadIdentity(LPTSTR Name, LPTSTR Company, LPTSTR Serial, LPTSTR RegCode)
{
  DWORD BufType, BufSize;
  if (Key != 0) {
    BufSize = 128;
    if (Name != NULL) RegQueryValueEx(Key, “Name”, NULL, &BufType, (LPBYTE)Name, &BufSize);
    BufSize = 128;
    if (Company != NULL) RegQueryValueEx(Key, “Company”, NULL, &BufType, (LPBYTE)Company, &BufSize);
    BufSize = 128;
    if (Serial != NULL) RegQueryValueEx(Key, “Serial”, NULL, &BufType, (LPBYTE)Serial, &BufSize);
    BufSize = 128;
    if (RegCode != NULL) RegQueryValueEx(Key, “RegCode”, NULL, &BufType, (LPBYTE)RegCode, &BufSize);
  }
}

// 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 NULL
void __export __stdcall SaveIdentity(LPCTSTR Name, LPCTSTR Company, LPCTSTR Serial, LPCTSTR RegCode)
{
  if (Key != 0) {
    // Save data
    if (Name != NULL) RegSetValueEx(Key, “Name”, 0, REG_SZ, (CONST BYTE*)Name, strlen(Name)+1);
    if (Company != NULL) RegSetValueEx(Key, “Company”, 0, REG_SZ, (CONST BYTE*)Company, strlen(Company)+1);
    if (Serial != NULL) RegSetValueEx(Key, “Serial”, 0, REG_SZ, (CONST BYTE*)Serial, strlen(Serial)+1);
    if (RegCode != NULL) RegSetValueEx(Key, “RegCode”, 0, REG_SZ, (CONST BYTE*)RegCode, strlen(RegCode)+1);
    // Clean up
    RegCloseKey(Key);
  }
}