我做了一个Dll里面只发布一个涵数;
//************************
在dll中是这样定义的(里面封装了窗体):
...................
library Project1;uses
  Unit1 in 'Unit1.pas';
exports
 dddd;
begin
end.
//涵数定义如下
function ddd( filename:Pchar;field_index:integer):Pchar;
var
input_form:Tinput_code_form;
begin
input_form:=Tinput_code_form.CreateForm(filename,field_index);
with input_form do
 begin
 if ShowModal = mrok then result:=Pchar(input_form.label1.Caption)
 else result:='';
 end;
end;//***********************************************
我用程序调用;
.......
implementation
function dddd(filename:pchar;field_index:integer):Pchar;stdcall;external'Project1.dll';//功能是返回一个人的姓名;
{$R *.dfm}
procedure TForm1.Edit1KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
if key=VK_F9 then
  showmessage(dddd(pchar('C:\sy\person_id.CTD'),0));
end;
程序能够正常运行;
但是会出现一个奇怪的现象;
当返回的姓名是三个字的是侯就回出现如:原来是"杨伧林"会显示为
"杨伧 ",好象是被截断了半个字符,但是有时侯又能正常显示"杨伧林"
真不知道是什么原因?请给解释一下?

解决方案 »

  1.   

    导出:
    function   ddd(   filename:Pchar;field_index:integer):Pchar; stdcall;
      

  2.   

    to 楼上的
    我不想用string;因为string只能用在delphi编的程序中;
    请问budded 
    导出: 
    function       ddd(       filename:Pchar;field_index:integer):Pchar;   stdcall;
    是什么意思?不明白
      

  3.   

    带上borlandMM.dll 用STRING吧,问题应该在这里。
      

  4.   

    如果带上borlandmm.dll用string的话用其它语言编的程序能调用这个dll吗?
    我想知道的是为什么pchar就不行?
      

  5.   

    to akirya 
    能具体一点吗?
      

  6.   

    大概是这样写的,不太会写delphifunction   ddd(   filename:Pchar;field_index:integer):Pchar;
    var
    input_form:Tinput_code_form;
    temp:pchar;
    begin
    input_form:=Tinput_code_form.CreateForm(filename,field_index);
    with   input_form   do
      begin
      if   ShowModal   =   mrok   then   result:=Pchar(input_form.label1.Caption)
      
      else   result:= ' ';
      end;
      //temp := result ;
      temp := SysAllocMem( result 的长度+1 );
     复制result的内容到temp;
    result:=temp;  
    end;
      

  7.   

    to 楼上的你的想法应该是行不通的;
    因为input_form.label1.Caption中显示的字符是正常的.只是返回到调用程序时才不能正常显示;
      

  8.   

    总算找到了,可还是有点不懂
    问题在于:没有释放form窗体把涵数改为:
    function   ddd(   filename:Pchar;field_index:integer):Pchar; 
    var 
    input_form:Tinput_code_form; 
    begin 
    input_form:=Tinput_code_form.CreateForm(filename,field_index); 
    try
    with   input_form   do 
      begin 
      if   ShowModal   =   mrok   then   result:=Pchar(input_form.label1.Caption) 
      else   result:= ' '; 
      end;
    finally
    input_form.free;//或是freeandnil(input_form);
    end;
    end; 可我还是弄不明白.照我的理解:showmodal=mrok就回释放form为什么还要free呢?
      

  9.   

    你知道为啥错误么?
    你返回了一个指针,这个指到一个局部变量,函数返回的时候这个局部变量已经无效.你说你返回的这个指针有意义么?
    我写的方法就是在堆上分配一段内存,只要不释放一直是有效的返回string是因为string是一个对象,delphi会保证返回的string对象是有效的.