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"
...全文
155 14 点赞 打赏 收藏 举报
写回复
14 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
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
是的!
  • 打赏
  • 举报
回复
相关推荐
发帖
Windows SDK/API
加入

1163

社区成员

Delphi Windows SDK/API
申请成为版主
帖子事件
创建了帖子
2004-04-30 11:05
社区公告
暂无公告