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,554 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Independent Software Vendors: 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

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!

Question has a verified solution.

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

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
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…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

705 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