Pascal:Interfaced Class,Double,to Interface实例

时间:2013-06-11 15:15:34

标签: class pointers interface freepascal

这是我的另一个问题的延伸。我有一个应该与Game Maker接口的DLL,它使用DoublePChar

我必须将我的对象转换为Double s(引用)以与Game Maker来回传递。

我有两个课程:TBitmapTWindow。每个都实现IBlittable。这意味着在IBlittable s上运行的方法可以同时使用这两种方法,但问题出在这里:

以下是我创建它们的方法:

{Creation of Bitmaps and Windows}

function CreateBitmap(W, H: Double): Double; STDCall;
var
  TBM: TBitmap;
begin
  TBM := TBitmap.Create(Floor(W), Floor(H));
  CreateBitmap := Double(Integer(TBM));
end;

function GetWindowByHWND(Handle: Double): Double; STDCall;
var
  ReturnVal: TWindow;
begin
  ReturnVal := TWindow.Create(Floor(Handle));
  GetWindowByHWND := Double(Integer(ReturnVal));
end;

以下是我尝试对其进行操作的方式:

function SetPixel(Handle, X, Y, Color: Double): Double; STDCall;
var
  <stuff>
begin
  Blittable := IBlittable(Floor(Handle)); //This!
  <set the pixel>
  SetPixel := 0;
end;

当我将Floor(Handle)转换为IBlittable时,会发生段错误和崩溃。

当我将它投射到之前的版本(TWindow或TBitmap)时,它可以正常工作。我该如何规避这个问题?

我真的不想特别重写所有方法(那是非常多余和蹩脚的)

1 个答案:

答案 0 :(得分:0)

我需要更改创建函数中的类型:

function CreateBitmap(W, H: Double): Double; STDCall;
var
  TBM: IBlittable; //See
begin
  TBM := TBitmap.Create(Floor(W), Floor(H));
  CreateBitmap := Double(Integer(TBM));
end;

function GetWindowByHWND(Handle: Double): Double; STDCall;
var
  ReturnVal: IBlittable; //Here too
begin
  ReturnVal := TWindow.Create(Floor(Handle));
  GetWindowByHWND := Double(Integer(ReturnVal));
end;