"Invalid class typecast" error with DLL's TMSQuery.Post

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" error with DLL's TMSQuery.Post

Post by Rnest » Sat 04 Feb 2012 13:38

"Invalid class typecast" error calling TMSQuery.Post method of dynamically created TMSQuery in a dll.

Steps to reproduce:
1) Use included demo app {InstallDemosDir}\Demos\Miscellaneous\Dll
2) Modify procedure ShowForm in DLLMain unit:

procedure ShowForm; cdecl;
var
msq: TMSQuery;
begin
with TfmDllMain.Create(Application) do
begin
msq := TMSQuery.Create(nil);
msq.Connection := ExternalMSConnection;
//put proper SQL statements
msq.SQL.Text := 'SELECT * FROM some_table';
msq.SQLInsert.Text := 'INSERT INTO some_table...';
msq.SQLUpdate.Text := 'UPDATE some_table...';
msq.Open;
msq.Append;
msq.FieldByName('some_field').AsInteger := 0;
msq.Post; <<- invalid typecast error occurs
msq.Free;


Inc(FormCount);
Caption := IntToStr(FormCount) + '. ' + Caption;
MSQuery.Connection := ExternalMSConnection;
MSQuery.Active := True;
Show;
end;
end;

AndreyZ

Post by AndreyZ » Mon 06 Feb 2012 15:32

Hello,

Thank you for the information. We have reproduced and fixed this problem. This fix will be included in the next SDAC build.
To solve the problem, you should replace the code:

Code: Select all

procedure AssignConnection(MSConnection: TMSConnection); cdecl;
begin
  ExternalMSConnection := MSConnection;
end;
with the following code:

Code: Select all

procedure AssignConnection(MSConnection: TMSConnection); cdecl;
begin
  if ExternalMSConnection = nil then
    ExternalMSConnection := TMSConnection.Create(nil);
  ExternalMSConnection.AssignConnect(MSConnection);
end;
Also, you should add the following code to the finalization section:

Code: Select all

ExternalMSConnection.Free;

Rnest
Posts: 10
Joined: Fri 19 Oct 2007 14:45

Post by Rnest » Tue 07 Feb 2012 11:09

Thank you Andrey, this solution works perfect!

AndreyZ

Post by AndreyZ » Tue 07 Feb 2012 13:43

I'm glad I could help. If any other questions come up, please contact us.

Post Reply