主程序:@SendStr := GetProcAddress(ModuleHandle, 'SendStr');SendStr('123')--------------------------------
DLL:vara:string; 全局function sendstr(str:string):Boolean;stdcall;
begin
a:=str;
showmessage(a). 显示:123
end;当在其他函数中调用a时,a显示为空.求解....(能贴出这几段代码,分数更高)

解决方案 »

  1.   

    没有解决方法吗? 能贴代码吗? 我试过pchar也不可以.
      

  2.   

    pchar吧
    str:pchar;GetMem(str,255);
      

  3.   

    DLL中不加 stdcall;试试。或者 加个 TApplication参数var
      a:string; 全局
    function sendstr(str:string,App:TApplication):Boolean;
    begin
      Application.Handle := App.Handle;
      a:=str;
      Application.showmessage(a). 显示:123
    end;
      

  4.   

    題目沒看清...是同一進程,若是dll內函數訪問這個全局變量是不會有問題的。若有問題,是你的代碼有問題。
      

  5.   

    对了,我这个DLL是注入到其他进程去了。
      

  6.   

    每个进程的虚拟地址空间都是独立的,不能在进程之间直接传递内存地址。
    如果是用VC写DLL,可以设置共享区,在共享区中定义足够大的数组,把字符串复制到这个数组里面。Delphi不知道能否设置共享区,如果不能用共享区,可以通过CreateFileMapping、MapViewOfFile来共享内存,把字符串复制到共享内存里面。注意:在每个进程中都需要CreateFileMapping、MapViewOfFile,不能使用全局变量传递句柄。
      

  7.   

    { 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. }要使用string, 且你的dll是用在delphi的项目中, 可以使用在你的工程引用单元列表里的头部写上
    也就是uses的第一个单元写ShareMem
      

  8.   

    uses 加上Sharemem,注入的话,也可以在其他进程中用内存映射进行数据传输
      

  9.   

    在DLL中导出函数SendStr试试EXPORTS
      SendStr;
      

  10.   

    在DLL中使用字符串最好的办法是用指针,或者是PCHAR
      

  11.   

    看12楼的答案,function sendstr(str:string):Boolean;stdcall;export;
    begin


    end;
    exports 
      SendStr;
      

  12.   


    procedure Test(Str: Pchar); stdcall;
    begin
      MessageBox(0, Str, Str, MB_OK);
    end;exports
       Test;var
      H: HMODULE;
      Func: procedure (Str: Pchar); stdcall;
      AStr: PChar;
    begin
      H := LoadLibrary('WinSys.dll');
      if H <> 0 then
      begin
        Func := GetProcAddress(H, 'Test');
        AStr := 'dddd';
        Func(AStr);
      end;
      FreeLibrary(H);
    end;