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 / Interop / March 2005

Tip: Looking for answers? Try searching our database.

Marshal C# -> C DLL: Automatic or explicit?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
TedSharp - 25 Mar 2005 03:35 GMT
I have a C struct and a DLL with a single function.  I've used the
MarshalAsAttribute, ref, DLllImport, StructLayout, UnmanagedType, etc...
declarations and I'm not having any luck.

I'm not really clear on when you can use the special declarations for
automatic marshalling or when (or how) you use IntPtr, Marshal.AllocHGlobal,
etc...

Any suggestions on how to translate my C to C# and/or any high-level
feedback on when to use the different approached (especially when the
structures have pointers to other structs or arrays)

Thank you --- Ted

// C
int Populate(int flags,PhotoBuffer *photo);

typedef struct {
 byte *buffer;
 int bufferSize;
 long mode;
}  PhotoBuffer

// C#

[DllImport("photo.dll",CharSet=CharSet.Ansi,CallingConvention=CallingConvention.StdCall,SetLastError=true)]
public static extern int populate(int flags, ref aPhotoBuffer);

[StructLayout(LayoutKind.Sequential, CharSet=CharSet.Auto)]
public class PhotoBuffer
{
    [MarshalAs(UnmanagedType.LPArray)] public byte[] travBuff;
    [MarshalAs(UnmanagedType.I4)] public int bifferSize
    [MarshalAs(UnmanagedType.U8)] public long ,mode;
}

PhotoBuffer photo=new PhotoBuffer();
photo.mode=1;
       
byte[] imageData=new byte[2048];
photo.buffer=imageData;           
photo.bufferSize=2048;

int returnCode=Populate(2,ref photo);
Willy Denoyette [MVP] - 25 Mar 2005 21:54 GMT
>I have a C struct and a DLL with a single function.  I've used the
> MarshalAsAttribute, ref, DLllImport, StructLayout, UnmanagedType, etc...
[quoted text clipped - 41 lines]
>
> int returnCode=Populate(2,ref photo);

1. long in C#  is 64 bit!
2. You can't marshal array's by reference when contained in a struct or
class, use an IntPtr instead.

Following should do....

 [StructLayout(LayoutKind.Sequential)]
struct PhotoBuffer
{
   internal IntPtr buffer;
   internal int size;
   internal int mode;
}

     PhotoBuffer pbu = new PhotoBuffer();
     pbu.mode = 2;
     byte[] bytearr = new byte[10] {60, 61, 62, 63, 64, 65, 66, 67, 68,
69};
     pbu.size = bytearr.Length;
     GCHandle gcH = GCHandle.Alloc(bytearr);    // Allocate a GCHandle for
the managed array
     pbu.buffer = Marshal.UnsafeAddrOfPinnedArrayElement(bytearr, 0);    //
pin the array and return a pointer to the first element
     int returnCode = Populate(2, ref pbu);
     gcH.Free();    // Free the buffer

Willy.
TedSharp - 29 Mar 2005 04:29 GMT
Willy ---

Thanks, it worked.  So , I did "Explicit" allocated of out-of-CLR memory and
are using IntPtr.  

Your long = 64 bit comment made me search some comphrehensive info covering
C <-> C# datatype sizes.  I found this link
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmxspec/html/v
cmg_PlatformInvocationServices.asp


--- Ted

> >I have a C struct and a DLL with a single function.  I've used the
> > MarshalAsAttribute, ref, DLllImport, StructLayout, UnmanagedType, etc...
[quoted text clipped - 69 lines]
>
> Willy.

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.