dll问题,如何返回string

mindog 2004-04-30 11:05:21
//////////dll中是这样声明的:
function ReadDbInfo(section:string;databasetype:integer):string;
var
MyIniFile: TIniFile;
PassWord,UserID,DataSource,Defaultdb,ss:string;
begin

MyIniFile := TIniFile.Create('C:\CJSMS\DBINFO.ini');
if databasetype=1 then
begin
with MyIniFile do
begin
Password:=ReadString(Section, 'PassWord','000000');
userid:=ReadString(Section, 'USERID','000000');
DATASOURCE:=ReadString(Section, 'DATASOURCE','000000');
end;

result:='Provider=MSDAORA.1;Password='+PASSWORD+
';User ID='+USERID+';Data Source='+
DATASOURCE+';Persist Security Info=True';
end
else begin
with MyIniFile do
begin
Password:=ReadString(Section, 'PassWord','000000');
userid:=ReadString(Section, 'USERID','000000');
DATASOURCE:=ReadString(Section, 'DATASOURCE','000000');
DEFAULTDB:= ReadString(Section, 'DATABASE','000000');
end;
result:='Provider=SQLOLEDB.1;Password='+PASSWORD+';Persist Security Info=True;User ID='+USERID+
';Initial Catalog='+DEFAULTDB+';Data Source='+DATASOURCE;
end;
MyIniFile.Free;

end;


//////////////////////unit中的声明及引出

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
TForm1 = class(TForm)
Button1: TButton;
Memo1: TMemo;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
Function ReadDBinfo(section:string;databasetype:integer):string;
var
Form1: TForm1;

implementation
function ReadDBinfo; external 'sms' index 4;
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
begin
memo1.Lines.Add(readdbinfo('Ora_48',1));
end;

end.

调用时出现错误,"Invalid pointer operation"
...全文
215 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
diecode 2004-06-25
  • 打赏
  • 举报
回复
uses shareMem;
Pitt_Zhou 2004-06-23
  • 打赏
  • 举报
回复
我也遇到了同样的问题,调用时出现错误,"Invalid pointer operation"
楼主,只要在调用的工程和dll的uses第一行加上ShareMem,再将borlandmm.dll放到工程文件夹下就搞定了。
program prjCallMyDLL;

uses
ShareMem,
Forms,
untMain in 'untMain.pas' {Form1};

{$R *.res}

begin
Application.Initialize;
Application.CreateForm(TForm1, Form1);
Application.Run;
end.

library MyDLL;

{ 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
ShareMem,
SysUtils,
Classes
Unit1 in 'Unit1.pas';

{$R *.res}

exports
ReadDbInfo;

begin
end.
delphi99 2004-05-02
  • 打赏
  • 举报
回复
应该把相关的string改成PChar
fim 2004-04-30
  • 打赏
  • 举报
回复
dll中的声明改为Pchar,看看api的声明你就知道了
bottom 2004-04-30
  • 打赏
  • 举报
回复
pchar好和C的 CHAR*容易接轨!
ttch 2004-04-30
  • 打赏
  • 举报
回复
用PCHAR和STRING差不多 uses shareMem有的时候会出错 而且你编译后还需要把borlandmm.dll拷贝到你的程序目录下才可以发布到没有delphi的环境中
aiirii 2004-04-30
  • 打赏
  • 举报
回复
代碼都不用改,就是直接用
uses shareMem;
insert2003 2004-04-30
  • 打赏
  • 举报
回复
呵呵
要为PChar!
bxh2dai 2004-04-30
  • 打赏
  • 举报
回复
参数改成Pchar的
hujinger 2004-04-30
  • 打赏
  • 举报
回复
delphi使用dll时,请将你们的string全部改为PChar
wanwangzhiwang 2004-04-30
  • 打赏
  • 举报
回复
参数改成Pchar的,其他的都不用改
mindog 2004-04-30
  • 打赏
  • 举报
回复
只是返回值改为pchar,还是所有的都要改呀?
aiirii 2004-04-30
  • 打赏
  • 举报
回复
兩邊都

uses ShareMem
就可
bottom 2004-04-30
  • 打赏
  • 举报
回复
是的!

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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