English 中文(简体)
rror : 类型不能瓦解,因为嵌入式阵列的长度与布局中宣布的长度不符。
原标题:Error : Type could not be marshaled because the length of an embedded array instance does not match the declared length in the layout
  • 时间:2012-01-13 03:25:44
  •  标签:
  • c#

i am using a COM dll in my C# Project. I have one USERINFO Structure in my COM dll which looks like :

struct USERINFO
{
        BYTE UserID[USER_ID_SIZE];//42
        BYTE FirstName[NAME_SIZE];//66
        BYTE LastName[NAME_SIZE]; //66
        long Pin;
        BYTE TimeGroupIDList[IDLIST_SIZE];//10
        enum EUserKind   eUserKind;
        BYTE WarningEye;        
};

当我在C# Application中利用这一结构来填补用户数据的这一结构并转至我的AddUser API时,它就把这一错误归来。

Any help is appreciated. Thanks.

最佳回答

C#不支持结构内(不安全情况下除外)的固定宽度阵列,因此您的C级结构很可能被归入C#结构,如:

struct USERINFO
{
    MarshalAs(UnmanagedType.ByValArray, SizeConst = 42)
    BYTE[] UserID;
    MarshalAs(UnmanagedType.ByValArray, SizeConst = 66)
    BYTE[] FirstName;
    // etc.
};

请注意,成员是指阵列,而不是阵列。

For the marshalling to work the arrays have to be exactly the right length (or maybe at least the right length, I forget). For example, UserID should be initialised with userInfo.UserID = new byte[42];

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签