473,776 Members | 2,228 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

SizeOf() method fails with ICustomMarshale r

Hello,

When I try to implement a custom marshaler I get an exception when the
Marshal.SizeOf( ) method is invoked.
The message is:
"Type MyNS.PT_DATAFRA ME can not be marshaled as an unmanaged structure;
no meaningful size or offset can be computed."

Is custom marshaling possible with a struct? Perhaps a better question
is help, what am I doing wrong here?

I attached a code snippet to show the problem.

Thanks!

*************** ******

using System;
using System.Runtime. InteropServices ;
using System.Runtime. Serialization.F ormatters.Binar y;
using System.IO;
using System.Windows. Forms;

namespace MyNS
{
[Serializable()]
[StructLayout( LayoutKind.Sequ ential)]
struct PT_DATAFRAME
{
[MarshalAs(Unman agedType.Custom Marshaler, MarshalTypeRef =
typeof(MyNS.Dum myMarshaler))]
public string myValue; //Just a value that can be marshaled
};

public class AppMain
{
[STAThread]
static void Main()
{
PT_DATAFRAME Frame = new PT_DATAFRAME();
Frame.myValue = "Joehoo";
Int32 size=Marshal.Si zeOf( Frame );
/* Exception at this point:
Message:
Type MyNS.PT_DATAFRA ME can not be marshaled as an unmanaged
structure;
no meaningful size or offset can be computed.
??????????????? ???
*/
}
}

// This custom marshaler does not implement anything meaningful.
// Not any of the methods will be invoked.
public class DummyMarshaler : ICustomMarshale r
{
DummyMarshaler( ) { MessageBox.Show ("Never Shown"); }

public static ICustomMarshale r GetInstance( string cookie )
{
MessageBox.Show ("Never Shown");
return new DummyMarshaler( );
}

public object MarshalNativeTo Managed( System.IntPtr pNativeData )
{
MessageBox.Show ("Never Shown");
return pNativeData;
}

public IntPtr MarshalManagedT oNative( object ManagedObj )
{
MessageBox.Show ("Never Shown");
return (IntPtr) new System.Int32();
}

public void CleanUpNativeDa ta( IntPtr pNativeData )
{
MessageBox.Show ("Never Shown");
return;
}

public int GetNativeDataSi ze()
{
MessageBox.Show ("Never Shown");
return ( 10 ); // Just a number
}

public void CleanUpManagedD ata( object ManagedObj )
{
MessageBox.Show ("Never Shown");
return;
}
}

}

Nov 16 '05 #1
0 1226

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

Similar topics

3
3556
by: Sunil Menon | last post by:
Dear All, A class having no member variables and only a method sizeof(object) will return 1byte in ANSI and two bytes in Unicode. I have the answer for this of how in works in ANSI. But I don't know it returns two bytes in UniCode. Please help... For ANSI: In ISO/ANSI C++ Standard, 5.3.3 § 1, it stays: "The sizeof operator yields the number of bytes in the object representation of its
4
2904
by: daniel.w.gelder | last post by:
I wrote a template class that takes a function prototype and lets you store and call a C-level function, like this: inline string SampleFunction(int, bool) {..} functor<string (int, bool)> myFunctor = SampleFunction; string result = myFunctor(7, true); Works great thanks to the help from this group. Here's the guts so far for two-arity:
9
3025
by: M Welinder | last post by:
This doesn't work with any C compiler that I can find. They all report a syntax error: printf ("%d\n", (int)sizeof (char)(char)2); Now the question is "why?" "sizeof" and "(char)" have identical precedence and right-to-left parsing, so why isn't the above equivalent to printf ("%d\n", (int)sizeof ((char)(char)2));
11
12037
by: D | last post by:
hi, i would like to know how to calculate the size of a dynamic array created using a dereference declaration like int *numbers and allocating via malloc or calloc: numbers=(int *)malloc(sizeof(int)*10); using sizeof(numbers) will return the pointers size... is there a possibility?
0
1307
by: Carlos Guzmán Álvarez | last post by:
Hello: I'm trying to implement a custom marshaler for a field of an struct, the field is an array of struct type, but it's not working, i want to know if this is possible ?? The structure: public struct XSQLDA
74
4698
by: ballpointpenthief | last post by:
If I have malloc()'ed a pointer and want to read from it as if it were an array, I need to know that I won't be reading past the last index. If this is a pointer to a pointer, a common technique seems to be setting a NULL pointer to the end of the list, and here we know that the allocated memory has been exhausted. All good. When this is a pointer to another type, say int, I could have a variable that records how much memory is being...
29
2703
by: lnitsu | last post by:
Hello I've got a simple question regarding sizeof operator. why sizeof 'a' return 4? (Note the argument is without parenthesis) If anybody is so kind to answer ... TIA
7
1972
by: Yen Kwoon | last post by:
Note: This problem is related to gcc but after some back and forth in group gnu.gcc.help it seems to have morph into more of a c++ specificiation question, hence the transplanting to this group. The original post at gnu.gcc.help can be found at this link http://groups.google.com/group/gnu.gcc.help/browse_thread/thread/ece55cbbd9c36270/2148a6c1ac6119e1?lnk=st&q=#2148a6c1ac6119e1 Here's the question: class base {
6
1175
by: WDS | last post by:
I ran across some old C code that was written something like this: int array; .. . . memset(array, -1, sizeof(int)*NUM_ELEMENTS); The code is filled with this idiom on memsets (and memcpys and ...). I am trying to figure out why they just didn't code it this way: memset(array, -1, sizeof(array));
0
9459
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
10282
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
10117
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...
1
10056
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8948
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
5365
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5489
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3619
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2857
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.