Click here to Skip to main content
15,920,896 members
Home / Discussions / C#
   

C#

 
GeneralRe: Read outlook express dbx files Pin
Mazdak1-Feb-04 22:51
Mazdak1-Feb-04 22:51 
GeneralUse of Structures in Unmanaged Code Pin
zelbezach1-Feb-04 20:00
zelbezach1-Feb-04 20:00 
I am trying to use the method SizeOf to determine the size of a structure containing arrays, among other elements. I succeeded in doing this by using MarshalAs with UnmanagedType and SizeConst. This works for structures with simple arrays, but for more complicated structures (such as nested ones), it compiles and links, but bombs during execution.

Below is sample code, with comments indicating exactly which line of code makes it bomb, and exactly where it bombs.

Do you have any suggestions? (I am running on .NET)

Thank you very much.


using System ;
using System.IO ;
using System.Runtime.InteropServices;

public class classMain
{
public static void Main ()
{
System.Type type = typeof (CLASS.STRUCT) ;

/* Here it bombs: ===> */ int length = Marshal.SizeOf (type) ;
return ;
}
}

public class CLASS
{
[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public struct float_3
{
[MarshalAs( UnmanagedType.ByValArray, SizeConst= 3 )]
public float [] FLOAT ;
}

[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public struct float_3_3
{
[MarshalAs( UnmanagedType.ByValArray, SizeConst= 3 )]
public float_3 [] FLOAT_3 ;
}

[StructLayout(LayoutKind.Sequential, Pack=1, CharSet=CharSet.Ansi)]
public struct STRUCT
{
[MarshalAs( UnmanagedType.ByValArray, SizeConst= 10 )]
ushort [] USHORT ;

[MarshalAs( UnmanagedType.ByValArray, SizeConst= 2 )]
/* This makes it bomb: ===> */ public float_3_3 [] FLOAT_3_3 ;
}
}


zelbezach
GeneralC# Pin
BP1281-Feb-04 15:55
BP1281-Feb-04 15:55 
GeneralRe: C# Pin
Jonathan Slenders_2-Feb-04 0:47
sussJonathan Slenders_2-Feb-04 0:47 
GeneralRe: C# Pin
Heath Stewart2-Feb-04 3:47
protectorHeath Stewart2-Feb-04 3:47 
GeneralInclude .NET Pin
adyrds1-Feb-04 11:48
adyrds1-Feb-04 11:48 
GeneralRe: Include .NET Pin
Bo Hunter1-Feb-04 12:12
Bo Hunter1-Feb-04 12:12 
GeneralRe: Include .NET Pin
jkerns2-Feb-04 2:41
jkerns2-Feb-04 2:41 
GeneralRe: Include .NET Pin
Heath Stewart2-Feb-04 4:01
protectorHeath Stewart2-Feb-04 4:01 
GeneralRe: Include .NET Pin
Anonymous2-Feb-04 10:28
Anonymous2-Feb-04 10:28 
GeneralPopup menus (please help me) Pin
Jonathan S1-Feb-04 9:26
Jonathan S1-Feb-04 9:26 
GeneralRe: Popup menus (please help me) Pin
Nick Parker1-Feb-04 9:32
protectorNick Parker1-Feb-04 9:32 
GeneralWin32 API functions Pin
zlaty1-Feb-04 9:11
zlaty1-Feb-04 9:11 
GeneralRe: Win32 API functions Pin
Nick Parker1-Feb-04 9:37
protectorNick Parker1-Feb-04 9:37 
GeneralRe: Win32 API functions Pin
zlaty1-Feb-04 9:46
zlaty1-Feb-04 9:46 
GeneralRe: Win32 API functions Pin
Mazdak1-Feb-04 9:37
Mazdak1-Feb-04 9:37 
GeneralRe: Win32 API functions Pin
zlaty1-Feb-04 9:46
zlaty1-Feb-04 9:46 
GeneralRe: Win32 API functions Pin
Nick Parker1-Feb-04 9:53
protectorNick Parker1-Feb-04 9:53 
GeneralRe: Win32 API functions Pin
Mazdak1-Feb-04 9:56
Mazdak1-Feb-04 9:56 
GeneralQuestion on a class communicating with main form Pin
Flack1-Feb-04 8:30
Flack1-Feb-04 8:30 
GeneralRe: Question on a class communicating with main form Pin
Nick Parker1-Feb-04 8:38
protectorNick Parker1-Feb-04 8:38 
GeneralRe: Question on a class communicating with main form Pin
Charlie Williams1-Feb-04 14:26
Charlie Williams1-Feb-04 14:26 
GeneralQuestion on tabpages and autoscroll. Pin
Flack1-Feb-04 8:20
Flack1-Feb-04 8:20 
GeneralRe: Question on tabpages and autoscroll. Pin
Heath Stewart2-Feb-04 4:10
protectorHeath Stewart2-Feb-04 4:10 
GeneralDataSets and Diffgrams Pin
MrJJKoolJ1-Feb-04 4:55
MrJJKoolJ1-Feb-04 4:55 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.