zoukankan      html  css  js  c++  java
  • Registering DLL and ActiveX controls from code

    http://delphi.about.com/od/windowsshellapi/l/aa040803a.htm

    How to register (and unregister) OLE controls such as dynamic-link library (DLL) or ActiveX Controls (OCX) files from a Delphi application.

    One of the features that make Delphi so popular is that when it comes to project deployment, you as a developer (in most cases) only need to send the executable file (exe) of your application.

    However, in some situations, for example when you import an ActiveX control into your project, you'll need to make sure that this ActiveX control is registered on your users machines. If the control is not registered there, an EOleSysError exception will be displayed to your user eyes.

    RegSvr32.exe
    The regsvr32.exe command-line tool registers dll and ActiveX controls on a system. You can manually use the Regsvr32.exe (Windows.Start - Run) to register and unregister OLE controls such as dynamic link library (DLL) or ActiveX Controls (OCX) files that are self-registerable. 
    When you use Regsvr32.exe, it attempts to load the component and call its DLLSelfRegister function. If this attempt is successful, Regsvr32.exe displays a dialog indicating success.

    RegSvr32.exe has the following command-line options:

    Regsvr32 [/u] [/s] [/n] [/i[:cmdline]] dllname
     /s - Silent; display no message boxes
     /u - Unregister server
     /i - Call DllInstall passing it an optional [cmdline]; when used with /u calls dll uninstall
     /n - do not call DllRegisterServer; this option must  be used with /i

    From Delphi code

    To call the regsvr32 tool from within Delphi code, you'll need a function that can execute a file and wait for the execution to finish.

    This is how the 'RegisterOCX' procedure could look:

    procedure RegisterOCX;
    type
      TRegFunc = function : HResult; stdcall;
    var
      ARegFunc : TRegFunc;
      aHandle  : THandle;
      ocxPath  : string;
    begin
     try
      ocxPath := ExtractFilePath(Application.ExeName) + 'Flash.ocx';
      aHandle := LoadLibrary(PChar(ocxPath));
      if aHandle <> 0 then
      begin
        ARegFunc := GetProcAddress(aHandle,'DllRegisterServer');
        if Assigned(ARegFunc) then
        begin
          ExecAndWait('regsvr32','/s ' + ocxPath);
        end;
        FreeLibrary(aHandle);
      end;
     except
      ShowMessage(Format('Unable to register %s', [ocxPath]));
     end;
    end;

     Note: the ocxPath variable points to the 'Flash.ocx' Macromedia ActiveX control.

    To be able to register itself, an ActiveX control needs to implement the DllRegisterServer function. In simple words, this function creates registry entries for all the classes inside the control. We do not need to worry about the DllRegisterServer function we just want to make sure it is there. For the sake of simplicity, we've presumed that the ActiveX control (the *.ocx file) is located in the same folder as where your application is.

    The red line in the above code, does the job of calling the regsvr32 tool by passing the "/s" switch along with the full path to the ActiveX control. The function is ExecAndWait.

    uses shellapi;
    ...
    function ExecAndWait(const ExecuteFile, ParamString : string): boolean;
    var
      SEInfo: TShellExecuteInfo;
      ExitCode: DWORD;
    begin
      FillChar(SEInfo, SizeOf(SEInfo), 0);
      SEInfo.cbSize := SizeOf(TShellExecuteInfo);
      with SEInfo do begin
        fMask := SEE_MASK_NOCLOSEPROCESS;
        Wnd := Application.Handle;
        lpFile := PChar(ExecuteFile);
        lpParameters := PChar(ParamString);
        nShow := SW_HIDE;
      end;
      if ShellExecuteEx(@SEInfo) then
      begin
        repeat
          Application.ProcessMessages;
          GetExitCodeProcess(SEInfo.hProcess, ExitCode);
        until (ExitCode <> STILL_ACTIVE) or Application.Terminated;
        Result:=True;
      end
      else Result:=False;
    end;

    The above, ExecAndWait, function uses ShellExecuteEx API call to execute a file on a system. If you need more examples of executing any file from Delphi, check the Start from Delphiarticle.

  • 相关阅读:
    android AudioManager AUDIOFOCUS
    uboot环境变量实现分析
    观察者模式总结
    【BZOJ3270】博物馆 概率DP 高斯消元
    从零開始学android&lt;TabHost标签组件.二十九.&gt;
    怎样在Web项目中的service业务层获取项目根路劲
    TexturePacker 算法
    [leetCode 75] Sort Colors
    无人车可能导致器官捐献者短缺以及吸烟率下降:4星|《无人驾驶,十万亿美元的大饼怎么分?》
    如何寻找颠覆式创新的机会,《创新者的窘境》作者二十年磨一剑:4星|《与运气竞争》
  • 原文地址:https://www.cnblogs.com/shangdawei/p/4058465.html
Copyright © 2011-2022 走看看