用代码实现注册Ocx和Dll

在windows系统中,可以通过Regsvr32来实现注册ocx或者dl,

编程时,调用Regsvr32来注册,却不能正常执行。尤其是在Win7系统中,需要管理员身份才能运行。

使用下面的代码则能正常注册。

//注册

function RegisterDllServer(FileName: string): boolean;
var
  nDllAddr: integer;
  bstr: string;
  ProcAddr: Pointer;
  str: PAnsiChar;
begin
  Result := False;

  if not FileExists(FileName) then
    exit;
   
  nDllAddr:=LoadLibrary(PWideChar(FileName));

  if nDllAddr <> 0 then
  begin
    bstr:='DllRegisterServer'+chr(0);
   
    str := PAnsiChar(AnsiString(bstr));
    ProcAddr := GetProcAddress(nDllAddr,str);
    if ProcAddr<>nil then
    begin
      CallWindowProc(ProcAddr,nDllAddr,0,0,0);
      Result := True;
    end;

    FreeLibrary(nDllAddr); 
  end;                    
end;

 

 

//反注册

function UnRegisterDllServer(FileName: string): boolean;
var
  nDllAddr: integer;
  bstr: string;
  ProcAddr: Pointer;
  str: PAnsiChar;
begin
  Result := False;
 
  if not FileExists(FileName) then
    exit;
   
  nDllAddr:=LoadLibrary(PWideChar(FileName));

  if nDllAddr <> 0 then
  begin
    bstr:='DllUnregisterServer'+chr(0);
   
    str := PAnsiChar(AnsiString(bstr));
    ProcAddr := GetProcAddress(nDllAddr,str);
    if ProcAddr<>nil then
    begin
      CallWindowProc(ProcAddr,nDllAddr,0,0,0);
      Result := True;
    end;

    FreeLibrary(nDllAddr); 
  end;                    
end;

你可能感兴趣的:(用代码实现注册Ocx和Dll)