?
Solved

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

Posted on 2006-07-20
7
Medium Priority
?
1,585 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 800 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 200 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 1000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Suggested Courses
Course of the Month9 days, 11 hours left to enroll

762 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