2个简单的关于dll调用的问题

tdtjjiao 2005-08-19 09:44:08
现在有一dll不知道怎么调用。请高手列出静态和动态的调用方法。
library delphi;
{ 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
SysUtils,
Clipbrd,
DBGrids,
forms,
Classes;

{$R *.res}

function CopyDBGrid(DBGrid: TDBGrid):tdbgrid;
const
SEP = #9; //field之间的分隔
CRLF= #13#10;//回车换行
var
i,j : Integer;
s : String;
begin
For i := 0 to Dbgrid.Columns.Count-1 do
begin
if i > 0 then s := s + SEP;
s := s + dbgrid.Columns[i].FieldName;
end;
s := s + CRLF;
if DBGrid.SelectedRows.Count>0 then
begin
with DBGrid.DataSource.DataSet do
begin
for i:=0 to DBGrid.SelectedRows.Count-1 do
begin
GotoBookmark(pointer(DBGrid.SelectedRows.Items[i]));
for j := 0 to FieldCount-1 do
begin
if (j>0) then s:=s+SEP;
s:=s+Fields[j].AsString;
end;
s := s + CRLF;
end;
end;
end;
ClipBoard.SetTextBuf(pchar(s));
end;
Exports CopyDBGrid;
begin
end.
...全文
147 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
tfeiy 2005-08-19
  • 打赏
  • 举报
回复
努力学习中...
tdtjjiao 2005-08-19
  • 打赏
  • 举报
回复
还有
procedure CopyDBGrid(DBGrid: TDBGrid);
function CopyDBGrid(DBGrid: TDBGrid):tdbgrid;
在dll调用上有不同的地方吗?
tdtjjiao 2005-08-19
  • 打赏
  • 举报
回复
我静态调用的时候总是出错。
procedure CopyDBGrid(DBGrid: TDBGrid);
external 'Project1.dll' name 'CopyDBGrid';
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
CopyDBGrid(DBgrid1);
end;
请问这样有问题吗?

在第一次执行的时候没有问题,但第二次执行就会出错。
错误
Project D:\程序\新建文件夹\Project2.exe faulted with message:'access violation at 0x0040ca6d:write of address 0x00030f6c'.Process Stopped. Use Step or Run to continue.
这是什么问题啊?
konhon 2005-08-19
  • 打赏
  • 举报
回复
靜態調用:
主程序在调用该DLL时,首先在interface部分声明要调用的函数:
function CopyDBGrid(DBGrid: TDBGrid):TDBGrid;stdcall;external 'delphi.dll'
name 'CopyDBGrid';
声明后就可以像用delphi普通函数一样使用啦.


動態調用:
procedure TMainForm.Button1Click(Sender: TObject);
var
 OneHandle : THandle; //定义一个句柄变量
begin
 OneHandle := LoadLibrary('delphi.dll'); //动态载入DLL,并返回其句柄
 try
  if OneHandle <> 0 then //如果载入成功则获取ShowCalendar函数的地址
   @ShowCalendar := GetProcAddress(OneHandle, 'CopyDBGrid');
   if not (@ShowCalendar = nil) then
// CopyDBGrid
   else
    RaiseLastWin32Error;
 finally
  FreeLibrary(OneHandle); //调用完毕收回DLL占用的资源
 end;
end;

tdtjjiao 2005-08-19
  • 打赏
  • 举报
回复
没人知道吗?
挺简单的啊。高手快来啊。
tdtjjiao 2005-08-19
  • 打赏
  • 举报
回复
还有
procedure CopyDBGrid(DBGrid: TDBGrid);
function CopyDBGrid(DBGrid: TDBGrid):tdbgrid;
在dll调用上有同的地方吗?
tdtjjiao 2005-08-19
  • 打赏
  • 举报
回复
我还没做动态那,只做了一个静态的,
我的程序,运行第一次和第二次的时候没有问题在第三次以后才出现问题的。

关闭程序,重新运行后,还是第三次出错,不知道为什么。
郁闷
tdtjitxypm 2005-08-19
  • 打赏
  • 举报
回复
我还没做动态那,只做了一个静态的,
我的程序,运行第一次和第二次的时候没有问题在第三次以后才出现问题的。

关闭程序,重新运行后,还是第三次出错,不知道为什么。
郁闷
snjun 2005-08-19
  • 打赏
  • 举报
回复
你的语法有问题
自己看看书吧,蛮简单的东西
buddycc 2005-08-19
  • 打赏
  • 举报
回复
你动态调用成功了吗?能不能把你动态调用的过程show一下呢???
konhon 2005-08-19
  • 打赏
  • 举报
回复
應該是你的DLL有問題吧.
應該只是聲明的時候不同吧, 調用一樣的.
还有
procedure CopyDBGrid(DBGrid: TDBGrid);
function CopyDBGrid(DBGrid: TDBGrid):tdbgrid;
在dll调用上有不同的地方吗?
tdtjitxypm 2005-08-19
  • 打赏
  • 举报
回复
顶一下。

5,388

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 开发及应用
社区管理员
  • VCL组件开发及应用社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧