Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Objects in DLL?

Status
Not open for further replies.

Glenn9999

Programmer
Jun 19, 2004
2,312
US
I'm trying to find a good example of how this is done - can anyone provide a pointer to one?

A simple example, say TStringList is in a DLL. How would I:

1) Put it into a DLL.
2) access it?

I'm guessing this would be related to COM, but I'm not finding a good Delphi example anyplace.
 
If you want to add VCL things, you can embed a data unit in a DLL.
Drop whatever you want onto its form.

But a Tstringlist can simply be declared in the main unit.

This is my own 'learn it yourself' thing. seems to work!!
Access it as you would any stringlist in a normal procedure.
Unless its being called from another language, when it gets a tab more involved.


Code:
library Ethernet3;

uses
  SysUtils,
  Classes,
  DataUnit3 in 'DataUnit3.pas' {DataModule2: TDataModule},
  IeeeCalc in '..\..\..\Delphi Projects\ieee\IeeeCalc.pas';

Const CARDREAD  = $42;
      CARDWRITE = $43;
      RESCAN    = $44;

var TimeConstant: integer;   // normally 10000000
    Initalised: boolean = false;

{$R *.res}

{----------------------------------------------------------------------------
Library Inteface
----------------------------------------------------------------------------}

function EthernetInitialise(T: integer; Timeout: integer): integer;  stdcall;
begin
   if not Initalised then
       try
           DataModule2 := TDataModule2.Create(nil); // creates the data module
           MemRecv := TStringList.Create;           // create a string list to hold recived messages
       except
           // if we cannot set this up scrub it!!
           result := 19;
           exit;
       end;

etc.............

To use a data unit add it to the DLL uses clause as you would with any other secondary unit, and access with Dataunitname.whatever.




Steve [The sane]: Delphi a feersum engin indeed.
 
I'm not sure the answer got answered too clearly, maybe because either I'm not asking the right questions or I just don't know enough to understand...

But a Tstringlist can simply be declared in the main unit

I just used that as a simple example I know most can relate to, and I know it can simply be declared in Delphi. But let's say this is something that is not available in unit form and only in DLL. Then what? If an object like TStringList is defined in a DLL, then what would I need to do to be able to let Delphi have access to it through the DLL?

I'm generally used with DLLs to finding defined functions that are exported. Presumably with an object, there will be some type definitions, and some methods defined. But beyond that, how does Delphi know of the proper methods and properties?

Looking at what you posted, I presume DataModule2 is your exported object, and you're using EthernetInitialise to expose the object on a procedure. I can understand that part, but in the main program, what? Let's say I define the EthernetInitialise procedure to this DLL. Then if I were to call the method "GetFrameBuffer" (as an example) in DataModule2, how would that be done, or would Delphi pick up all the type data up from the DLL being loaded?
 

The DLL functions are declared in the calling application
this is where your types would have to be correct.

I think what you are saying is that if a data type is declared in a DLL how does the calling program access it.

My DLL doesnt contain any custom/user defined data types, to be honest unless someone else says otherwise I don't think you can pass custom types across a DLL.

In fact my DLL returns strings from the string list it doesnt return the whole thing.
Though that should be possible as you say a Tstringlist is predefined Delphi object.


Steve [The sane]: Delphi a feersum engin indeed.
 
in fact you can use any object across DLL's.
the only thing that delphi needs is RTTI (runtime type information) RTTI is made when a class is registered.

so if you have custom classes, all you need to do is add

RegisterClass(TMyCustomClass); //

in the initialization part of the classes' unit.

then use this unit in the DLL and main app.

-----------------------------------------------------
What You See Is What You Get
Never underestimate tha powah of tha google!
 
The DLL functions are declared in the calling application
this is where your types would have to be correct.

Okay, I actually got to looking around in the documentation for the DLL and found a link to an C .H file. 525K too. So probably won't be able to do much with it until it is translated to Delphi - my web searches have indicated that no one has done that yet.

If I ever get it done (isn't that how life works?), I'm sure I'll have more questions.
 
This is the problem Glen if both bits have to be Written in Delphi (and then be correctly documented). In the real world this seldom happens!

Steve [The sane]: Delphi a feersum engin indeed.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top