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 <> 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), );
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.

Registering DLL and ActiveX controls from code的更多相关文章

  1. MultiTouch camera controls source code

    http://www.jpct.net/wiki/index.php/MultiTouch_camera_controls MultiTouch camera controls This code w ...

  2. python——报错ImportError:DLL load failed with error code -1073741795的解决方式

    python中导入一个包,import cv2总是报错'ImportError:DLL load failed with error code -1073741795',报错形式: 网上找了好久的解决 ...

  3. ImportError: DLL load failed with error code -1073741795

    Win7,python3.6,pip安装tensorflow之后报错: >>> import tensorflow Traceback (most recent call last) ...

  4. 转载:Win7系统 利用 pycharm导入Tensorflow失败,出现报错——ImportError:DLL load failed with error code -1073741795的解决方式

    转载自:https://blog.csdn.net/shen123me/article/details/80621103 下面的报错信息困扰了一天,网上的各种方法也都试过了,还是失败,最后自己瞎试,把 ...

  5. VS2010 开发 VB6.0 activeX控件 dll

    项目源码 https://download.csdn.net/download/csdn_z_s/10427764 开发环境 操作系统: win7 64位 旗舰版 Java语言开发环境:  Eclip ...

  6. Activex、OLE、COM、OCX、DLL之间区别、联系[转]

    转自:http://baike.baidu.com/view/393671.htm 创建COM:http://blog.csdn.net/henry000/article/details/700839 ...

  7. A Complete ActiveX Web Control Tutorial

    A Complete ActiveX Web Control Tutorial From: https://www.codeproject.com/Articles/14533/A-Complete- ...

  8. Code Project精彩系列(转)

    Code Project精彩系列(转)   Code Project精彩系列(转)   Applications Crafting a C# forms Editor From scratch htt ...

  9. 计算机视觉code与软件

    Research Code A rational methodology for lossy compression - REWIC is a software-based implementatio ...

随机推荐

  1. Tomcat: Connector中HTTP与AJP差别与整合

    apache tomcat 整合(ajp proxy, http proxy) 1.软件: apache: httpd-2.2.17-win32-x86-openssl-0.9.8o.msi tomc ...

  2. C/C++——库函数strcpy和strdup比较

    版权声明:原创文章,禁止转载. 1. strcpy 原型: extern char *strcpy(char *dest,char *src); 用法: #include <string.h&g ...

  3. 杂乱的code

    /*o(n)的堆化方法*/ void myjust(vector<int>& A,int i){ int l=i*2+1; int r=i*2+2; int minn=i; if( ...

  4. Kail Linux渗透测试之测试工具Armitage

    Kali Linux下的Armitage是一个很强大的渗透工具,图形化操作页面,但我们把kali linux装在虚拟机里面,然后再启动armitage就会出现一个error,他会给你一个message ...

  5. HDU 2147 kiki's game(博弈图上找规律)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2147 题目大意:给你一个n*m的棋盘,初始位置为(1,m),两人轮流操作,每次只能向下,左,左下这三个 ...

  6. Codeforces Round #423 Div. 2 C-String Reconstruction(思维)

    题目大意:告诉你n个字符串以及这些字符串在字符串s中出现的位置(x1,x2.....xn),要求在满足上述条件的情况下,求出字典序最小的字符串s. 解题思路:主要问题是,如果直接模拟是会超时的,比如v ...

  7. 洛谷P2692 覆盖 题解

    题目传送门 这道题一开始想使用二维的bool型数组来存,最后统计.但看到数据范围... 所以就改用两个bool型数组(一维),分别储存横.列,最后将横.列面积求出来,再减去重复算的面积(横的个数*列的 ...

  8. GO语言Windows下Liteide

    今天用到了. 就学习一下. https://www.golangtc.com/t/56e7caf5b09ecc66b90000fe 在网上看了好多此类介绍,操作太麻烦,自己琢磨出来怎么配置了. 以Li ...

  9. Loadrunner中cookie解释与用法

    loadrunner对于cookie的处理loadrunner中与cookie处理相关的常用函数如下: web_add_cookie():添加新的cookie或者修改已经存在的cookie web_r ...

  10. spring_150905_sqlmapclient

    添加ibatis相关的jar包! 实体类: package com.spring.model; public class DogPet { private int id; private String ...