PInvoke problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • nicolasr

    #1

    PInvoke problem

    Hi,
    I need to PInvoke the ExtCreateRegion API.
    How do I declare the structures used by it and
    the function itself in C#?

    The original declarations are:

    typedef struct _RGNDATAHEADER
    {
    DWORD dwSize;
    DWORD iType;
    DWORD nCount;
    DWORD nRgnSize;
    RECT rcBound;
    } RGNDATAHEADER;


    typedef struct _RGNDATA
    {
    RGNDATAHEADER rdh;
    char Buffer[1];
    } RGNDATA;

    //I don't need lpXform so I assume I could
    //declare it as pointer or int and always pass null?
    HRGN ExtCreateRegion (
    CONST XFORM *lpXform,
    DWORD nCount,
    CONST RGNDATA *lpRgnData
    );

    Somehow my main problem seems to be
    the char Buffer[1] element in RGNDATA.

    thanks for any help,
    Nick


  • Rob Teixeira [MVP]

    #2
    Re: PInvoke problem

    You can avoid P/Invoke completely by using the classes in
    System.Drawing. Drawing2D. It allows you to play with regions natively.
    The char Buffer[1] is one of my C++ pet peeves. It's really a pointer to a
    series of rectangles (array of rectangles if you will).
    You can define it as an array of rectangles, and tweak the MarshalAs
    attribute to pass a pointer to the first element.
    You will also need to use the Marshal class to get the Size of the rectangle
    data. If you never use lpXform, you can define it as an IntPtr and pass
    IntPtr.Zero (which basically translates to C++ null).
    However, I recommend using the native classes instead of using P/Invoke.
    Makes life easier.

    -Rob Teixeira [MVP]

    "nicolasr" <nicolasrNOSPAM ATSIGNgmx.net> wrote in message
    news:%23LKy3t3w DHA.2092@TK2MSF TNGP09.phx.gbl. ..[color=blue]
    > Hi,
    > I need to PInvoke the ExtCreateRegion API.
    > How do I declare the structures used by it and
    > the function itself in C#?
    >
    > The original declarations are:
    >
    > typedef struct _RGNDATAHEADER
    > {
    > DWORD dwSize;
    > DWORD iType;
    > DWORD nCount;
    > DWORD nRgnSize;
    > RECT rcBound;
    > } RGNDATAHEADER;
    >
    >
    > typedef struct _RGNDATA
    > {
    > RGNDATAHEADER rdh;
    > char Buffer[1];
    > } RGNDATA;
    >
    > //I don't need lpXform so I assume I could
    > //declare it as pointer or int and always pass null?
    > HRGN ExtCreateRegion (
    > CONST XFORM *lpXform,
    > DWORD nCount,
    > CONST RGNDATA *lpRgnData
    > );
    >
    > Somehow my main problem seems to be
    > the char Buffer[1] element in RGNDATA.
    >
    > thanks for any help,
    > Nick
    >
    >[/color]


    Comment

    • nicolasr

      #3
      Re: PInvoke problem

      Hi and thanks for the reply!

      The reason why I tried to use ExtCreateRegion was for
      performance. My code scans large bitmaps and creates
      rectangles for all contiguous pixels that do not match certain
      colors. Finally a region should be created from all rectangles.
      I assumed that ExtCreateRegion would be faster than repeatedly
      using Region.Combine( ) ?
      Now after you pointed me to Drawing2D I found the RegionData
      class. The documentation does, however, not explain what its
      Data property really is, just byte[].
      Looking at the platform docs for GetRegionData() it is probably
      again a RGNDATA struct?
      [color=blue]
      > The char Buffer[1] is one of my C++ pet peeves. It's really a pointer to a
      > series of rectangles (array of rectangles if you will).
      > You can define it as an array of rectangles, and tweak the MarshalAs
      > attribute to pass a pointer to the first element.
      > You will also need to use the Marshal class to get the Size of the[/color]
      rectangle[color=blue]
      > data.[/color]
      Do you have a small example for this? I'm quite new to the Marshal stuff.

      thanks
      Nick


      Comment

      Working...