Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why this C to Pascal conversion crash?

I have this C code:

/*
  WARNING:  The order of this table must also match the order of a table
  located in AcquireResizeFilter() in "resize.c" otherwise the users filter
  will not match the actual filter that is setup.
*/
typedef enum
{
  UndefinedFilter,
  PointFilter,
  BoxFilter,
  TriangleFilter,
  HermiteFilter,
  HannFilter,
  HammingFilter,
  BlackmanFilter,
  GaussianFilter,
  QuadraticFilter,
  CubicFilter,
  CatromFilter,
  MitchellFilter,
  JincFilter,
  SincFilter,
  SincFastFilter,
  KaiserFilter,
  WelchFilter,
  ParzenFilter,
  BohmanFilter,
  BartlettFilter,
  LagrangeFilter,
  LanczosFilter,
  LanczosSharpFilter,
  Lanczos2Filter,
  Lanczos2SharpFilter,
  RobidouxFilter,
  RobidouxSharpFilter,
  CosineFilter,
  SplineFilter,
  LanczosRadiusFilter,
  CubicSplineFilter,
  SentinelFilter  /* a count of all the filters, not a real filter */
} FilterType;

and

WandExport MagickBooleanType MagickResizeImage(MagickWand *wand,
  const size_t columns,const size_t rows,const FilterType filter)

I convert it to Pascal like this:

type
  FilterType =(
    UndefinedFilter,
    PointFilter,
    BoxFilter,
    TriangleFilter,
    HermiteFilter,
    HannFilter,
    HammingFilter,
    BlackmanFilter,
    GaussianFilter,
    QuadraticFilter,
    CubicFilter,
    CatromFilter,
    MitchellFilter,
    JincFilter,
    SincFilter,
    SincFastFilter,
    KaiserFilter,
    WelchFilter,
    ParzenFilter,
    BohmanFilter,
    BartlettFilter,
    LagrangeFilter,
    LanczosFilter,
    LanczosSharpFilter,
    Lanczos2Filter,
    Lanczos2SharpFilter,
    RobidouxFilter,
    RobidouxSharpFilter,
    CosineFilter,
    SplineFilter,
    LanczosRadiusFilter,
    CubicSplineFilter,
    SentinelFilter);  // a count of all the filters, not a real filter

and

function MagickResizeImage(wand: PMagickWand; const columns: size_t; rows: size_t; const filter: FilterType): MagickBooleanType; cdecl; external MagickWandDLL;

When I call MagickResizeImage(), I get an Access Violation :(

if I change const filter: FilterType to const filter: integer, it works.

Any idea what I am doing wrong?

like image 589
zeus Avatar asked Dec 23 '22 09:12

zeus


1 Answers

In C on Windows an enum is an int, so 4 bytes in size. In Delphi, by default, it is a single byte, or two bytes if there are more than 256 values, and so on.

You need to ensure that your Delphi type is the same size as the C type. For instance using the MINENUMSIZE directive.

{$MINENUMSIZE 4}

Place this before you define the enumerated type.

like image 169
David Heffernan Avatar answered Dec 28 '22 07:12

David Heffernan