Andreas Software - Source Code - RandomDLL.PAS

Dynamically load my little DLL file RANDOM.DLL / RANDOM-X86.DLL in Embarcadero Delphi and use it to generate random Bytes

unit RandomDLL;

{
  Dynamically load my little DLL file RANDOM.DLL / RANDOM-X86.DLL in Embarcadero Delphi and use it to generate random Bytes. It's using Microsoft Crypto API. The returned string is hex encoded. The DLL file is Unicode and is available in both x64 and x86 variants. Download the DLL files here:

  https://www.andreas-software.com/RANDOM.DLL.ZIP

  https://www.andreas-software.com/RANDOM-X86.DLL.ZIP

  Original Author: Andreas Jonsson. (www.andreas-software.com)

  NOTICE: YOU MAY USE AND MODIFY THIS SOURCE CODE FREELY, THE ONLY REQUIREMENT IS THAT THE COMMENT ABOUT THE ORIGINAL AUTHOR AND THIS NOTICE REMAINS IN THE SOURCE CODE.
}

interface

uses
  Winapi.Windows, Winapi.Messages,
  System.SysUtils, System.Variants, System.Classes;

function GenerateRandomHexBytes(IntBytes: NativeInt): string;

implementation

type
  TGenerateRandomHexBytesFunction = function(IntBytes: NativeInt; OutputBuffer: Pointer): NativeInt; stdcall;

function GenerateRandomHexBytes(IntBytes: NativeInt): string;
var DLLFileName: string;
var DLLFunctionName: string;
var DLLHandle: NativeUInt;
var GenerateRandomHexBytesFunction: TGenerateRandomHexBytesFunction;
begin
  Result := '';
  if IntBytes > 0 then begin
    DLLFileName := 'RANDOM';
    {$IFDEF WIN32}
      DLLFileName := DLLFileName + '-X86';
    {$ENDIF}
    DLLFileName := DLLFileName + '.DLL';
    DLLFunctionName := 'GenerateRandomHexBytes';
    DLLHandle := LoadLibrary(PWideChar(DLLFileName));
    if DLLHandle <> 0 then begin
      @GenerateRandomHexBytesFunction := GetProcAddress(DLLHandle, PWideChar(DLLFunctionName));
      if Assigned(GenerateRandomHexBytesFunction) = True then begin
        SetLength(Result, IntBytes * 2);
        if GenerateRandomHexBytesFunction(IntBytes, Pointer(Result)) = ((IntBytes * 2) * 2) then begin
          // OK.
        end
        else begin
          Result := '';
        end;
      end;
      FreeLibrary(DLLHandle);
    end;
  end;
end;

end.

⎙ Print RandomDLL.PAS

You can also view the source code as plain text, RandomDLL.PAS.TXT.

The source code is related to KSADLL.PAS.

www.andreas-software.com

Copyright © 1998-2021, Andreas Jönsson. All rights reserved.

In Swedish?
View this page in Swedish