尝试从dll加载过程时在Delphi中出现错误

阿里亚尔

从dll加载过程时,无论是动态加载还是静态加载,我都遇到问题。当我将程序从dll放到我的单元中时,一切正常。当我尝试使用dll时,它给了我

首次机会例外为$ 00526399。带有消息“在0x00526399发生访问冲突:读取地址0x00000390”的异常类$ C0000005。处理Project1.exe(21988)

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, Vcl.ComCtrls,Unit2;

type
  TForm1 = class(TForm)
    ListView1: TListView;
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    Edit5: TEdit;
    procedure FormCreate(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure Refresh;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;


implementation

type
plist = ^element;
element = record
  artist,title,genre: string[20];
  year,grade: integer;
  wsk: plist;
end;
database = file of element;

var
base: database;
first: plist;
handler: HModule;
{$R *.dfm}



procedure TForm1.Refresh();
var
current: plist;
begin
  ListView1.Clear;
  current:= first;
  while current<>nil do
  begin
    with ListView1.Items.Add do
    begin
      Caption:=current^.artist;
      SubItems.Add(current^.title);
      SubItems.Add(current^.genre);
      SubItems.Add(IntToStr(current^.year));
      SubItems.Add(IntToStr(current^.grade));
    end;
    current:=current^.wsk;
  end;
end;



procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
var Save: procedure;
begin
handler:=LoadLibrary('lib.dll');
try
  @Save:=GetProcAddress(handler, PChar(2));
  if @Save = nil then raise Exception.Create('Load nie dziala');
  Save();
finally
FreeLibrary(handler);
end;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
Load: procedure;
begin
handler:=LoadLibrary('lib.dll');
try
  @Load:=GetProcAddress(handler, PChar(1));
  if @Load = nil then raise Exception.Create('Load nie dziala');
  Load();
finally
FreeLibrary(handler);
end;
Refresh();
end;

procedure TForm1.Button1Click(Sender: TObject);
var
el: element;
Add: procedure(el:element);
begin
el.artist:=Edit1.Text;
el.title:=Edit2.Text;
el.genre:=Edit3.Text;
el.year:=StrToInt(Edit4.Text);
el.grade:=StrToInt(Edit5.Text);
handler:=LoadLibrary('lib.dll');
try
  @Add:=GetProcAddress(handler, PChar(3));
  if @Add = nil then raise Exception.Create('Load nie dziala');
  Add(el);
finally
FreeLibrary(handler);
Refresh();
{Form2:=TForm2.Create(Form1);
Form2.ShowModal;
Form2.Free;}
end;
end;
end.

dll文件如下所示:

  library lib;

{ Important note about DLL memory management: ShareMem must be the
  first unit in your library's USES clause AND your project's (select
  Project-View Source) USES clause if your DLL exports any procedures or
  functions that pass strings as parameters or function results. This
  applies to all strings passed to and from your DLL--even those that
  are nested in records and classes. ShareMem is the interface unit to
  the BORLNDMM.DLL shared memory manager, which must be deployed along
  with your DLL. To avoid using BORLNDMM.DLL, pass string information
  using PChar or ShortString parameters. }

uses
  System.SysUtils,
  System.Classes;

{$R *.res}

type plist = ^element;
element = record
  artist,title,genre:string[20];
  year,grade:integer;
  wsk: plist;
end;
database = file of element;

var
first: plist;
base: database;

procedure add(el: element); stdcall;
var current,tmp: plist;
begin
New(current);
current^ := el;
current^.wsk := nil;
if first = nil then
begin
  first:=current;
end else
begin
  tmp:=first;
  while tmp^.wsk<>nil do
  begin
    tmp:=tmp^.wsk;
  end;
  tmp^.wsk:=current;
end;

end;

procedure load();stdcall;
var
  el: element;
  i: integer;
begin
  AssignFile(base, 'baza.dat');
  if not FileExists('baza.dat') then
  begin
    Rewrite(base);
  end else
  begin
    Reset(base);
    for i := 0 to FileSize(base)-1 do
    begin
        read(base, el);
        add(el);
    end;
  end;
  CloseFile(base);
end;

procedure save();stdcall;
var
current: plist;
el: element;
begin
  AssignFile(base, 'baza.dat');
  Rewrite(base);
  current:=first;
  while current<>nil do
  begin
    el:=current^;
    el.wsk:=nil;
    write(base, el);
    current:= current^.wsk;
  end;
end;

exports
add index 1,
load index 2,
save index 3;
begin
end.

它还显示了一个错误:

预期的 ';' 但在第91行收到了标识符'index'

但是导出就像我在网上一样完成。

戴维·赫弗南

明显的错误是:

  • 您无需执行太多错误检查。您假定对电话的调用LoadLibrary总是成功的。
  • 调用约定不匹配。您可以stdcall在DLL和register可执行文件中使用。
  • 常规项目不匹配。在DLL中添加(1),加载(2)和保存(3)。在可执行文件中,您具有添加(3),加载(1)和保存(2)的功能。
  • 每次从DLL调用函数时,都将加载和卸载DLL。这意味着每次卸载DLL时,保存状态的DLL中的全局变量都会丢失。

坦白说,这段代码真是一团糟。我建议您执行以下操作:

  1. 切换到使用功能名称而不是常规名称的加载时间链接。这意味着external在可执行文件中使用关键字。通过删除对等等的所有调用LoadLibrary这将大大简化代码GetProcAddress。如果需要运行时链接,则可以在以后使用delayed关键字添加它
  2. 停止在DLL中使用全局状态,而是在模块之间来回传递信息。删除所有全局变量。但是请确保不要来回传递Delphi对象。
  3. PChar跨模块边界使用而不是短字符串。
  4. 停止使用链接列表和动态分配。很难做到这一点。TList<T>在DLL中使用以存储元素列表。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

尝试从父文档包含jQuery时,IE(any)和Firefox中出现内存不足错误

来自分类Dev

尝试在另一个对象之前创建对象时,FactoryGirl中出现错误

来自分类Dev

尝试安装Scala时Mac OS Mavericks中出现自制错误

来自分类Dev

尝试编译工作项目时,Android Studio中出现断言错误

来自分类Dev

当我尝试删除产品时,Magento中出现错误

来自分类Dev

尝试通过JavaScript显示引导模态时IE11中出现错误

来自分类Dev

尝试启动过程时出现错误

来自分类Dev

尝试编译基本JNI测试代码时VS2010中出现错误

来自分类Dev

AngularJS错误:尝试加载“模板”时出现[$ injector:nomod]

来自分类Dev

尝试使用stdin时在Python中出现错误:io.UnsupportedOperation:fileno

来自分类Dev

Win7 Virtualbox尝试启动vm时出现此错误:加载'crypt32.dll'时出错:1790

来自分类Dev

从情节提要加载时,XCTest中出现“无法投射”错误

来自分类Dev

尝试从适配器类加载活动时出现错误

来自分类Dev

尝试运行TestNG类时,Eclipse中的线程“ main”中出现错误

来自分类Dev

尝试导入头文件并使用它时在C中出现分段错误

来自分类Dev

尝试在闭包内使用常量属性时,Xcode中出现错误

来自分类Dev

尝试加载Picasso库时出现错误

来自分类Dev

绑定delphi网格宽度存储过程时出现能力错误

来自分类Dev

尝试访问Google API时Java中出现JSON错误

来自分类Dev

尝试在mysql中创建过程时出现语法错误

来自分类Dev

尝试加载xml zend Framework 2时出现错误

来自分类Dev

尝试使用DLL时出现库错误

来自分类Dev

尝试加载大于4的地图时出现错误

来自分类Dev

AngularJS错误:尝试加载“模板”时出现[$ injector:nomod]

来自分类Dev

存储过程中出现错误1064

来自分类Dev

尝试从适配器类加载活动时出现错误

来自分类Dev

尝试加载列表时出现 PHP 错误

来自分类Dev

尝试实施 Fody 加载项时出现错误

来自分类Dev

尝试创建外键时在 MySQL 中出现错误 1822

Related 相关文章

  1. 1

    尝试从父文档包含jQuery时,IE(any)和Firefox中出现内存不足错误

  2. 2

    尝试在另一个对象之前创建对象时,FactoryGirl中出现错误

  3. 3

    尝试安装Scala时Mac OS Mavericks中出现自制错误

  4. 4

    尝试编译工作项目时,Android Studio中出现断言错误

  5. 5

    当我尝试删除产品时,Magento中出现错误

  6. 6

    尝试通过JavaScript显示引导模态时IE11中出现错误

  7. 7

    尝试启动过程时出现错误

  8. 8

    尝试编译基本JNI测试代码时VS2010中出现错误

  9. 9

    AngularJS错误:尝试加载“模板”时出现[$ injector:nomod]

  10. 10

    尝试使用stdin时在Python中出现错误:io.UnsupportedOperation:fileno

  11. 11

    Win7 Virtualbox尝试启动vm时出现此错误:加载'crypt32.dll'时出错:1790

  12. 12

    从情节提要加载时,XCTest中出现“无法投射”错误

  13. 13

    尝试从适配器类加载活动时出现错误

  14. 14

    尝试运行TestNG类时,Eclipse中的线程“ main”中出现错误

  15. 15

    尝试导入头文件并使用它时在C中出现分段错误

  16. 16

    尝试在闭包内使用常量属性时,Xcode中出现错误

  17. 17

    尝试加载Picasso库时出现错误

  18. 18

    绑定delphi网格宽度存储过程时出现能力错误

  19. 19

    尝试访问Google API时Java中出现JSON错误

  20. 20

    尝试在mysql中创建过程时出现语法错误

  21. 21

    尝试加载xml zend Framework 2时出现错误

  22. 22

    尝试使用DLL时出现库错误

  23. 23

    尝试加载大于4的地图时出现错误

  24. 24

    AngularJS错误:尝试加载“模板”时出现[$ injector:nomod]

  25. 25

    存储过程中出现错误1064

  26. 26

    尝试从适配器类加载活动时出现错误

  27. 27

    尝试加载列表时出现 PHP 错误

  28. 28

    尝试实施 Fody 加载项时出现错误

  29. 29

    尝试创建外键时在 MySQL 中出现错误 1822

热门标签

归档