从DLL函数中返回动态数组,为什么释放DLL时出错?(200)

  • 主题发起人 主题发起人 delphi002
  • 开始时间 开始时间
D

delphi002

Unregistered / Unconfirmed
GUEST, unregistred user!
DLL type TMyIntResult=Array of Integer; Procedure TestDll(Var Rst :TMyIntResult);stdcall; Var I:integer; begin SetLength(Rst,10); For i:=0 to 9 Rst :=i; end; exports TestDll; 调用EXE procedure TForm1.BtnTestClick(Sender: TObject); type TestDll=Procedure(Var Rst :TMyIntResult);stdcall; Var DllHandle:THandle; Dllfarproc:Tfarproc; MyRst :TMyIntResult begin DllHandle:=LoadLibrary('Test.dll'); if DllHandle>32 then try Dllfarproc:=GetProcAddress(DllHandle,'TestDll'); if DllfarProc<>Nil then TestDll(MyRst); finally FreeLibary(DllHandle); end; end;每次FreeLibary(DllHandle)时就出错(Invalid pointer operation)!这是为什么?是不是MyRst没有释放引起的?要如何释放?
 
dll,及调用dll的单元都要uses sharemem单元
 
这个我有这样做,不是这里的问题.
 
SetLength是不是需要释放资源啊?ZeroMemory?
 
最好还是从外分配好长度, 在 Dll 里只是赋值!主程序 与 Dll 相当于两个程序, 还是各自管理各自的内存好
 
200大洋呵呵,以下代码测试通过library Prjdll;{ 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;{$R *.res}type TMyIntResult = array of Integer; PMyIntResult = ^TMyIntResult;procedure TestDll(VAR Rst: PMyIntResult); stdcall;var I: integer;begin SetLength(Rst^, 10); for i := 0 to 9 do Rst^ := i;end;exports TestDll;end.-------------------------------------unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TMyIntResult=Array of Integer; PMyIntResult = ^TMyIntResult; TForm1 = class(TForm) Button1: TButton; Memo1: TMemo; procedure Button1Click(Sender: TObject); private { Private declarations } public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.Button1Click(Sender: TObject);type TestDll = procedure(VAR Rst: PMyIntResult); stdcall;var DllHandle: THandle; Dllfarproc: TestDll; MyRst: PMyIntResult; i:Integer;begin DllHandle := LoadLibrary('Prjdll.dll'); if DllHandle > 32 then try Dllfarproc := GetProcAddress(DllHandle, 'TestDll'); new(MyRst); if assigned(DllfarProc) then DllfarProc(MyRst); for I:=low(MyRst^) to High(MyRst^) do Memo1.Lines.Add( Inttostr(MyRst^) ); finally FreeLibrary(DllHandle); end;end;end.
 
to znxia你的代码一执行到SetLength(Rst^, 10)就出错,为什么?
 
搞定,是我搞错了,谢谢znxia!也谢谢各位参与者
 

Similar threads

I
回复
0
查看
520
import
I
I
回复
0
查看
878
import
I
I
回复
0
查看
650
import
I
I
回复
0
查看
738
import
I
后退
顶部