title: DLLImport type problems
body: <p>I'm trying to import a C DLL into my C# project.
  The <strong>.h</strong> code looks like this:</p>
  
  <pre><code> _declspec(dllexport)   LRESULT CALLBACK SensFindDeviceW(LONG n, LPWSTR pwszMask, PSENSDEVICEW pDevice);
  </code></pre>
  
  <p>the _SENSDEVICEW struct:</p>
  
  <pre><code>typedef struct  _SENSDEVICEW
  {
      WCHAR       szSerialNo[32];
      WCHAR       szDeviceID[32];
      LONG        nIndex;
  } SENSDEVICEW, * PSENSDEVICEW;
  </code></pre>
  
  <p>So what I have done in <strong>C#</strong> until now is using the DLLImport:</p>
  
  <pre><code>[DllImport("dll/UFTAccess.dll", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
      static extern unsafe long SensFindDeviceW(long n, String pszMask, SENSDEVICEW pDevice);
  </code></pre>
  
  <p>Defining a struct:</p>
  
  <pre><code>[StructLayout(LayoutKind.Sequential)]
  unsafe struct SENSDEVICEW
  {
      public char[] szSerialNo;
      public char[] szDeviceID;
      public long nIndex;
  }
  </code></pre>
  
  <p>And trying to use it in a method:</p>
  
  <pre><code>SENSDEVICEW myDevice = new SENSDEVICEW();
  myDevice.szSerialNo = new char[32];
  myDevice.szDeviceID = new char[32];         
          long test =  SensFindDeviceW(1, "", myDevice);
  </code></pre>
  
  <p>With this a get <em>System.Runtime.InteropServices.SafeArrayTypeMismatchException</em>.</p>
  
  <p>If I don't initialize the arrays I get a stack imbalance error.</p>
  
  <p>I already tried using stringbuilder (which doesn't work) and marshalling the char-arrays like this: </p>
  
  <pre><code>[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)]
  string szSerialNo;
  </code></pre>
  
  <p>but it results in a stack imbalance.</p>
  
  <p>Maybe anyone of you has an idea what else I can try to get the dll to work?</p>