Solved

How to detect if a OCX is registered before app generates an error because it is not

Posted on 2006-07-20
7
1,529 Views
Last Modified: 2008-01-09
I have a Delphi app and I have integrated a 3rd party PDF reader OCX, however the problem is I can't check to see if the OCX is registered (and fix the problem by registering it) before the app crashes with a Kernal32 error (because the OCX is embedded in the main form).  I know I could fix this with an installer (which registers the OCX) but it would be alot more elegant if the program could check for the proper registration and then register it before finishing initialization.  Is there any way to do this?

First thing in my main form's OnCreate I am calling this code (but unfortunately it never gets called):

procedure RegisterOCX;
var
  OCXHand: THandle;
begin
  OCXHand:=LoadLibrary('L:\APPS\Family Services\FSWorkflow\VSPDFViewerX.ocx');

  // If OCX didn't load (OCXHand = 0) then attempt to register it
  if OCXHand = 0 then
  begin
    if (GetProcAddress(OCXHand,'DllRegisterServer')<> nil) then
    begin
      StandardEurekaError('Error Registering OCX VSPDFViewerX');
    end;
  end;
  FreeLibrary(OCXHand);
end;
0
Comment
Question by:lgallion
7 Comments
 
LVL 17

Assisted Solution

by:mokule
mokule earned 200 total points
ID: 17150149
try to call registerOCX in dpr

begin
  Application.Initialize;

  RegisterOCX;               // try to call here

  Application.CreateForm(TForm1, Form1);
  Application.Run;
end.
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17150151
You need to perform that logic in the project source BEFORE the Application creates an instance of the main form, otherwise its too late and the error will occur.

Regards,
Russell
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 17150157
Too slow on this one ;-)

Russell
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:Ivanov_G
ID: 17151721
Run regsvr32 YourOCX.ocx to register it (it the file exists)
0
 
LVL 10

Assisted Solution

by:atul_parmar
atul_parmar earned 50 total points
ID: 17152322
Hi use ProgIDFromCLSID win32 API function and check it's return value. It requires a class guid and returns S_OK if the class is registered otherwise it returns REGDB_E_CLASSNOTREG means the class is not registered.
0
 
LVL 26

Accepted Solution

by:
Russell Libby earned 250 total points
ID: 17153863

By the way, the code you have is incorrect. The only way to call DllRegisterServer (regardless if you do it manually, or use a wrapper process like regsvr32) is to get back a valid handle from LoadLibrary.

procedure RegisterOCX;
var
  OCXHand: THandle;
begin
  OCXHand:=LoadLibrary('L:\APPS\Family Services\FSWorkflow\VSPDFViewerX.ocx');

  // If OCXHand  = 0, then you have bigger problems like: missing dll, missing dependancy dlls, etc.
  // Thers is no way to GetProcAddress at this point.

  if OCXHand = 0 then
  begin
    if (GetProcAddress(OCXHand,'DllRegisterServer')<> nil) then
    begin

 
The other comment regarding ProgIDFromCLSID is a good test indicator that an Ax/Ocx control is registered, but does not guarantee that you will actually be able to create an instance of the desired interface. The only absolute way of testing is to get an S_OK result from CoCreateInstance (this is called by CreateOleObject, CreateComObject, etc). Because of this, it doesn't hurt to make a call to CoCreateInstance first to see if an instance can be created, and if not (and you know the library path and file) you can attempt registration.

You might also check out this recent PAQ where I provide a link to an object class (something I wrote awhile back) that simplifies the loading/calling of the dll/ocx register/unregister functions.

http://www.experts-exchange.com/Programming/Programming_Languages/Delphi/Q_21919442.html
 
Using it, the application's dpr might end up looking something like this:

uses
  Forms,
  ComReg,
  Unit1 in 'Unit1.pas' {Form1};

{$R *.RES}
begin

  // Initialize the application
  Application.Initialize;

  // Ensure the component is registered
  with TComRegistration.Create do
  begin
     try
       // Load library and ensure loaded
        if (Load('L:\APPS\Family Services\FSWorkflow\VSPDFViewerX.ocx') = ERROR_SUCCESS) and IsLoaded and IsComLibrary then
        begin
           // Attempt registration
           if not(Perform(raRegister) = S_OK) then
              // Error handling
              StandardEurekaError('Error Registering OCX VSPDFViewerX');
        end
        else
           // Error handling
           StandardEurekaError(Format('Error Loading OCX VSPDFViewerX (%d)', [GetLastError]));
     finally
        // Free COM registration
        Free;
     end;
  end;

  // Create form
  Application.CreateForm(TForm1, Form1);

  // Run
  Application.Run;

end.
0
 

Author Comment

by:lgallion
ID: 17154043
Thanks guys excellent feedback!  Kudos to mokule for a quick reply, nods to atul_parmar for pointing me in a better direction and a big thanks to rllibby for a super indepth answer.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
URL for downloading Google Chrome for Win XP 2 234
How to renew a Delphi rad-studio licence? 5 77
Multiple image collision 13 88
Delphi...Split view - idea? 1 116
This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question