dll使用问题

feicui0128 2003-10-09 10:21:52
我定义了一个dll,定义的函数如下:
function getseason(date:integer):string;stdcall;
begin
case date of
1:result:='spring';
2:result:='summer';
3:result:='autumn';
4:result:='winter';
end;
end;
使用如下:
var
cc:string;
begin
cc:=getseason(2);
showmessage(cc);
end;
能显示出结果'summer',确认后系统提示错误:无效的指针操作。请问一下这是什么原因?
...全文
32 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
swayi21 2003-10-09
  • 打赏
  • 举报
回复
還是用 PChar吧。
function GetSeason(Date: Integer): PChar; stdcall;
jacky_shen 2003-10-09
  • 打赏
  • 举报
回复
{ 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. }

在与DLL有string类型的传递时候要引用ShareMem,而且要放在第一个位置上。

uses
ShareMem, <----
SysUtils,
Classes;

{$R *.RES}

begin
end.

在project中也需要添加ShareMem单元。
seatu 2003-10-09
  • 打赏
  • 举报
回复
在动态库中 建议不使用string进行返回值 使用指针进行返回是
比较好的 如char *
feicui0128 2003-10-09
  • 打赏
  • 举报
回复
为什么会出现错误呢?是什么原因?
jiayf020978 2003-10-09
  • 打赏
  • 举报
回复
以下是dll程序,可以防止出现程序关闭时出现的非法操作。
library Onedll;
uses
SysUtils,
Classes,
Windows,
Forms,

{$R *.res}
var
DLLApp: TApplication;

procedure DLLUnloadProc(Reason: Integer);
begin
if Reason = DLL_PROCESS_DETACH then Application := DLLApp; //恢复
end;

exports

begin
DLLApp := Application; //保存 DLL 中初始的 Application 对象
DLLProc := @DLLUnloadProc; //保证 DLL 卸载时恢复原来的 Application
end.
wzds2000 2003-10-09
  • 打赏
  • 举报
回复
帮你顶。。。
ZyxIp 2003-10-09
  • 打赏
  • 举报
回复
将DLL中的窗体显示出来了,可是不能停靠到主窗体中,我已设置了可停靠和允许停靠。


为什么不能停靠???用代码可停靠,可拖出来就在进不去了。

有什么好的解决办法吗???
mengxiang5160 2003-10-09
  • 打赏
  • 举报
回复
function getseason(date:integer):pchar;stdcall;
begin
case date of
1:result:='spring';
2:result:='summer';
3:result:='autumn';
4:result:='winter';
end;
end;
主程序中:
showmessage(getseason(2));
xiangwangz 2003-10-09
  • 打赏
  • 举报
回复
//你的dll中的函数写的用问题的,我的修改如下:
function getseason(date:integer):pchar;stdcall;
begin
case date of
1:result:='spring';
2:result:='summer';
3:result:='autumn';
4:result:='winter';
end;
end;
//在调用dll是没问题的,只要以下就行的,切记在delphi中写的dll最好不要用string的
showmessage(getseason(2));
delphi_xizhousheng 2003-10-09
  • 打赏
  • 举报
回复
要是不usrs ShareMem(放第一个)的话,就用windows兼容类型的PChar了

5,388

社区成员

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

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