DLL 没用string 怎么还提示delphi invalid pointer operation

fscnt 2009-12-05 01:27:23
//调用程序
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TStrArr = array of Pchar;
type
TForm1 = class(TForm)
Button1: TButton;
Edit1: TEdit;
procedure Button1Click(Sender: TObject);
end;
var
Form1: TForm1;
implementation
{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
type
TDllReadCard = function(): TStrArr; stdcall;
var
DllReadCard: TDllReadCard;
DLLHandle: THandle;
sStrArr: TStrArr;
begin
SetLength(sStrArr, 10);
DLLHandle := LoadLibrary('Project2.dll');
if DLLHandle <= 0 then
raise Exception.Create('加载Project2.dll失败!');
@DllReadCard := GetProcAddress(DLLHandle, 'FDLL');
if not (@DllReadCard = nil) then
begin
sStrArr := DLLReadCard();
end;
edit1.Text := sStrArr[2];
end;
end.


dll程序
library Project2;

uses
SysUtils,
Classes;
type
TStrArr = array of Pchar;

{$R *.res}

function FDLL(): TStrArr; stdcall;
var
sResult: TStrArr;
begin
SetLength(sResult, 10);
sResult[1] := Pchar('是');
sResult[2] := Pchar('否');
Result := sResult;
end;

exports
FDLL;
begin

end.
...全文
366 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
liangpei2008 2009-12-05
  • 打赏
  • 举报
回复
string与动态数组原理是一样的,需要加ShareMem!
要不你就用静态数组
fscnt 2009-12-05
  • 打赏
  • 举报
回复
我是要返回一个数组..
yumenyoudian 2009-12-05
  • 打赏
  • 举报
回复
unit Unit1;

interface

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

type
TForm1 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TStrArr = array of Pchar;
var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
type
TDllReadCard = procedure(var StrAyy: TStrArr); stdcall;
var
DllReadCard: TDllReadCard;
DLLHandle: THandle;
sStrArr: TStrArr;
begin
DLLHandle := LoadLibrary('Project2.dll');
if DLLHandle <= 0 then
raise Exception.Create('加载Project2.dll失败!');
@DllReadCard := GetProcAddress(DLLHandle, 'FDLL');
SetLength(sStrArr, 10);
if not (@DllReadCard = nil) then
begin
DLLReadCard(sStrArr);
end;
Caption := sStrArr[2];
end;
end.


library Project2;

{ 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,
Classes;

type
TStrArr = array of Pchar;

{$R *.res}

procedure FDLL(var AStrArr: TStrArr); stdcall;
var
sResult: TStrArr;
n, nlength: Integer;
begin
nlength := length(AStrArr);
for n := 0 to nlength - 1 do
begin
if n = 1 then
AStrArr[n] := Pchar('是');
if n = 2 then
AStrArr[n] := Pchar('否');
end;
end;

exports
FDLL;
begin

end.
fscnt 2009-12-05
  • 打赏
  • 举报
回复
没明白你的意思。
Seamour 2009-12-05
  • 打赏
  • 举报
回复
是没用长字符串类型,但你用动态数组不一样么
fscnt 2009-12-05
  • 打赏
  • 举报
回复
怎么没人理我。都放假了吗

5,928

社区成员

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

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