"as" operator across DLLs

Posted on 1999-11-09
Medium Priority
Last Modified: 2012-05-04
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?

Question by:cabela

Expert Comment

ID: 2194753
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.


Author Comment

ID: 2194766
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.
LVL 15

Expert Comment

ID: 2195833
It looks like type libraries and ActiveX (read COM) is the way to go.

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

LVL 20

Expert Comment

ID: 2196150
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.

Accepted Solution

aldyn earned 100 total points
ID: 2196165
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.

Author Comment

ID: 2196429
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?
LVL 20

Expert Comment

ID: 2196466
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.

Author Comment

ID: 2196481
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.
LVL 20

Expert Comment

ID: 2196569
That's right...   :-(

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…
This video tutorial shows you the steps to go through to set up what I believe to be the best email app on the android platform to read Exchange mail.  Get the app on your phone: The first step is to make sure you have the Samsung Email app on your …
Suggested Courses

599 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