+ -
当前位置:首页 → 问答吧 → DLL中能否使用RegisterClass?

DLL中能否使用RegisterClass?

时间:2011-11-29

来源:互联网

DLL中能否使用RegisterClass?
请教各位高人,能否住DLL中使用RegisterClass注册类,并住调用程序软件代码中使用GetClass将其取出?为什么俺每次都非到?谢谢! 
//MYLIB.DLL:

initialization
  RegisterClass(TMyClass);
finalization
  UnregisterClass(TMyClass);
end.

//Application:
...
  LoadLibrary('MYLIB.DLL');
  if GetClass('TMyClass') = nil 
  then ShowMessage('Class not found');

怎么都找不到?要怎么弄呢?


 

作者: case5166   发布时间: 2011-11-29

dll和exe中的类信息是不同的吧,想要使用的话,可以使用包

作者: funxu   发布时间: 2011-11-29

dll中RegGroups中含有你的相关类的注册信息 
exe中的RegGroups中含有你的相关类的注册信息
------------------------------------------------
额,,我在网上查了下是这样说的,但是我现在有一套源码却是采用DLL来做的,在library界面中也没有看见有有导出方法来SHOW窗体,
其下有注册类
initialization
  RegisterClassE(TCallProcTestfrm);
finalization
  UnRegisterClassE(TCallProcTestfrm);
但源代码不完整,所以不怎么明白是怎么实现的

作者: case5166   发布时间: 2011-11-29

引用一段原话
If a class is registered in a dynamic-link library (DLL) with the RegisterClass() function and an application uses this class when creating a window or a dialog box, then the CreateDialog() or CreateWindow() call in the application will fail. The debug version of Windows will produce a Fatal Exit code 0x8800 with the following message:

作者: funxu   发布时间: 2011-11-29

源代码里是不是dll里提供了窗体调用函数,由exe来调用呢?

作者: funxu   发布时间: 2011-11-29

找到说明了
To register a window class in a DLL and subsequently create a window (or create a dialog box) of that class in any application that links into this DLL, the window class must be registered with the CS_GLOBALCLASS class style. This style specifies that the window class is an application global class, and will allow this class to be accessed globally by all applications. The CS_GLOBALCLASS class style is documented in the Windows version 3.1 SDK "Programmer's Reference, Volume 1: Overview," manual on page 19 under the Class Styles section.
 

不过暂时只能帮这么多了,dll注册类用的不多

作者: funxu   发布时间: 2011-11-29

DLL里面没有,但看到其引用了个BPL包,在BPL包中写了很多类注册的函数在里面,
难道二者结合后可以从BPL中获得---?
------------BPL中一窗体管理单元
interface
uses Classes,SysUtils;

type
  TManagerForm=class(TObject)
  private
  FClassList:TList;
  function GetClass(Index: Integer): TPersistentClass;
  function GetCount: Integer;
  public
  constructor Create;
  destructor Destroy;override;
  function FindClassE(AClass:TPersistentClass):Integer;
  procedure RegisterClassE(AClass:TPersistentClass);
  procedure UnRegisterClassE(AClass:TPersistentClass);
  property Items[Index:Integer]:TPersistentClass read GetClass;
  property Count:Integer read GetCount;
  end;
  procedure RegisterClassE(AClass:TPersistentClass); //注册窗体类函数
  procedure UnRegisterClassE(AClass:TPersistentClass); //反注册窗体类函数
var
  GoManagerForm:TManagerForm;

implementation

procedure RegisterClassE(AClass:TPersistentClass);
begin
  GoManagerForm.RegisterClassE(AClass);
end;

procedure UnRegisterClassE(AClass:TPersistentClass);
begin
  GoManagerForm.UnRegisterClassE(AClass);
end;

{ TManagerForm }

constructor TManagerForm.Create;
begin
  FClassList:=TList.Create;
end;

destructor TManagerForm.Destroy;
begin
  FClassList.Free;
  inherited;
end;

function TManagerForm.FindClassE(AClass: TPersistentClass): Integer;
var
  i:Integer;
  loClass:TPersistentClass;
begin
  Result:=-1;
  for i:=0 to Count-1 do
  begin
  loClass:=TPersistentClass(FClassList[i]);
  if AClass=loClass then
  begin
  Result:=i;
  Exit;
  end;
  end;
end;

function TManagerForm.GetClass(Index: Integer): TPersistentClass;
begin
  if (Index>-1) and (Index<Count) then
  Result:=TPersistentClass(FClassList[Index])
  else
  Result:=nil;
end;

function TManagerForm.GetCount: Integer;
begin
  Result:=FClassList.Count;
end;

procedure TManagerForm.RegisterClassE(AClass: TPersistentClass);
var
  Index:Integer;
begin
  Index:=FindClassE(AClass);
  if Index<0 then
  begin
  FClassList.Add(AClass);
  try
  RegisterClass(AClass);
  except
  end;
  end;
end;

procedure TManagerForm.UnRegisterClassE(AClass: TPersistentClass);
var
  Index:Integer;
begin
  Index:=FindClassE(AClass);
  if Index>=0 then
  begin
  FClassList.Delete(Index);
  try
  UnRegisterClass(AClass);
  except
  end;
  end;
end;

initialization
  GoManagerForm:=TManagerForm.Create;
finalization
  GoManagerForm.Free;
end.

作者: case5166   发布时间: 2011-11-29

引用 5 楼 funxu 的回复:
找到说明了
To register a window class in a DLL and subsequently create a window (or create a dialog box) of that class in any application that links into this DLL, the window class must be registered with……


没怎么看明白其意思---这方面其实自己也不怎么用得上,本想学习下其思路
似乎这个对自己来说有点难了

作者: case5166   发布时间: 2011-11-29

结合3L的内容,意思是说,如果你使用普通的方法注册窗体类或者dialog在dll中,主程序是无法访问的,你需要把dll中的类注册为CS_GLOBALCLASS全局类型

作者: funxu   发布时间: 2011-11-29

一般的DLL和EXE的Classes是各自一份.所以你在DLL中RegisterClass在其他模块是取不到的.
除非你的DLL和EXE都是带运行时包运行的.Classes才会都用运行时包中的

作者: wr960204   发布时间: 2011-11-29

热门下载

更多