如何在Delphi中将LPARAM转换为结构?

时间:2019-04-07 13:06:46

标签: delphi

我需要帮助将LPARAM类型转换为结构。例如,在C ++中,我们有:

struct MyStructure
{
   int data01;
   int data02;
};

BOOL CALLBACK CallbackProc(int number, LPARAM param)
{
   MyStructure *myStruct = (MyStructure *) param;
}

在Delphi中,我尝试了此操作,但是编译器在突出显示的行上用注释给出了不兼容类型的错误:

type
  TMyStructure = record
    data01: Integer;
    data02: Integer;
  end;

PMyStructure = ^TMyStructure;

function CallbackProc(number: Integer; param: LPARAM): BOOL; stdcall;
var
  myStruct: TMyStructure;
begin
  myStruct := param; // How stays here?
end;

1 个答案:

答案 0 :(得分:5)

C ++代码将LPARAM强制转换为 pointer 的结构。在Delphi中,看起来像这样:

type
  TMyStructure = record
    data01: Integer;
    data02: Integer;
  end;
  PMyStructure = ^TMyStructure;

function CallbackProc(number: Integer; param: LPARAM): BOOL; stdcall;
var
  myStruct: PMyStructure;
begin
  myStruct := PMyStructure(param);
end;