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 / Languages / C# / March 2008

Tip: Looking for answers? Try searching our database.

marshal array of formatted blittable classes

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
jonpb - 26 Mar 2008 23:05 GMT
Hi,
I would like to pass an array of formatted blittable classes through
P/Invoke to a C++ dll, but it is not working. Here are the definitions:

C++

struct Point2Rec
{
  double x;
  double y;
};

void WINAPI AddPolyToList(Point2Rec* pg1, int numPts);

C#

[StructLayout(LayoutKind.Sequential, Pack = 4)]
public class Point2D
{
  private double _x;
  private double _y;

  public double X { get { return _x; } set { _x = value; } }
  public double Y { get { return _y; } set { _y = value; } }
}

[DllImport("ascBool.dll")]
public static extern void AddPolyToList([In, Out] Point2D[] pg1, int
numPts);

If I change "class" to "struct" it works. I am taking my information
from the following documentation:

http://msdn2.microsoft.com/en-us/library/23acw07k(VS.80).aspx

Is there something I'm missing in the definitions of formatted and
blittable, or is it just not possible to pass an array of formatted
blittable classes to a C++ dll.

Thanks
Jeroen Mostert - 27 Mar 2008 00:48 GMT
> I would like to pass an array of formatted blittable classes through
> P/Invoke to a C++ dll, but it is not working. Here are the definitions:
[quoted text clipped - 26 lines]
>
> If I change "class" to "struct" it works.

Quite so. So your problem is...? Do you feel that using a class here gives
you an advantage over a struct?

The reason it doesn't work when you use "class" is because it turns Point2D
into a reference type. An array of reference types is marshaled as an array
of pointers (to COM proxies, in this case).

> I am taking my information
> from the following documentation:
>
> http://msdn2.microsoft.com/en-us/library/23acw07k(VS.80).aspx

The link to "Blittable and Non-Blittable Types" mentions (indirectly) that
reference types (including most classes) are not blittable and will be
converted in some way.

"Default Marshaling for Classes"
(http://msdn2.microsoft.com/en-us/library/s0968xy8(VS.80).aspx) and "Default
Marshaling vor Value Types"
(http://msdn2.microsoft.com/en-us/library/0t2cwe11(VS.80).aspx) provide more
insight.

Signature

J.


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.