主程序:@SendStr := GetProcAddress(ModuleHandle, 'SendStr');SendStr('123')--------------------------------
DLL:vara:string; 全局function sendstr(str:string):Boolean;stdcall;
begin
a:=str;
showmessage(a). 显示:123
end;当在其他函数中调用a时,a显示为空.求解....(能贴出这几段代码,分数更高)
调试欢乐多
str:pchar;GetMem(str,255);
a:string; 全局
function sendstr(str:string,App:TApplication):Boolean;
begin
Application.Handle := App.Handle;
a:=str;
Application.showmessage(a). 显示:123
end;
如果是用VC写DLL,可以设置共享区,在共享区中定义足够大的数组,把字符串复制到这个数组里面。Delphi不知道能否设置共享区,如果不能用共享区,可以通过CreateFileMapping、MapViewOfFile来共享内存,把字符串复制到共享内存里面。注意:在每个进程中都需要CreateFileMapping、MapViewOfFile,不能使用全局变量传递句柄。
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
SendStr;
begin
end;
exports
SendStr;
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;