473,785 Members | 2,221 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9227
You can only use Marshal.SizeOf to get the size of a value type (struct) or
a class that has an explicit layout declaration (StructLayoutAt tribute),
none of the FCL classes have this attribute.

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

[StructLayout(La youtKind.Sequen tial)]
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*********@in valid.com> wrote in message
news:tedLd.2382 $bu.264@fed1rea d06...
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******** ********@TK2MSF TNGP10.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
(StructLayoutAt tribute), none of the FCL classes have this attribute.

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

[StructLayout(La youtKind.Sequen tial)]
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*********@in valid.com> wrote in message
news:tedLd.2382 $bu.264@fed1rea d06...
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
3826
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 class), how to decide if "sizeof(MyStruct)" would fail before calling sizeof and having to use Marshal.sizeof? TIA public struct MyStruct {
6
10820
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 lines)? TIA! sb
1
4196
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? thanks Claire
6
6698
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
1558
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
8144
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 = Marshal.AllocHGlobal(Marshal.SizeOf(wRegData)); Marshal.StructureToPtr(wRegData, p, false);
2
3186
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
2135
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 Detail(int _age)
1
2394
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. Code: public struct IDName { public ushort Id;
0
9647
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9489
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10356
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10162
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.