我的DLL如下,在VB中无法返回正确值,但调用不出错,我实验了各种数据类型,有的无返回值,有的返回一串无用的数字,怎么回事????????(100分)

  • 主题发起人 主题发起人 blackfish99
  • 开始时间 开始时间
B

blackfish99

Unregistered / Unconfirmed
GUEST, unregistred user!
library thedate;
uses
SysUtils,
Classes;
var mydate:pchar;
function makeitaday(S:integer):pchar;stdcall;
var thedate:array[0..20] of char;

begin
GetMem(mydate, 200);
if s=1 then thedate:='Sunday ';
if s=2 then thedate:='Monday ';
if s=3 then thedate:='Tuesday ';
if s=4 then thedate:='Wednesday ';
if s= 5 then thedate:='Thursday ';
if s = 6 then thedate:='Friday ';
if s= 7 then thedate:='Saturday ';
strpcopy(mydate,thedate);
makeitaday:=mydate;
end;

exports makeitaday;
begin
end.


 
为什么不用string
 
这样试试?
function makeitaday(handle:thandle;S:integer):pchar;stdcall;
我以前也遇到过这样的问题
 
程序中没有uses sharemem单元,你没看过DELPHI在DLL单元中的一段英文吗:
{ 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. }
 
在delphi 中试试有问题吗
要不换种传直方式
 
改成这样试试:
library thedate;
uses
SysUtils,
Classes;
var mydate:pchar;
function makeitaday(S:integer):pchar;stdcall;
var thedate:array[0..20] of char;

begin
GetMem(makeitaday, 12);
if s=1 then StrPCopy(thedate,'Sunday ');
if s=2 then StrPCopy(thedate,'Monday ');
/*省略*/
strpcopy(makeitaday,thedate);
end;

exports makeitaday;
begin
end.

不过这样有内存泄漏
建议你用变参
function makeitaday(S:integer;var s:Pchar):pchar;stdcall;
 
多人接受答案了。
 
后退
顶部