用DELPHI开发DLL,如果其中包含有需要传递字符串的函数,那么怎么DLL就只能仅有着一个函数,如果添加其它函数,就会引起传递的字符串错误或内存冲突。我找了很多的资料,但都没有好的解决办法。难道delphi就只能开发这样的DLL吗?那位老大有这方面的经验,请不吝赐教!
解决方案 »
- Delphi 调用 VC 写的 DLL 提示下列错误,跪求帮忙!
- !!怎么禁止ADO的自动简繁转换功能!!
- 请教变量生命周期问题
- 关于coolcontrols这个控件的
- 关于DBchart的问题.急急急
- valuelisteditor控件在那个块里?
- QuickReport报表在XP下怎么不能用呢???
- 在TDBGrid控件中的Columns中,在PickList属性中添加值,如何在TDBGrid添加值时,光标移到该Columns时,让其自动展开,变成下拉列表选择?
- 通过两个modem连接两台计算机,实现文件共享,在线等待!
- web调用ocx
- 尋找打開UG\PROE\SOILDWORKS文件的方法,急用!!!
- access数据库问题:如何在程序中设置和修改某一字段的默认值呢?
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
ShareMem,
SysUtils,
Classes;
{$R *.res} function isRuning:integer;
begin
if threadcount>0 then
result:=1
else
result:=0;
end; function StartSend:integer;
begin
result:=1;
readsetupfile;
sendaction;
end;function SetThreadNum(i:integer):integer;
begin
threadnum:=i;
result:=1;
end;function SetParameter(WPara:pchar;Lpara:pchar):integer;
begin
if Wpara='connstr' then
begin
Connstr:=lpara;
result:=1;
end;
result:=0;end;exports
startsend,
isRuning,
SetThreadNum,
SetParaMeter;begin
end.这是测试的unit frmmain;interfaceuses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;type
TForm1 = class(TForm)
btn1: TButton;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}
function StartSend:boolean; stdcall; external 'dlltest.dll' name 'StartSend';
function isRuning:boolean; stdcall; external 'dlltest.dll' name 'isRuning';
function SetThreadNum(i:integer):boolean; stdcall; external 'dlltest.dll' name 'SetThreadNum';
function SetParameter(Wpara:pchar;Lpara:pchar):boolean; stdcall; external 'dlltest.dll' name 'SetParameter';procedure TForm1.btn1Click(Sender: TObject);
var
tf:boolean;
value:string;
begin tf:=isruning;
if tf then
showmessage('send....!')
else
showmessage('send finished'); tf:=SetThreadNum(20);
if tf then
showmessage('y')
else
showmessage('n'); tf:=SetParameter('connstr','teststr');
if tf then
showmessage('connstry')
else
showmessage('n');
end;end.在测试运行时,在tf:=SetParameter('connstr','teststr');这一句时,系统就会报内存冲突。单步调试时,在DLL中读不到Wpara的值,到if Wpara='connstr' then 这一句时,就会报错。其它的函数可以正常传递并返回!
如果去掉DLL中的其它几个函数后,字符串就可以正常传递参数下去了。并可以赋值给connstr
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
ShareMem,
SysUtils,
Classes;
{$R *.res} function isRuning:integer;STDCALL;
begin
if threadcount>0 then
result:=1
else
result:=0;
end; function StartSend:integer;STDCALL;
begin
result:=1;
readsetupfile;
sendaction;
end;function SetThreadNum(i:integer):integer;STDCALL;
begin
threadnum:=i;
result:=1;
end;function SetParameter(WPara:pchar;Lpara:pchar):integer;STDCALL;
begin
if Wpara='connstr' then
begin
Connstr:=lpara;
result:=1;
end;
result:=0;end;exports
startsend,
isRuning,
SetThreadNum,
SetParaMeter;begin
end.