Sample code for 30+ languages & platforms
Lazarus Pascal

Import a Certificate (.cer file) into a Windows Certificate Store

See more Certificates Examples

Demonstrates how to import a certificate (without private key) into a Windows certificate store.

Chilkat Lazarus Pascal Downloads

Lazarus Pascal
program ChilkatDemo;

// Demonstrates using the Chilkat Pascal wrapper via the C bridge DLL.
// Builds as a console application under Lazarus (FPC) or Delphi.

{$IFDEF FPC}
  {$MODE DELPHI}
{$ENDIF}
{$APPTYPE CONSOLE}

uses
  {$IFDEF UNIX}
  cthreads,
  {$ENDIF}
  SysUtils,
  CkDllLoader,
  Chilkat.Cert,
  Chilkat.CertStore;

// ---------------------------------------------------------------------------

procedure RunDemo;
var
  success: Boolean;
  cert: TCert;
  certStoreCU: TCertStore;
  readOnlyFlag: Boolean;

begin
  success := False;

  cert := TCert.Create;

  success := cert.LoadFromFile('qa_data/certs/example.cer');
  if (success = False) then
    begin
      WriteLn(cert.LastErrorText);
      Exit;
    end;

  certStoreCU := TCertStore.Create;
  readOnlyFlag := False;

  //  "CurrentUser" and "My" are the exact keywords to select your user account's certificate store.
  success := certStoreCU.OpenWindowsStore('CurrentUser','My',readOnlyFlag);
  if (success = False) then
    begin
      WriteLn('Failed to open the CurrentUser/My certificate store for read/write.');
      Exit;
    end;

  //  Import the certificate into the CurrentUser/My certificate store.
  success := certStoreCU.AddCertificate(cert);
  if (success = False) then
    begin
      WriteLn(certStoreCU.LastErrorText);
      Exit;
    end;

  WriteLn('Imported ' + cert.SubjectCN);
  WriteLn('Success.');


  cert.Free;
  certStoreCU.Free;

end;

// ---------------------------------------------------------------------------

begin

  try
    RunDemo;
  except
    on E: Exception do
      WriteLn('Unhandled exception: ', E.ClassName, ': ', E.Message);
  end;

  WriteLn;
  {$IFDEF MSWINDOWS}
  WriteLn('Press Enter to exit...');
  ReadLn;
  {$ENDIF}
end.