RtfLabel, Version 1.3a (alt)

Hinweis: dies ist nicht die neueste Version!

Zurück zur Übersicht

Datei: RichEditDll.pas

{
  RichEditDll.pas

  Managing the RichEdit 3.0+ RICHED20.DLL / MSFTEDIT.DLL modules. Also includes
  some code for debugging.

  Version 1.1 - always find the most current version at
  http://flocke.vssd.de/prog/code/pascal/rtflabel/

  Copyright (C) 2006 Volker Siebert <flocke@vssd.de>
  All rights reserved.

  Permission is hereby granted, free of charge, to any person obtaining a
  copy of this software and associated documentation files (the "Software"),
  to deal in the Software without restriction, including without limitation
  the rights to use, copy, modify, merge, publish, distribute, sublicense,
  and/or sell copies of the Software, and to permit persons to whom the
  Software is furnished to do so, subject to the following conditions:

  The above copyright notice and this permission notice shall be included in
  all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  DEALINGS IN THE SOFTWARE.
}

unit RichEditDll;

{$I Rich3Conf.inc}

interface

uses
  Windows, SysUtils, RichEdit, RichEdit2;

type
  ERichEditDllNotFound = class(Exception);

  TRichEditModule = record
    DllName: string;
    WndClass: string;
    Version: Integer;
    Handle: THandle;
  end;

  TRichEditVersion = (rvNone, rvRichEdit3, rvRichEdit4);
  TRichEditVersions = set of TRichEditVersion;

var
  RichEditModules: array [TRichEditVersion] of TRichEditModule = (
    ( DllName: '';             WndClass: 'EDIT';          Version:  0 ),
    ( DllName: 'RICHED20.DLL'; WndClass: RICHEDIT_CLASSW; Version: -1 ),
    ( DllName: 'MSFTEDIT.DLL'; WndClass: MSFTEDIT_CLASS;  Version: -1 )
  );

var
  UseRichEditVersions: TRichEditVersions = [rvRichEdit3, rvRichEdit4];

function AvailableRichEditModules: TRichEditVersions;
function GetRichEditModule(Versions: TRichEditVersions = []): TRichEditVersion;
procedure UnloadRichEditModules;

type
  TRichEditDebugOutputProc = procedure(const s: string);

var
  RichEditDebugOutputProc: TRichEditDebugOutputProc = nil;

procedure RichEditDebugOutput(const str: string);

implementation

procedure RichEditDebugOutput(const str: string);
begin
  {$IFDEF RICHEDIT_DEBUG}
  if Assigned(RichEditDebugOutputProc) then
    RichEditDebugOutputProc(str);
  {$ENDIF}
end;

resourcestring
{$IFDEF LANG_GERMAN}
  {$DEFINE LANG_DEFINED}
  SRichEditDllNotFound = 'RichEdit Bibliothek "%s" konnte nicht geladen werden';
{$ENDIF}

{$IFNDEF LANG_DEFINED}
  SRichEditDllNotFound = 'RichEdit library "%s" could not be loaded';
{$ENDIF}

function DllVersionNumber(const Name: string): Integer;
var
  fn: string;
  VerSize: cardinal;
  VerBuf: pointer;
  VerMisc: cardinal;
  VerInfo: PVSFixedFileInfo;
begin
  Result := 0;
  fn := Name;
  UniqueString(fn);
  VerSize := GetFileVersionInfoSize(PChar(fn), VerMisc);
  if VerSize > 0 then
  begin
   Result := 20;
    GetMem(VerBuf, VerSize + 4);
    try
      if GetFileVersionInfo(PChar(fn), VerMisc, VerSize, VerBuf) then
        if VerQueryValue(VerBuf, '\', pointer(VerInfo), VerSize) then
          if Word(VerInfo^.dwFileVersionMS) > 20 then
            Result := Word(VerInfo^.dwFileVersionMS);
    finally
      FreeMem(VerBuf);
    end;
  end;
end;

function AvailableRichEditModules: TRichEditVersions;
var
  Ver: TRichEditVersion;
begin
  Result := [];

  for Ver := High(TRichEditVersion) downto Low(TRichEditVersion) do
    if Ver <> rvNone then
      with RichEditModules[Ver] do
      begin
        if Version = -1 then
        begin
          Version := DllVersionNumber(DllName);
          if Version < 30 then
            Version := 0;
        end;

        if Version > 0 then
          include(Result, Ver);
      end;
end;

function GetRichEditModule(Versions: TRichEditVersions): TRichEditVersion;
var
  Ver, MinVer: TRichEditVersion;

  function LoadLib(const Name: string): THandle;
  var
    oem: LongInt;
  begin
    // Load the richedit library
    oem := SetErrorMode(SEM_NOOPENFILEERRORBOX);
    try
      Result := LoadLibrary(PChar(Name));
      if (Result > 0) and (Result <= HINSTANCE_ERROR) then
        Result := 0;
    finally
      SetErrorMode(oem);
    end;
  end;

begin
  if Versions = [] then
    Versions := UseRichEditVersions;

  MinVer := rvNone;
  for Ver := High(TRichEditVersion) downto Low(TRichEditVersion) do
    if Ver in Versions then
    begin
      MinVer := Ver;
      if Ver <> rvNone then
        with RichEditModules[Ver] do
        begin
          if Version = -1 then
          begin
            Version := DllVersionNumber(DllName);
            if Version < 30 then
              Version := 0;
          end;

          if Version > 0 then
          begin
            if Handle = 0 then
              Handle := LoadLib(DllName);

            if Handle <> 0 then
            begin
              Result := Ver;
              exit;
            end;
          end;
        end;
    end;

  if MinVer <> rvNone then
    raise ERichEditDllNotFound.CreateResFmt(@SRichEditDllNotFound,
      [RichEditModules[MinVer].DllName]);

  Result := rvNone;
end;

procedure UnloadRichEditModules;
var
  Ver: TRichEditVersion;
begin
  for Ver := High(TRichEditVersion) downto Low(TRichEditVersion) do
    with RichEditModules[Ver] do
      if Handle <> 0 then
      begin
        FreeLibrary(Handle);
        Handle := 0;
      end;
end;

initialization
finalization
  UnloadRichEditModules;
end.
Flocke's Garage
Valid HTML 4.01 Transitional Valid CSS!
(C) 2005-2018 Volker Siebert.
Creative Commons-LizenzvertragDer gesamte Inhalt dieser Webseite steht unter einer Creative Commons-Lizenz (sofern nicht anders angegeben).