Recent

Author Topic: Error using dll in Debug call  (Read 193 times)

myisjwj

  • Jr. Member
  • **
  • Posts: 83
Error using dll in Debug call
« on: October 10, 2024, 04:20:55 pm »
dll code:
Code: Pascal  [Select][+][-]
  1. library DllClass;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. uses
  6.   Classes
  7.   { you can add units after this };
  8. type
  9. TMyCls = class(TObject)
  10.   private
  11.     temp:Integer;
  12.   public
  13.     constructor Create(inInt:Integer);
  14.     function Foo(inInt: Integer):Integer;virtual;
  15.   end;
  16.  
  17. function TMyCls.Foo(inInt: Integer):Integer;
  18. begin
  19.   Result:=inInt*inInt*temp;
  20. end;
  21. constructor TMyCls.Create(inInt:Integer);
  22. begin
  23.   temp:=inInt;
  24. end;
  25.  
  26. function CreateMyClass(inInt:Integer): TMyCls; stdcall;
  27. begin
  28.   Result := TMyCls.Create(inInt);
  29. end;
  30. exports
  31.   CreateMyClass;
  32. begin
  33. end.

EXE code
Code: Pascal  [Select][+][-]
  1.   TMyCls = class(TObject)
  2.   private
  3.   public
  4.     function Foo(inInt: Integer):Integer;virtual;abstract;
  5.   end;
  6.   function CreateMyClass(inInt:Integer):TMyCls;stdcall; external 'DllClass.dll';
  7.  
  8. var
  9.   outint:Integer;
  10.   MyObj: TMyCls;
  11. begin
  12.   MyObj:=CreateMyClass(2);
  13.   outint:= MyObj.Foo(5);
  14.   ShowMessage(IntToStr(outint));
  15.   MyObj.Free;
  16. end;    


outint:= MyObj.Foo(5); Error "Invalid type cast" is displayed in Debug mode. It can run in other modes

PascalDragon

  • Hero Member
  • *****
  • Posts: 5755
  • Compiler Developer
Re: Error using dll in Debug call
« Reply #1 on: October 10, 2024, 09:13:58 pm »
You should not use classes across the module boundary, this is not supported and can and will only lead to problems. And also you should not free memory that had been allocated in one module in another module unless you also use a shared memory manager.

 

TinyPortal © 2005-2018