none
Problems accessing a structure in a C# wrapper RRS feed

  • Question

  • Hi,

    I have written a C# wrapper for a library of C functions. Most of the wrapper works correctly, when I call the C# methods, the underlying C functions perform as expected. The wrapper is compiled as a DLL which I've added as a reference in my project, and is brought in with "using CSWrapper"

    I have a problem in that in my wrapper I have a structure:

     [StructLayout(LayoutKind.Explicit)]
            public struct tagDecodeCommand
            {
                [FieldOffset(0)]
                public int nSize;
                [FieldOffset(4)]
                public ulong ulCommand;
                [FieldOffset(12)]
                public ulong ulFlags;
    
                //[StructLayout(LayoutKind.Explicit)]
                //public struct Param
                //{
                    [FieldOffset(20)]
                    public Int64 i64Param;
    
                    [FieldOffset(20)]
                    public byte bParam;
                    [FieldOffset(21)]
                    public UInt16 usParam;
                    [FieldOffset(23)]
                    public int nParam;
                    [FieldOffset(27)]
                    public UInt32 ulParam;
                    [FieldOffset(31)]
                    public long lParam;
                //}
            }

    I assgin values to the structure members with

    CSWrapper.tagDecodeCommand Command = new CSWrapper.tagDecodeCommand();    

    Command.nSize = Marshal.SizeOf(Command); Command.ulCommand = CSWrapper.ID_DECODE_SEEK; Command.ulFlags = CSWrapper.ID_DECODE_SEEK_FRAME | CSWrapper.ID_DECODE_INT; Command.i64Param = n64Pos;

    The application compiles without any problems, however when I run the application I get an unhandled exception error: "tagDecodeCommand.nSize".

    If I comment out the referece to nSize, I get a similar error for ulCommand

    I cannot work out why I can't assign values to the members of the structure. I wrote a simpler app to test whether what I'm trying will work and it did:

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Runtime.InteropServices;
    
    namespace StructTest
    {
        class Program
        {
            [StructLayout(LayoutKind.Explicit)]
            struct TestStruct
            {
                [FieldOffset(0)]
                public int a;
                [FieldOffset(4)]
                public int b;
            }
    
            static void TestFunc(TestStruct t)// (Object obj)
            {
                //TestStruct t = (TestStruct)obj;
    
    
                Console.WriteLine("Test Func A: {0:d} B: {1:d}", t.a, t.b);
            }
    
            static void Main(string[] args)
            {
                TestStruct test;// = new TestStruct();
    
                test.a = 10;
                test.b = 20;
    
                Console.WriteLine("A: {0:d} B: {1:d}\n", test.a, test.b);
    
                TestFunc(test);
    
                Console.ReadKey();
            }
        }
    }
    

    Can someone please explain why I'm having trouble accessing the members of the structure in my Wrapper

    Thanks in advance

    Andrew

    • Moved by Lisa Zhu Monday, May 13, 2013 5:13 AM CLR related
    Friday, May 10, 2013 4:11 PM

Answers

  • The code runs on XP with Net 4.0  I made changes to the code below to get the actual size of the variables.  The structure is giving me 8 bytes.  I suspect the size of int isn't 4 bytes, but 16 bytes on your system.  Notice I commented out the FieldOffsetin the structure as part of my experiment.

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Runtime.InteropServices;
    namespace StructTest
    {
        class Program
        {
            //[StructLayout(LayoutKind.Explicit)]
            struct TestStruct
            {
                //[FieldOffset(0)]
                public int a;
                //[FieldOffset(4)]
                public int b;
            }
            static void TestFunc(TestStruct t)// (Object obj)
            {
                //TestStruct t = (TestStruct)obj;
                Console.WriteLine("Test Func A: {0:d} B: {1:d} Size : {2}", 
                    t.a, t.b,System.Runtime.InteropServices.Marshal.SizeOf(t));
            }
            static void Main(string[] args)
            {
                TestStruct test;// = new TestStruct();
                test.a = 10;
                test.b = 20;
                Console.WriteLine("A: {0:d} B: {1:d} Size : {2}\n",
                    test.a, test.b, System.Runtime.InteropServices.Marshal.SizeOf(test));
                TestFunc(test);
                Console.ReadKey();
            }
        }
    }


    jdweng

    Sunday, May 12, 2013 11:31 PM

All replies

  • "I get an unhandled exception error: "tagDecodeCommand.nSize"."

    I don't understand the error, it that all the error message says? What's the type of the exception and where is it thrown?

    Friday, May 10, 2013 4:50 PM
    Moderator
  • Hi Mike,

    No further details are given. The exception appears to be thrown at

    Command.nSize

    If I use try and catch, it doesn't trap the exception, so I'm confused by whats going wrong.

    Sunday, May 12, 2013 10:47 PM
  • The code runs on XP with Net 4.0  I made changes to the code below to get the actual size of the variables.  The structure is giving me 8 bytes.  I suspect the size of int isn't 4 bytes, but 16 bytes on your system.  Notice I commented out the FieldOffsetin the structure as part of my experiment.

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Text;
    using System.Runtime.InteropServices;
    namespace StructTest
    {
        class Program
        {
            //[StructLayout(LayoutKind.Explicit)]
            struct TestStruct
            {
                //[FieldOffset(0)]
                public int a;
                //[FieldOffset(4)]
                public int b;
            }
            static void TestFunc(TestStruct t)// (Object obj)
            {
                //TestStruct t = (TestStruct)obj;
                Console.WriteLine("Test Func A: {0:d} B: {1:d} Size : {2}", 
                    t.a, t.b,System.Runtime.InteropServices.Marshal.SizeOf(t));
            }
            static void Main(string[] args)
            {
                TestStruct test;// = new TestStruct();
                test.a = 10;
                test.b = 20;
                Console.WriteLine("A: {0:d} B: {1:d} Size : {2}\n",
                    test.a, test.b, System.Runtime.InteropServices.Marshal.SizeOf(test));
                TestFunc(test);
                Console.ReadKey();
            }
        }
    }


    jdweng

    Sunday, May 12, 2013 11:31 PM
  • Hi Andrew,

    From your description, I ‘d like to move this post to  the most related forum.

    There are more  experts in this aspect, so you will get  better support and  may have more luck getting answers.

    Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, May 13, 2013 5:13 AM
  • "If I use try and catch, it doesn't trap the exception, so I'm confused by whats going wrong."

    Hmm, if you can't catch it may mean it's an AccessViolationException. But I don't see anything in the posted code that could cause it.

    Are there any other PInvoke calls before the exception happens? Maybe you have some memory corruption from a pervious call that did something wrong.

    Monday, May 13, 2013 5:34 AM
    Moderator
  • Did you run the test I asked?  the size of the structure may not be correct and you are trying to access an address outside the structure.  Please run the test I asked and verify the size of the structure is 8 bytes.

    jdweng

    Monday, May 13, 2013 3:32 PM
  • Hi again,

    I have found the problem, and fell pretty stupid. I had copied the wrong dll to my test system, so what I thought was there wasn't.

    The app works fine now.

    Andrew

    Monday, May 13, 2013 3:32 PM
  • Hi Joel,

    I ran the test, and the size of the structure was 8 bytes.

    Andrew

    Monday, May 13, 2013 3:53 PM