Board index » cppbuilder » Automation object fails on VB in design mode (not in compiled)

Automation object fails on VB in design mode (not in compiled)

I found the following message on groups.google.com, and it describes the
exact problem that I'm seeing today (although w/ 5.0 Update Pack 1).
Anybody know if

(a) this is resolved in 6.0 or
(b) there is a workaround for 5.0?

Thanks in advance!
-- Early Ehlinger
From: baxthe...@yahoo.com (Bax)
Newsgroups: borland.public.cppbuilder.activex
Subject: Automation object fails on VB in design mode (not in compiled)
Date: 16 Jun 2001 02:30:17 -0700
Organization: http://groups.google.com/
Lines: 13
Message-ID: <106600b5.0106160130.682ceab0@posting.google.com>
NNTP-Posting-Host: 212.216.40.177
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
X-Trace: posting.google.com 992683818 21920 127.0.0.1 (16 Jun 2001 09:30:18
GMT)
X-Complaints-To: groups-support@google.com
NNTP-Posting-Date: 16 Jun 2001 09:30:18 GMT

HI,
I am Bax and I've developed under CPPBuilder 5.0 an Automation object.
When I've tried it on VB 5.0 in design mode the CreateObject method
fails the second time i run the program. The program contains a
reference to my builder obect. When I rnu the first time all runs
without problem but if I restart the program the second time,
sistematicaly it's getting to me an EOutOfResource exception from
TApplication::CreateHandle.
I Hate VB when it's behave like that because if I produce the exe
program form VB, it works fine every time.

any suggestion?
thanx BAx

 

Re:Automation object fails on VB in design mode (not in compiled)


Replying before the original post even shows up on the server.  Found out
the root cause and a workaround - I'm so proud... :)

OK - first the root cause:

When you hit "stop" in VB's design mode, the ActiveX dll gets unloaded from
memory, but the VCL does not Unregister the window class "TApplication".
When you then run in design mode again, the ActiveX dll gets loaded and VCL
attempts to RegisterClass with "TApplication" as the class name.  Because it
is already registered, VCL throws EOutOfResources.

Now for the workaround:

Use the code from http://www.bcbdev.com/faqs/faq6.htm as a starting point.
It overrides the VCL version of TApplication.CreateHandle with yours.
You'll need to make the following changes.  After the attempt to
RegisterClass succeeds, add a call to atexit, like so:

if (!GetClassInfo(HInstance, WindowClass.lpszClassName, &TempClass))
  {
  WindowClass.hInstance = HInstance;
  if (RegisterClass(&WindowClass) == 0)
    throw EOutOfResources("Error registering window class");
  g_hInstance = HInstance;
  atexit( &Unregister );
  }

The next step is to write a function, Unregister, that will get called when
VB unloads the DLL:

HINSTANCE g_hInstance = NULL;
void _USERENTRY Unregister( )
  {
  delete Application;
  Application = NULL; /* <- might be unnecessary.  it's here j/i/c VCL tries
to delete it later */
  UnregisterClass( g_hInstance , "TApplication" );
  }

Actually, in my implementation, the class name "TApplication" has been
changed to include a GUID, thus avoiding any conflicts with other
BCB-implemented ActiveX Libraries that might need TApplication.

A couple of things...

* I tried to write Unregister at first without the delete Application.  The
call to UnregisterClass failed because Application still had a window of
class "TApplication.<guid>" in existence.

* An alternative approach would be to not use atexit and instead use a
different class name on each run, e.g., to generate a new <guid> to go into
"TApplication.<guid>".  I chose not to do this because if I had implemented
it that way, the ActiveX library would be leaking Window class atoms
whenever you hit stop/play in VB.

--
-------- Early Ehlinger - CEO - ResPower Inc --------
- RenderFarm - Lightwave,3dSMax,Bryce @$0.50/CPU*Hr -
- www.respower.com Coming Soon - Maya @$0.75/CPU*Hr -
----------- SuperComputing For the Masses! ----------

Quote
"Early Ehlinger" <ear...@yahoo.com> wrote in message news:...
> I found the following message on groups.google.com, and it describes the
> exact problem that I'm seeing today (although w/ 5.0 Update Pack 1).
> Anybody know if

> (a) this is resolved in 6.0 or
> (b) there is a workaround for 5.0?

> Thanks in advance!
> -- Early Ehlinger
> From: baxthe...@yahoo.com (Bax)
> Newsgroups: borland.public.cppbuilder.activex
> Subject: Automation object fails on VB in design mode (not in compiled)
> Date: 16 Jun 2001 02:30:17 -0700
> Organization: http://groups.google.com/
> Lines: 13
> Message-ID: <106600b5.0106160130.682ce...@posting.google.com>
> NNTP-Posting-Host: 212.216.40.177
> Content-Type: text/plain; charset=ISO-8859-1
> Content-Transfer-Encoding: 8bit
> X-Trace: posting.google.com 992683818 21920 127.0.0.1 (16 Jun 2001
09:30:18
> GMT)
> X-Complaints-To: groups-supp...@google.com
> NNTP-Posting-Date: 16 Jun 2001 09:30:18 GMT

> HI,
> I am Bax and I've developed under CPPBuilder 5.0 an Automation object.
> When I've tried it on VB 5.0 in design mode the CreateObject method
> fails the second time i run the program. The program contains a
> reference to my builder obect. When I rnu the first time all runs
> without problem but if I restart the program the second time,
> sistematicaly it's getting to me an EOutOfResource exception from
> TApplication::CreateHandle.
> I Hate VB when it's behave like that because if I produce the exe
> program form VB, it works fine every time.

> any suggestion?
> thanx BAx

Other Threads