无法将证书添加到X509Store

时间:2018-07-17 20:20:02

标签: c# x509certificate

我正在尝试以编程方式将证书添加到证书存储中 我正在使用以下代码从目录中获取pfx文件,并将证书添加到“我的商店”下的CurrentUser中。 代码运行无一例外,但我看不到商店中添加的证书。 我尝试将CurrentUser更改为LocalMachine,还尝试在TrustedPeople下添加,但没有成功。

unit GridsEx;

interface

uses
  Windows, SysUtils, Classes, Controls, Grids, Graphics, Dialogs;

const
  CONST_CELL_PADDING = 4;

type
  TStringGridEx = class;

  THeader = class(TPersistent)
  private
    FGrid: TStringGridEx;
    FColor: TColor;
    FFont: TFont;
    procedure FontChanged(Sender: TObject);
    function GetColCount: Longint;
    function GetHeight: Integer;
    procedure SetColor(Value: TColor);
    procedure SetColCount(Value: Longint);
    procedure SetHeight(Value: Integer);
    procedure SetFont(Value: TFont);
  public
    constructor Create(const AGrid: TStringGridEx);
    destructor Destroy; override;
    procedure Assign(Source: TPersistent); override;
  published
    property ColCount: Longint read GetColCount write SetColCount;
    property Color: TColor read FColor write SetColor;
    property Font: TFont read FFont write SetFont;
    property Height: Integer read GetHeight write SetHeight;
  end;

  TStringGridEx = class(TStringGrid)
  private
    FHeader: THeader;
    procedure SetHeader(AValue: THeader);
  protected
    procedure DrawCell(ACol, ARow: Integer; Rect: TRect; State: TGridDrawState); override;
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property ColCount default 3;
    property Header: THeader read FHeader write SetHeader;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Additional', [TStringGridEx]);
end;

{ THeader }

procedure THeader.Assign(Source: TPersistent);
var
  H: THeader;
begin
  if Source is THeader then
  begin
    H := THeader(Source);
    ColCount := H.ColCount;
    Color := H.Color;
    Font := H.Font;
    Height := H.Height;
  end else
    inherited;
end;

constructor THeader.Create(const AGrid: TStringGridEx);
begin
  inherited Create;
  FGrid := AGrid;
  FColor := clBtnFace;
  FFont := TFont.Create;
  FFont.Name := 'Tahoma';
  FFont.Size := 9;
  FFont.Color := clNavy;
  FFont.OnChange := FontChanged;
end;

destructor THeader.Destroy;
begin
  FFont.Free;
  inherited;
end;

procedure THeader.FontChanged(Sender: TObject);
begin
  FGrid.Invalidate;
end;

function THeader.GetColCount: Longint;
begin
  Result := FGrid.ColCount;
end;

function THeader.GetHeight: Integer;
begin
  Result := FGrid.RowHeights[0];
end;

procedure THeader.SetColCount(Value: Longint);
begin
  FGrid.ColCount := Value;
end;

procedure THeader.SetColor(Value: TColor);
begin
  if (Value <> FColor) then
  begin
    FColor := Value;
    FGrid.Invalidate;
  end;
end;

procedure THeader.SetHeight(Value: Integer);
begin
  FGrid.RowHeights[0] := Value;
end;

procedure THeader.SetFont(Value: TFont);
begin
  FFont.Assign(Value);
end;

{ TStringGridEx }

constructor TStringGridEx.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  FHeader := THeader.Create(Self);

  DefaultDrawing := False;
  DefaultRowHeight := 20;
  //Ctl3D := False;
  FixedCols := 0;
  FixedRows := 1;

  ColCount := 3;
  RowHeights[0] := 22;

  Cells[0, 0] := 'Serial';
  Cells[1, 0] := 'Name';

  Cells[0, 1] := '00001';
  Cells[1, 1] := 'Lorem Ipsum';
end;

destructor TStringGridEx.Destroy;
begin
  FHeader.Free;
  inherited;
end;

procedure TStringGridEx.DrawCell(ACol, ARow: Integer; Rect: TRect; State: TGridDrawState);
var
  TextRect: TRect;
  TextFormat: Cardinal;
  S: string;
begin
  inherited;

  Canvas.Brush.Style := bsSolid;
  Canvas.Brush.Color := clWindow;

  if (ARow = 0) then
  begin
    Canvas.Brush.Color := FHeader.Color;
    Canvas.Font.Assign(FHeader.Font);
  end;

  Canvas.FillRect(Rect);

  TextFormat := DT_SINGLELINE or DT_VCENTER or DT_LEFT or DT_END_ELLIPSIS;
  TextRect := Rect;
  TextRect.Left := TextRect.Left + (CONST_CELL_PADDING);

  S := Cells[ACol, ARow];
  DrawText(Canvas.Handle, PChar(S), Length(S), TextRect, TextFormat);
end;

procedure TStringGridEx.SetHeader(AValue: THeader);
begin
  FHeader.Assign(AValue);
end;

end.

0 个答案:

没有答案