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,490 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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Newbie Thread Programming 1 118
Delphi cmd execution 6 42
Strange behavior when a form is closed 6 44
Run video youtube webbrowse 10 45
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 my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

747 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now