I have have a DLL that creates a connection object to a database. This is part of the code of the function:
AADConnection = ^TADConnection;
......
function OpenPGConnection: Pointer; register;
var
RC: Integer;
ERRM: String;
ADConnection: TADConnection;
P: AADConnection;
begin
New(P);
P^:= TADConnection.Create(nil);
P^.Name:= 'ADConnection123';
P^.FetchOptions.Unidirectional:= True;
.....
Result:= Pointer(P);
end;
This is how I use this function:
function OpenPGConnection: Pointer; register; external 'mypqconnection.dll';
......
var
P: AADConnection;
.....
begin
P:= OpenPGConnection;
ADConnection:= P^;
ShowMessage(ADConnection.Name);
if ADConnection.connected then ShowMessage('True') else
ShowMessage('False');
.....
Q.Connection:= ADConnection;
Q.Close;
Q.SQL.Clear;
Q.SQL.Add('select * from mytable');
Q.Open;
ShowMessage(ADConnection.Name) gives the right name "ADConnection123" and check for connectivity returns "True". But when I run Q.Open then I get "Invalid Class Typecast". I know returning pointer to object from a DLL is very tricky business but at first glance it seems this code returns the correct values. Still something is not right. What can it be?
EDIT: I have tried the suggested solutions from these two topics:
Accessing classes that are in another DLL?
Fist I rewrote the DLL and the main program using Interface. Then I tried runtime BPL package. I always got 'Invalid Class Typecast'.