Re: Converting a C struct to C# for a P/Invoke call (struct in structs and unions)
- From: "David Browne" <davidbaxterbrowne no potted meat@xxxxxxxxxxx>
- Date: Sun, 10 Jul 2005 12:14:13 -0500
"Tom Hellström" <nospamplease@xxxxxxxx> wrote in message
news:42cfdc72$1_3@xxxxxxxxxxxxxxxxxxxxxxxxxxxx
> Hi, i want to make a call to a dll and I need to pass a pointer that then
> gets
> converted to a struct
> via the Marshal.PtrToStructure method. The thing is, the struct is a bit
> complicated.
>
> I need some tip on how to get around the union parts and also how to deal
> with
> the #if's at the bottom.
> Any ideas?
>
>
> typedef struct _devicemode {
> BCHAR dmDeviceName[CCHDEVICENAME];
> WORD dmSpecVersion;
> WORD dmDriverVersion;
> WORD dmSize;
> WORD dmDriverExtra;
> DWORD dmFields;
> union {
> struct {
> short dmOrientation;
> short dmPaperSize;
> short dmPaperLength;
> short dmPaperWidth;
> short dmScale;
> short dmCopies;
> short dmDefaultSource;
> short dmPrintQuality;
> };
> POINTL dmPosition;
> DWORD dmDisplayOrientation;
> DWORD dmDisplayFixedOutput;
> };
>
> short dmColor;
> short dmDuplex;
> short dmYResolution;
> short dmTTOption;
> short dmCollate;
> BYTE dmFormName[CCHFORMNAME];
> WORD dmLogPixels;
> DWORD dmBitsPerPel;
> DWORD dmPelsWidth;
> DWORD dmPelsHeight;
> union {
> DWORD dmDisplayFlags;
> DWORD dmNup;
> }
> DWORD dmDisplayFrequency;
> #if(WINVER >= 0x0400)
> DWORD dmICMMethod;
> DWORD dmICMIntent;
> DWORD dmMediaType;
> DWORD dmDitherType;
> DWORD dmReserved1;
> DWORD dmReserved2;
> #if (WINVER >= 0x0500) || (_WIN32_WINNT >= 0x0400)
> DWORD dmPanningWidth;
> DWORD dmPanningHeight;
> #endif
> #endif /* WINVER >= 0x0400 */
> } DEVMODE;
>
> have a nice summer
> ///. Tom H
>
>
Beyond a certian threshold of gruesomeness you might be better off using the
struct in a managed C++ wrapper. The wrapper can interact natively with the
struct and provide managed code with access to the data.
Another option is to figure out the size of the struct and just use a
managed byte array. Then use BitConverter and procedural logic to read and
write the struct. A hybrid approach is to break the struct into several
regions and procedurally marshal them. These approaches use pure C#, but
ironically require a higher level of understanding of C/C++ structs and
unmanaged memory than the C++ wrapper approach, since the C++ compiler
understands the struct for you.
David
.
- Follow-Ups:
- References:
- Converting a C struct to C# for a P/Invoke call (struct in structs and unions)
- From: Tom Hellström
- Converting a C struct to C# for a P/Invoke call (struct in structs and unions)
- Prev by Date: Console application
- Next by Date: Re: using CryptUnprotectData api from c#
- Previous by thread: Re: Converting a C struct to C# for a P/Invoke call (struct in structs and unions)
- Next by thread: Re: Converting a C struct to C# for a P/Invoke call (struct in structs and unions)
- Index(es):
Relevant Pages
|
Loading