Page 1 of 1

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

Posted: Sat 04 Feb 2012 13:38
by Rnest
"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;

Posted: Mon 06 Feb 2012 15:32
by AndreyZ
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;

Posted: Tue 07 Feb 2012 11:09
by Rnest
Thank you Andrey, this solution works perfect!

Posted: Tue 07 Feb 2012 13:43
by AndreyZ
I'm glad I could help. If any other questions come up, please contact us.