一个DLL的问题!!!(20分)

  • 主题发起人 主题发起人 guhui
  • 开始时间 开始时间
G

guhui

Unregistered / Unconfirmed
GUEST, unregistred user!
[red][/red]DLL代码如下:
library QueryPY;

uses
; Windows,SysUtils;

function QueryWholePY(sHZ:string):string;stdcall;
begin
; Result:='abc';
end;

exports
; QueryWholePY;

end.

程序如下

unit Unit1;

interface

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

type

; TForm1 = class(TForm)
; ; Edit1: TEdit;
; ; Button1: TButton;
; ; Edit2: TEdit;
; ; procedure Button1Click(Sender: TObject);
; private
; ; { Private declarations }
; public
; ; { Public declarations }
; end;

var
; Form1: TForm1;
; function QueryWholePY(sHZ:String):string;stdcall;external 'QueryPY.dll';
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);

begin

; Edit2.Text:=QueryWholePY(Edit1.Text);

end;

运行出现如下错误:
Invalid pointer operation.

将dll中的Result:='abc'变为Result:='',即空,就无错误!

为什么?
 
我也很想知道这个错误的产生的原因到底是什么,像超星它在下载书的时候就会经常出现这个错误。
 
千万别用string类型,改用PChar。
 
string改为widestring
 
加一个SHAREMEM对象,DLL向导里的注释说的很清楚了,要不就用Pchar指针

;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.
 
用pchar指针方式
 
多人接受答案了。
 

Similar threads

I
回复
0
查看
791
import
I
I
回复
0
查看
777
import
I
I
回复
0
查看
649
import
I
I
回复
0
查看
585
import
I
后退
顶部