title: Marshalling C++ struct to C#
body: <p>I have such simple structure in unmanaged C++:</p>
  
  <pre><code>struct Cam
  {
      char ip[16];
      char login[16];
      char pass[16];
      char name[16];
  };
  </code></pre>
  
  <p>and C# marshalled structure:</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi, Pack = 1)]
  public struct Cam
  {
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
      public string ip;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
      public string login;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
      public string pass;
      [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 16)]
      public string name;
  }
  </code></pre>
  
  <p>When I pass C# struct to C++ lib by function</p>
  
  <pre><code>[DllImport("NVRLib.dll", CallingConvention = CallingConvention.Cdecl)]
  public static extern void AddCameraStruct(Cam cam);
  ...
  Cam cam = new Cam();
  cam.ip = "192.168.0.232";
  cam.login = "admin";
  cam.pass = "admin";
  cam.name = "kekekeke";
  AddCameraStruct(cam);
  </code></pre>
  
  <p>and print passed values in C++:</p>
  
  <pre><code>__declspec(dllexport) void AddCameraStruct(Camera cam)
  {
      printf(cam.ip);
      printf("\n");
      printf(cam.login);
      printf("\n");
      printf(cam.name);
      printf("\n");
      printf(cam.pass);
      strcpy(camera[CAM_NUM].ip, cam.ip);
      strcpy(camera[CAM_NUM].login, cam.login);
      strcpy(camera[CAM_NUM].pass, cam.pass);
      strcpy(camera[CAM_NUM].name, cam.name);
      CAM_NUM++;
  }
  </code></pre>
  
  <p>it prints:</p>
  
  <pre><code>232
  &lt;EMPTY LINE&gt;
  192.168.0.232
  n
  </code></pre>
  
  <p>What am I doing wrong?</p>