HOW-TO Marshall structure containing wchar array and char array?

Posted on 2009-02-16
Last Modified: 2013-11-12

I have a structure as below:

struct FooStruct
      char fooChar[10];
      wchar fooWChar[20];

I would like to marshall it ..
I tried the following:

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct fooStruct
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst=10)]
  public string charArray;
  [MarshalAs(UnmanagedType.ByValArray, SizeConst=20*2)]
  public string wcharArray;

But I get - Cannot marshal field 'wcharArray' of type 'fooObj.wcharArray': Invalid managed/unmanaged type combination (String fields must be paired with LPStr, LPWStr, LPTStr, BStr or ByValTStr).

If I use as below, I get junk:
  [MarshalAs(UnmanagedType.ByValTSr, SizeConst=20*2)]
  public string wcharArray;

Please help !

Note: I need the size of the struct to be 50 ( 10 + 20*2 ), and I cannot change the unmanaged struct.

Question by:libin_v
    LVL 8

    Expert Comment

    what do you mean you get junk when you use UnmanagedType.ByValTSr?

    Have you tried UnmanagedType.LPWStr
    LVL 5

    Author Comment

    I have tried LPWStr, for which I get junk again.

    By junk I mean, if assigned "hello" to wcharArray, in the unmanaged dll I get non-printable characters and they keep changing on every exectution.
    LVL 8

    Expert Comment


    Maybe try just passing the byte array and converting to a string to see if you are at least passing the data correctly.

    Something like this:

    public byte[] wcharArray;

    LVL 5

    Accepted Solution

    I got it from this tool -

    [System.Runtime.InteropServices.StructLayoutAttribute(System.Runtime.InteropServices.LayoutKind.Sequential, CharSet=System.Runtime.InteropServices.CharSet.Ansi)]
    public struct FooStruct {
        /// char[10]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValTStr, SizeConst=10)]
        public string fooChar;
        /// wchar[20]
        [System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=20, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I2)]
        public char[] fooWChar;

    Open in new window


    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Join & Write a Comment

    In my previous article ( we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
    In my previous two articles we discussed Binary Serialization ( and XML Serialization ( In this article we will try to know more about SOAP (Simple Object Acces…
    This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor ( If you're looking for how to monitor bandwidth using netflow or packet s…
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    24 Experts available now in Live!

    Get 1:1 Help Now