"as" operator across DLLs

It looks like the "is" and "as" operators do not work across DLLs.

I have an object created in one DLL. For example TStrings.

I pass this object via exported function or a COM interface method to another DLL.

function GetTheObject() : TObject;   external;           (or TStrings actually - it doesn't matter)

myobject = GetTheObject()

When I use the "is" operator on the object like this

  b := myobject is TStrings;

I receive FALSE because the "is" operator is implemented by a TObject.InheritsFrom which goes through virtual tables and each DLL has its own set of vtables that are different.

The situation is even worse with the "as" operator. When I use this I get a "Invalid typecast" exception.

  ts := myobject as TStrings;

Is there any solution to this? How to make the operators work across DLLs?


cabelaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DrDelphiCommented:
IS and AS will work across DLL's with this provision:

 Both the DLL and the Calling routine have to know what the object being called is. For example, if you want to export a Tpicture from your DLL to a VB application, you would need to use IpictureDisp, since VB doesn't know what the hell a Tpicture is. By the same token if you wanted to pass a VBpicture to your DLL , you would need to use something that Delphi understands.

 Now, COM on the other hand has a very small and select group of things that it will pass. Primarialy you would never pass an object, but rather a POINTER to the object. It is possible to pass objects as Variants (or so I have read), but I have never tried it.





0
cabelaAuthor Commented:
Well, the problem is not in the getting the pointer but in using it. I do know pretty well what the object is, but I get the Invalid typecast exception when using the as operator. The problem is in the vtables. If I would use the standard cast, it would work ok. But I have no way of checking the actual object type in the other DLL.
0
simonetCommented:
It looks like type libraries and ActiveX (read COM) is the way to go.

Alex
0
Cloud Class® Course: C++ 11 Fundamentals

This course will introduce you to C++ 11 and teach you about syntax fundamentals.

MadshiCommented:
I think it should be possible to check the class name e.g. like this:

if myobject.ClassName = 'TStrings' then
  with TStrings(myobject) do
    ...

But I didn't test that...   :-)

Regards, Madshi.
0
aldynCommented:
The only way to forse AS and IS operators work accross DLL is to place objects that should be used with AS and IS into run-time packages. Only in this case your DLLs will share VTables of these objects. Most native Delphi objects already included into run-time packages. Note that you can use only those run-time packages you really need and remove all other.
 
If you do not like packages you should use approach suggested by Madshi. It works.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cabelaAuthor Commented:
Well, there are some problems with both the approaches. The first solves the "as" operator and helps in no way with the "is".

And I am afraid, that I can come across to this problem in some internal part of VCL code when I create a native VCL object in one DLL and call some standard method from another DLL with this object as a parametr. Or do standard packages solve this?

And I cannot use packages because I use COM objects in the DLLs, or can the package work as a ActiveX server?
0
MadshiCommented:
Why does my suggestion not help with "is"?

  if myobject is TStrings then

  if myobject.ClassName = 'TStrings' then

!!!

Well, but I see problems, if the DLL is written with a different Delphi version than the application is.
0
cabelaAuthor Commented:
Well the versions will be the same. But the "is" operator is TRUE when the object is of the exact class type or of a type DERIVED from the class, which is not the case when comparing names.
0
MadshiCommented:
That's right...   :-(
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.