KeyLife富翁笔记
作者: HongYuan
标题: 在DLL中怎么共用一个全局变量
关键字:
分类: 开发经验
密级: 保护
(评分: , 回复: 0, 阅读: 190) »»

有几个朋友经常向我问题在DLL中怎么共用一个全局变量。比如像用户登陆后的用户名(UserName)...
其实这个问题很简单。下面我把我的做法写出来大家一起分享。

把共享的变量放在主程序里:UserName,....等等,
在主程序里写两个函数
Function CurUserName():PChar;
begin
 Result:=pchar(UserName);
end;

procedure SetCurUserName(LoginUserName: PChar)
begin
 UserName:=LoginUserName;
end;

然后在主程序的工程文件(*.dpr)里输出这两个函数里写
.....
{$R *.res}

exports
  CurUserName, SetCurUserName;
....

这样在每个DLL里就可以使用这两个函数了

function CurUserName: PChar; //从主程序里获取当前登陆帐号
var
  GetCurUserName: function: pchar;
begin
  @GetCurUserName := GetProcAddress(GetModuleHandle(pchar(application.Exename)), 'CurUserName');
  if Assigned(GetCurUserName) then //找到
    result := GetCurUserName
  else
    result := 'admin';
end;


2005-10-27 13:11:13   

相关文章:

  • 2022-12-23
  • 2021-06-15
  • 1970-01-01
  • 2022-02-07
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-08
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-12
相关资源
相似解决方案