怎样解决DLL中函数返回一个字符串时产生的'Invalid pointer operation'错误?(50分)

W

wolf_zj

Unregistered / Unconfirmed
GUEST, unregistred user!
一个DLL库,当被调用时,返回值为一字符串。但每当程序调用时,在返回字符串的
同时也产生了“Invalid pointer operation”错误,百思不得其解,请高手指点!谢谢!
 
是Delphi调Delphi吗
 
这跟某个指针类型没有被正确初始化有关,我以前也遇到多.仔细检查下自己的代码吧.
 
在DELPHI中,字符串的内存是自动分配和释放的。解决办法是:
use
sharemem,
在DLL和调用程序中,必须是第一个。
 
如果你是用string,还是改为pchar好!
你那个错误是没有初始化变量!(分配内存)
 
各位大侠,好象还是老问题啊。只是这次调用的时候没出错,程序退出的时候才出错的。
想来想去还是把源代码拉上来麻烦各位看看

库代码
unit VerifyPass;
interface
uses
ShareMem, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, DateUtils;
//中间一些代码就删了
var
Form1: TForm1;
function VP(sPppoe:Integer):AnsiString;Stdcall;
implementation
{$R *.dfm}
function VP(sPppoe:Integer):AnsiString;Stdcall;
var
year,month,day,hour,minute,second,msecond:Word;
sVer:Integer;
begin
Result:='';
DecodeDateTime(Now,year,month,day,hour,minute,second,msecond);
sVer:=year+month+day+hour+minute+second+msecond;
if sPppoe<>sVer then
Result:='Provider=MSDAORA.1;Password=123;User ID=PLH;Data Source=p41600;Persist Security Info=True'
else
Result:='44332';
end;
end.

调用程序代码
unit Unit1;

interface

uses
ShareMem, Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, DateUtils;
type
TForm1 = class(TForm)
Button1: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;
function VP(sPppoe:Integer):AnsiString;Stdcall;external 'Orapub.DLL';
implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
Sum,year,month,day,hour,minute,second,msecond:Word;
LibHandle:THandle;
begin
try
LibHandle := LoadLibrary('Orapub.DLL');
if LibHandle<>0 then
begin
DecodeDateTime(Now,year,month,day,hour,minute,second,msecond);
Sum:=year + month + day + hour + minute + second + msecond;
Label1.Caption:=VP(Sum);
end
else
Application.MessageBox(' 动态连接库加载失败! ','错误',MB_OK+MB_ICONSTOP);
finally
FreeLibrary(LibHandle);
end;
end;

end.

 
function VP(sPppoe:Integer):AnsiString;Stdcall;external 'Orapub.DLL';
===========================
你这是静态调用动态库,把你的LibHandle := LoadLibrary('Orapub.DLL');
和FreeLibrary(LibHandle);都删掉;把Orapub.dll和你的应用程序放在一个目录下;
调用声明这样:在interface中
Function VP;external 'Orapub.DLL';
在implement 中:
function VP(sPppoe:Integer):AnsiString;Stdcall;

OK!
 
楼上的兄弟,还是不行啊,照你说的改了,程序又说没有声明返回值什么的一大堆错误,
想不通啊。
 
把你的代码贴出来,或者mail to me
swarmmail@163.net
 
代码就在上面粘着呢
 
我也碰到过,用widestring行不行?
 
我也碰到了同一个问题,就是解决不了,没办法只好用整型返回了。:-{
谁解决了说一声啊!
 
这个问题真的很重要,但你给的分少,所以人家高手都不来看。:-{
 
顶部