473,387 Members | 1,588 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Marshal.SizeOf()

Why do I get the following run time error:

Additional information: Type System.Object can not be marshaled as an
unmanaged structure; no meaningful size or offset can be computed.

When the following code is executed?
object test = new object();

int temp = Marshal.SizeOf(test);

Isn't the size of test determined during its creation?

Thanks,

Eric
Nov 16 '05 #1
2 9195
You can only use Marshal.SizeOf to get the size of a value type (struct) or
a class that has an explicit layout declaration (StructLayoutAttribute),
none of the FCL classes have this attribute.

Both....
struct os
{
int i;
}

[StructLayout(LayoutKind.Sequential)]
class MyClass
{
int i;
}

will return 4, but using any FCL class will fail.
Note that this method is only usable in interop scenario's where you may
need the size of the (or to be) marshaled data portion of a class or
struct.
Do not use this to get the real size of the managed type however.

Willy.

"Beringer" <bo*********@invalid.com> wrote in message
news:tedLd.2382$bu.264@fed1read06...
Why do I get the following run time error:

Additional information: Type System.Object can not be marshaled as an
unmanaged structure; no meaningful size or offset can be computed.

When the following code is executed?
object test = new object();

int temp = Marshal.SizeOf(test);

Isn't the size of test determined during its creation?

Thanks,

Eric

Nov 16 '05 #2
Thanks for the response.
I later found this information after searching MSDN for awhile.
Eric
"Willy Denoyette [MVP]" <wi*************@pandora.be> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
You can only use Marshal.SizeOf to get the size of a value type (struct)
or a class that has an explicit layout declaration
(StructLayoutAttribute), none of the FCL classes have this attribute.

Both....
struct os
{
int i;
}

[StructLayout(LayoutKind.Sequential)]
class MyClass
{
int i;
}

will return 4, but using any FCL class will fail.
Note that this method is only usable in interop scenario's where you may
need the size of the (or to be) marshaled data portion of a class or
struct.
Do not use this to get the real size of the managed type however.

Willy.

"Beringer" <bo*********@invalid.com> wrote in message
news:tedLd.2382$bu.264@fed1read06...
Why do I get the following run time error:

Additional information: Type System.Object can not be marshaled as an
unmanaged structure; no meaningful size or offset can be computed.

When the following code is executed?
object test = new object();

int temp = Marshal.SizeOf(test);

Isn't the size of test determined during its creation?

Thanks,

Eric


Nov 16 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: William Stacey | last post by:
Using the following struct def, how can I tell (using reflection) if "ba" has the marshal attribute and get the "ByValArray" and maybe even the size? In the bigger picture, given a struct (or a...
6
by: SB | last post by:
I feel dumb to ask because I bet this is a simple question... Looking at the code below, can someone please explain why I get two different values in my Marshal.SizeOf calls (see the commented...
1
by: Claire | last post by:
char a = 'p'; if (System.Runtime.InteropServices.Marshal.SizeOf(a) == 1) dothis(); else dothat(); SizeOf(a) is returned as 1. I thought chars were 16 bits in size. Why is it returning 1 to me?...
6
by: Howard Kaikow | last post by:
Given: private struct PROCESSENTRY32 { public int dwSize; public int cntUsage; public int th32ProcessID; public int th32DefaultHeapID; public int th32ModuleID;
4
by: marcosegurini | last post by:
Hi. Is is possible to mark a class-member-variable to avoid its marshaling? class MyClass { int i_; IntPtr point_;
2
by: scottt | last post by:
I need to call into a C++ DLL from my C# code. The function is expecting a void pointer to an unsigned short. Which would be more correct? UInt16 wRegData; IntPtr p =...
2
by: RYoung | last post by:
Given this native struct: typedef struct vendor { char name; } VENDOR I want to make managed equivalent, so I did this: public value struct Vendor
0
by: Charming12 | last post by:
Hi All, I have a strange problem and due to my inefficiency with IntPtr i am unable to figure it out. I have an structure something like: public struct Detail { public int age; public...
1
by: Charming12 | last post by:
Hi All, I am using System.Runtime.InteropServices; to marshal a structure using Marshal.structureToPtr(). But to get the size of structure when i get Marshal.sizeOf(), it gives me improper sizes....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.