PInvoke problem

  • Thread starter Thread starter nicolasr
  • Start date Start date
N

nicolasr

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
 
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]
 
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?
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.
Do you have a small example for this? I'm quite new to the Marshal stuff.

thanks
Nick
 
Back
Top