天行健,君子以自强不息;地势坤,君子以厚德载物。


Tag: .预览模式: 普通 | 列表

Delphi中Ocx的动态注册方法

 方法1


uses windows;

type
   TDllRegisterServer = function: HResult; stdcall;
 
function RegisterOCX(FileName: string): Boolean;
var
   OCXHand: THandle;
   RegFunc: TDllRegisterServer;
begin
   OCXHand := LoadLibrary(PChar(FileName));
   RegFunc := GetProcAddress(OCXHand, 'DllRegisterServer');
   if @RegFunc <> nil then
     Result := RegFunc = S_OK
   else
     Result := False;
   FreeLibrary(OCXHand);
end;

function UnRegisterOCX(FileName: string): Boolean;
var
   OCXHand: THandle;
   RegFunc: TDllRegisterServer;
begin
   OCXHand := LoadLibrary(PChar(FileName));
   RegFunc := GetProcAddress(OCXHand, 'DllUnregisterServer');
   if @RegFunc <> nil then
     Result := RegFunc = S_OK
   else
     Result := False;
   FreeLibrary(OCXHand);
end;

方法2

procedure TForm1.Button1Click(Sender: TObject);
var
   astr:String;
   bstr:String;
   ProcAddr:Pointer;
   DllAddr:Integer;
begin
   od1.Execute;
   astr:=od1.FileName+chr(0);
   DllAddr:=LoadLibrary(PChar(astr));
   if DllAddr<>0 then
     begin
       bstr:='DllregisterServer'+chr(0);
       //只要将以上处改为DllUnRegisterServer即不注册ActiveX控件
       ProcAddr:=GetProcAddress(DllAddr,PChar(bstr));
       if ProcAddr<>nil then
         CallWindowProc(ProcAddr,DllAddr,0,0,0)
       else
           ShowMessage(od1.FileName+'不是有效的控件');
       FreeLibrary(DllAddr);
     end
   else
       ShowMessage(od1.FileName+'无法装载');
end;



uses OLEctl {, ....};
interface{...}
var  OCXHandle: THandle;
RegFunc: TDllRegisterServer;
begin
 
OCXHandle:= LoadLibrary('c:/XXX.ocx');
 
RegFunc := GetProcAddress(OCXHandle, 'DllRegisterServer');
 
if RegFunc <> 0 then ShowMessage('Error!');
 
FreeLibrary(OCXHand);
 
end;
 
或者
 
WinExec("regsvr32.exe/s yourocx.ocx",SW_NORMAL);

 

Tags: Delphi Ocx 动态注册 .

分类:Delphi技巧 | 固定链接 | 评论: 0 | 引用: 0 | 查看次数: 218