Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / July 2005

Tip: Looking for answers? Try searching our database.

Converting a C struct to C# for a P/Invoke call (struct in structs and unions)

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Hellström - 09 Jul 2005 15:16 GMT
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
Morten Wennevik - 10 Jul 2005 09:53 GMT
Hi Tom,

I won't attempt to answer this myself, but this site might give you a few ideas.

http://www.pinvoke.net/

> Hi, i want to make a call to a dll and I need to pass a pointer that then gets
> converted to a struct
[quoted text clipped - 63 lines]
> http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
> ----= East and West-Coast Server Farms - Total Privacy via Encryption =----

Signature

Happy coding!
Morten Wennevik [C# MVP]

Tom Hellström - 14 Jul 2005 13:08 GMT
Hi Morten, thanks for the tip, there where a C# version of the struct at that
site that I could use.
Excellent.

>Hi Tom,
>
[quoted text clipped - 74 lines]
>>Newsgroups
>> ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
David Browne - 10 Jul 2005 18:14 GMT
> Hi, i want to make a call to a dll and I need to pass a pointer that then
> gets
[quoted text clipped - 61 lines]
> 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
Tom Hellström - 14 Jul 2005 13:10 GMT
>> Hi, i want to make a call to a dll and I need to pass a pointer that then
>> gets
[quoted text clipped - 75 lines]
>
>David

Well, I got hold of a C# version of the struct, but I ran into some other
problems, where
your tip about splitting it up and marshaling the separate pices worked fine.

Thanks,
///. Tom

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.