Invalid class typecast message on opening MSQuery in DLL

Discussion of open issues, suggestions and bugs regarding SDAC (SQL Server Data Access Components) for Delphi, C++Builder, Lazarus (and FPC)
Post Reply
Rnest
Posts: 10
Joined: Fri 19 Oct 2007 14:45

Invalid class typecast message on opening MSQuery in DLL

Post by Rnest » Fri 19 Oct 2007 16:08

I have a problem with opening more than one TMSQuery in DLL form (SDac ver 4.30.00.12)
Proper MSConnection is assigned to DLL's variable of TMSConnection type.
Next, this variable is assigned to MSQuery1 and MSQuery2 as "connection" property.
MSQuery1.Open - opens query, but if I try to open next Query, error message "Invalid Class Typecast" is displayed.

There is simple way to reproduce this problem. Just use demo app from SDAC Demos\Win32\Miscellaneous\Dll, place second TMSQuery component on DLL form, assign "ExternalConnection" and open query from code.

Antaeus
Posts: 2098
Joined: Tue 14 Feb 2006 10:14

Post by Antaeus » Mon 22 Oct 2007 07:21

Thank you for information. We have reproduced this problem and fixed it. This fix will be included in the next SDAC build.

peetjet
Posts: 8
Joined: Fri 04 Mar 2005 10:45
Location: Netherlands
Contact:

Post by peetjet » Mon 21 Dec 2009 15:41

I'm experiencing the exact same problem in SDAC 4.80 for RAD studio 2007 ...

I hoped upgrading to 4.80 would resolve this problem, but it didn't.

Please help.

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Tue 22 Dec 2009 12:15

This problem may arise if you create the TMSConnection class object in a dll and use it in an application. Methods of TMSConnection use the "as" operator which returns invalid type cast error.
To solve the problem you can use the TCustomMSConnection.AssignConnect method to assign the TMSConnection object created in dll to the TMSConnection object of application.
Also you can use the approach used in the SDAC DLL demo. You can find this demo by the following path: SDAC_InstDir\Demos\Miscellaneous\Dll.
SDAC_InstDir is the SDAC installation directory on your computer.

peetjet
Posts: 8
Joined: Fri 04 Mar 2005 10:45
Location: Netherlands
Contact:

"Invalid class typecast" revisited

Post by peetjet » Wed 24 Mar 2010 16:28

The DLL example provided works fine with the TMSQuery created on the form, but I create a lot of queries just in code, as follows:

[code]mq := TMsQuery.Create(nil);
mq.Connection := ExternalMSConnection;
mq.SQL.Text := 'some select statement';
mq.Open;[/code]

When I add a button to the form fmDllMain in DLLMain.pas and put this in the OnClick:

[code]procedure TfmDllMain.Button1Click(Sender: TObject);
var mq: TMsQuery;
begin
mq := TMsQuery.Create(nil);
mq.Connection := ExternalMSConnection;
mq.SQL.Text := 'select ''Hai''';
mq.Open;
while not mq.Eof do
begin
ShowMessage(mq.Fields[0].AsString);

mq.Next;
end;
mq.Close;
mq.Destroy;

end;[/code]

Then I get an invalid typecast while a similar procedure on fmExeMain in ExeMain.pas just says "Hai", as I intended.

Is this something that can be fixed?

With regards,
Peet Terluin

Dimon
Devart Team
Posts: 2910
Joined: Mon 05 Mar 2007 16:32

Post by Dimon » Thu 25 Mar 2010 14:47

To solve this problem you should create a TMSConnection object in dll or use one TMSQuery object for all queries.

peetjet
Posts: 8
Joined: Fri 04 Mar 2005 10:45
Location: Netherlands
Contact:

What I did to solve/workaround this

Post by peetjet » Fri 26 Mar 2010 10:34

First of all, thanks for your quick reply.

My DLL's used to have their own TMSConnection but I started running in to "lock timeout exceeded" exceptions. (multiple users and some "heavy" batch processing going on) These errors did not occur while everything was still in one executable so I thought it might be helpful to use a single MSconnection for executable and DLL's. And I started to experiment a little. So your suggestion to create a TMSConnection object in the DLL does solve the error, but not my problem ...
But it did inspire me to the following extension of the DLL-example.
Not only is the Connection made available for use in the DLL but also functions for the creation of TMsQuery and TMsSQL.
And the transaction methods, StartTransaction, Commit and Rollback are also made available for use in the DLL.
The TAssignMsConnection declaration became something like this:

TDBConnMethod = procedure; cdecl;
TCreateMsQuery = function: TMsQuery; cdecl;
TCreateMsSQL = function: TMsSQL; cdecl;
TAssignMsConnection = procedure(Conn: TMSConnection;
cqp: TCreateMsQuery ;
cqs: TCreateMsSQL ;
DBStartTransaction, DBCommit, DBRollback: TDBConnMethod); cdecl;

The first test results are hopeful ...

With regards, Peet Terluin

Ludek
Posts: 301
Joined: Thu 12 Oct 2006 09:34

Post by Ludek » Tue 30 Mar 2010 07:22

Or just simply build your projects with runtime packages active (custom or those prebuilt from borland), IMHO the best solution, especially with the custom runtime package, saves much place on disc and many other problems.

Post Reply