在使用EnumFontFamiliesEx枚举Windows 64位平台上的字体时接收访问违规 &g



尝试在Windows 64位平台上使用EnumFontFamiliesEx枚举字体时收到访问违规。

我尝试了很多方法来解决我的问题,但强烈需要你的帮助!!

第一次机会异常&0000000000713034。异常类$C0000005,消息' C0000005 ACCESS_VIOLATION'。过程Progect1.exe

下面是我的程序:

{ On Windows 64-bit Platform "EnumFontFamiliesEx" shows Error     }
{ On Windows 32-bit Platform "EnumFontFamiliesEx" works perfectly }
{ On Form TListBox "ListBox1" and TButton "Button1"               }
unit Unit1;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes,
Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls;
type
TFontTypeObject = class (TObject)
FtoFontType: Integer;
FtoFlags: Cardinal;
end;
type
TForm1 = class(TForm)
ListBox1: TListBox;
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure TForm1.Button1Click(Sender: TObject);
function EnumFontFamExProc(var LogFont: TEnumLogFontEx; var TextMetric: TNewTextMetric; FontType: Integer; Data: Pointer): Integer; stdcall;
var
Strings: TStringList;
TempFaceName: String;
FontTypeObject: TFontTypeObject;
begin
Strings:= TStringList(Data);
TempFaceName:= LogFont.elfLogFont.lfFaceName;
if (Strings.IndexOf(TempFaceName) = -1) then      // ERROR HERE
// First chance exception at &0000000000713034. Exception class $C0000005 with message 'c0000005 ACCESS_VIOLATION'. Process Progect1.exe
begin
FontTypeObject:= TFontTypeObject.Create;
FontTypeObject.FtoFontType:= FontType;
FontTypeObject.FtoFlags:= TextMetric.ntmFlags;
Strings.AddObject(TempFaceName, TFontTypeObject(FontTypeObject));
end;
Result:= 1;
end;
var
DC: HDC;
LogFont: TLogFont;
begin
DC:= GetDC(0);
try
FillChar(LogFont, SizeOf(LogFont), 0);
LogFont.lfCharSet:= DEFAULT_CHARSET;
LogFont.lfFaceName:= '';
LogFont.lfPitchAndFamily:= 0;
EnumFontFamiliesEx(DC, LogFont, @EnumFontFamExProc, LPARAM(ListBox1.Items), 0);
ListBox1.Sorted:= TRUE;
finally
ReleaseDC(0, DC);
end;
end;
end.

我看到两个问题:

  1. 你不应该取本地过程的地址。使您的EnumFontFamExProc成为一个正常的(非本地的)过程。

  2. castTStringList(Data)不正确。Data不是TStringList实例。它是一个TListBoxStrings,它是一个TStrings,ListBox1.Items属性的类型。

最新更新