Problem in Getting the exact char[] data from C++ to C# String

Dec 22 2006 7:51 AM

Hi, 
I am facing a problem while accessing a C++ dll from C# code. 
Following is my C++ structure I am using...
typedef struct
 {
    LONG   lPaBitNr;
    UCHAR  szPaAbr[8];
 } AudioUnit_T_DataType;

and corresponding structure I am using in Dot Net is:
 
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
 public struct AudioUnit_T_DataType
 {
  public int paBitNr;
  [MarshalAs(UnmanagedType.ByValTStr, SizeConst=8)]
  public String paAbr;
 }

 and following is my declaration of the function of C++ dll.
 [DllImport("MyLib.dll", EntryPoint = "MyFunc")]
 public static extern int MyFunc(
  [MarshalAs(UnmanagedType.AsAny)]
  object key,
  [MarshalAs(UnmanagedType.AsAny),In, Out]
  object data,
  int size
  );
size is the respective size of the structure (no of bytes to copy in data object)

When I call the MyFunc function with object data as AudioUnit_T_DataType structure,, i got all the data but I always loose the last character (8th) in the String paAbr.
i.e. suppose, the value of szPaAbr[8] was "abcdefgh"
then, after the call of MyFunc function, i got "abcdefg" value.. 'h' is lost (might be it is replaced by null character...) 
What should I do to overcome this issue.
I dont want to change the lenght of array from 8 to 9.

Any help is appreciable.


Answers (1)