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# / January 2008

Tip: Looking for answers? Try searching our database.

Non-bittable data in struct

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Soren S. Jorgensen - 11 Jan 2008 11:26 GMT
Hi,

I got a structure defined in C as:

typedef struct _M4W_SERVICE_MESSAGE
{
ULONG Command;
ULONG Reserved; // Alignment on IA64
LPWSTR FileName;
} M4W_SERVICE_MESSAGE, *PM4W_SERVICE_MESSAGE;

and in C# as:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
private struct SERVICE_MESSAGE
{
public uint Command;
public uint Reserved;
[MarshalAs(UnmanagedType.LPWStr)] public string FileName;
}

but when trying to create a GChandle allocated buffer of type
SERVICE_MESSAGE (to recieve data through a native overlapped operation),
GCHandle.Alloc throws exception:

System.ArgumentException: Object contains non-primitive or non-blittable
data.
  at System.Runtime.InteropServices.GCHandle.InternalAlloc(Object value,
GCHandleType type)
  at System.Runtime.InteropServices.GCHandle.Alloc(Object value,
GCHandleType type)

Obviously the problem is the string data in the buffer, but why???
I have used other structures (a least i think to remember so) that contained
string data.

When removing the FileName variable from struct no exception occurs!!

Thanks, SSJ
christery@gmail.com - 11 Jan 2008 15:45 GMT
Think of anything but C, this is what I wrote a couple of weeks ago...

           StringBuilder sb1 = new StringBuilder("Hello");
           StringBuilder sb2 = new StringBuilder(" world");
           sb1.Append(sb2);
           Console.WriteLine(sb1); // Hello world
           sb1 = sb2;
          Console.WriteLine(sb1); //  world

And then... it hit me... that time

           StringBuilder sb1 = new StringBuilder("Hello");
           StringBuilder sb2 = new StringBuilder(" world");
           sb1.Append(sb2);
           Console.WriteLine(sb1); // Hello world
           sb1 = sb2;
           Console.WriteLine(sb1); //  world
           Console.WriteLine(sb2); //  world

A string is not always an reference... ;)

Hope this helps...

//CY

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.