【发布时间】:2017-01-21 19:15:55
【问题描述】:
在 Embarcadero Delphi v10.1 中,我有一个带有记录的 DLL 库和一个包含 TStringGrid 和 TEdit 的 VCL 应用程序。这个想法是把短字符串输入到 TEdit 中;将其保存到 DLL 中的记录中,然后使用记录中存储的数据填充 TStringGrid 中的一个单元格。
我的问题是,在将短字符串保存到记录后,我似乎无法在 DLL 过程中找到访问 TStringGrid 的方法。到目前为止,我已经尝试使用类和指针来访问 DLL 中的 TStringGrid,但都没有奏效:
type
pstringgrid = ^TStringGrid;
//or
type
pstringgrid = ^stringgrid1;
//or
type
istringgrid = class(TStringGrid);
我什至尝试将 TStringGrid 导入到应该将短字符串从记录输入到 TStringGrid 的过程中:
procedure AddElement (var grid : stringgrid1); stdcall;
//or
type
pstringgrid = ^TStringGrid;
procedure AddElement (var grid : ^pstringgrid); stdcall;
到目前为止,没有任何效果,我得到的只是来自调试器的“未清除标识符”错误消息;请帮忙!在 DLL 过程中如何访问和编辑 TStringGrid?
编辑:
这里是相关代码,外来变量名见谅。
DLL:
library BibliotekaDLL;
uses
System.SysUtils,
System.Classes;
type
StringGrid1 = class(TStringGrid);
plist = ^game;
game = record
nazwa: shortstring;
wydawca: shortstring;
rokwyd: integer;
gatunek1: shortstring;
gatunek2: shortstring;
pointer: plist;
end;
var
BazaDanych : file of game;
first, current: plist;
[...]
procedure WyswietlListe; stdcall;
var
row : integer;
begin
AssignFile(BazaDanych, 'c:\Baza_Danych_Gier.dat');
if not FileExists('c:\Baza_Danych_Gier.dat') then
ShowMessage ('Baza Danych Nie Instnieje' +E.Message)
else
begin
Reset(BazaDanych);
Read(BazaDanych, first);
Close(BazaDanych);
current := first;
row := 1;
while current^.pointer <> nil do
begin
current := first;
StringGrid1.Cells[0,row] := current^.nazwa;
StringGrid1.Cells[1,row] := current^.wydawca;
StringGrid1.Cells[2,row] := current^.rokwyd;
StringGrid1.Cells[3,row] := current^.gatunek1;
StringGrid1.Cells[4,row] := current^.gatunek2;
current := current^.pointer;
row = row +1;
StringGrid1.RowCount := row;
end;
if current^.pointer = nil do
begin
StringGrid1.Cells[0,row] := current^.nazwa;
StringGrid1.Cells[1,row] := current^.wydawca;
StringGrid1.Cells[2,row] := current^.rokwyd;
StringGrid1.Cells[3,row] := current^.gatunek1;
StringGrid1.Cells[4,row] := current^.gatunek2;
end;
end;
end;
[...]
以及VCL应用代码:
[...]
type
TForm1 = class(TForm)
Button2: TButton;
StringGrid1: TStringGrid;
procedure Button2Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
[...]
procedure TForm1.Button2Click(Sender: TObject);
var
Handle : THandle;
WyswietlListe : procedure;
begin
Handle := LoadLibrary('BibliotekaDLL.dll');
try
@WyswietlListe:= GetProcAddress(Handle, 'WyswietlListe');
if @WyswietlListe = nil then raise Exception.Create('Nie Można Znaleźć Procedury w Bibliotece!');
WyswietlListe;
finally
FreeLibrary(Handle);
end;
end;
[...]
【问题讨论】:
-
不明白你对指针的使用。类已经是指针。太多的间接性。除非您使用包,否则跨模块边界传递任何这些类型都不起作用。现在是 2017 年。停止使用短字符串。向我们展示minimal reproducible example,以便我们了解互操作。
-
我很同情你,但是这里有很多问题,几乎不可能知道从哪里开始。我只是没有精力做这个
标签: class delphi pointers dll tstringgrid