Some experiences in using ActiveX in Delphi

Source: Internet
Author: User
There are two methods:
1. directly put the visualized ActiveX control into the program;
2. Real-Time Creation as needed during runtime.
If it is directly used, the application will automatically find and create the required ActiveX Control during initialization. If the control is not registered, the initialization program will generate an exception, capture and handle this exception.
Add a new method to the Form program:
Unit UAutoRegActiveX;
Interface
Uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComObj; // Add ComObj Unit
Type
TAutoRegActiveXFrm = class (TForm)
Procedure FormCreate (Sender: TObject );
Private
{Private declarations}
Public
{Public declarations}
Protected
{Public declarations}
Procedure checkexception (Sender: tobject; eabort: exception );
End;
VaR
Autoregactivexfrm: tautoregactivexfrm;
Implementation
{$ R *. DFM}
{-------------------------------------------------
Standard ActiveX controls have two functions: dllregisterserver and dllunregisterserver,
Functions are used to register controls and uninstall controls. We can use loadlibrary to load dll/OCX files.
Getprocaddress obtains the pointers of the dllregisterserver and dllunregisterserver functions.
Then, you can directly run these two functions to register and uninstall ActiveX controls, replacing Windows
Regsvr32.exe of the system registers and uninstalls ActiveX controls.
--------------------------------------------------}
{-------------------------------------------------
Parameter description:
Solefilename: a DLL or ocx file name;
Oleaction indicates the registration operation type: 1 indicates registration, 0 indicates uninstall
Return Value: True indicates that the operation is successful. False indicates that the operation failed.
--------------------------------------------------}
Function oleregister (solefilename: string; oleaction: byte): Boolean;
Const
Registerole = 1; // register
Unregisterole = 0; // uninstall
Type
Toleregisterfunction = function: hresult; // register or uninstall the function prototype.
VaR
Hlibraryhandle: thandle; // DLL or OCX handle returned by loadlibray
Hfunctionaddress: tfarproc; // function handle in DLL or ocx, Which is returned by getproaddress
Regfunction: toleregisterfunction; // register or uninstall the function pointer
Begin
Result: = false;
// Open the file and return the DLL or OCX handle
HLibraryhandle: = LoadLibrary (PChar (SOleFileName ));
If (hLibraryHandle> 0) then // The DLLakg OCX handle is correct
Try
// Return the registration or uninstallation function pointer
If (OleAction = RegisterOle) then // return the registration function pointer
HFunctionAddress: = GetProcAddress (hLibraryhandle, PChar ('dlregisterserver '))
Else // returns the unmount function pointer
HFunctionAddress: = GetProcAddress (hLibraryhandle, PChar ('dllunregisterserver '));
If (hFunctionAddress <> nil) then // determines whether the registration or uninstallation function exists
Begin
RegFunction: = TOleRegisterFunction (hFunctionAddress); // gets the pointer of the Operation Function
If RegFunction> = 0 then // execute the registration or uninstall operation. The return value> = 0 indicates that the execution is successful.
Result: = True;
End;
Finally
FreeLibrary (hLibraryHandle); // close an opened file
End;
End;
{TAutoRegActiveXFrm}
Procedure TAutoRegActiveXFrm. CheckException (Sender: TObject;
EAbort: Exception );
Begin
If EAbort is EOleSysError then
Begin
If HResult (EOleSysError (EAbort). ErrorCode) = regdb_e_classnoreg then
OleRegister ('d:/Flash. ocx ', 1 );
End
Else
Application. ShowException (EAbort );
End;
// Assign the CheckException Method to the system Application variable in the OnCreate event of the main Form.
Procedure TAutoRegActiveXFrm. FormCreate (Sender: TObject );
Var
DemoOcx: Variant; // variable Declaration
Begin
Application. OnException: = CheckException;
// Whether a class name string error is generated
Try
DemoOcx: = CreateOleObject ('demo. demo ');
Except
On EAbort: EOleSysError do
If HResult (EAbort. ErrorCode) = CO_E_CLASSSTRING then
Begin
If OleRegister ('d:/Flash. ocx ', 1) then
DemoOcx: = CreateOleObject ('demo. demo ')
Else
Begin
Application. MessageBox ('control registration failed, the program will not run properly ', PChar ('register control'), MB_ OK + MB_ICONERROR );
Application. Terminate;
End;
End;
End;
End;
Method 2: Sometimes *. OCX will use a lot of DLL, so if we still follow the above method at this time, it is quite troublesome, so I suggest using the following method:
1. First find the DLL file required by *. OCX;
2. Copy all files to the directory where the program is located;
3. Write the following code in the program initialization section:
Winexec(pchar('regsvr32.exe-S' + getcurrentdir () + '/vcf132.ocx'), sw_show );
In fact, we just put the original steps of manual installation in the MS-DOS into the program to do,
Note that you only need to register vcf132.ocx for registration;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.