title: DLLImport: passing short type parameter
body: <p>I'm trying to pass parameter of short type to C++ unmanaged function imported from DLL. In my C++ DLL code I have a following function:</p>
  
  <pre><code>__declspec(dllexport)void ChangeIcon(char *executableFile, char *iconFile,
      UINT16 imageCount)
  {
      // Some code, doesn't matter
  }
  </code></pre>
  
  <p>In C# managed code I import this function with the following code:</p>
  
  <pre><code>[DllImport("IconChanger.dll")]
  static extern void ChangeIcon(string executableFile, string iconFile,
      ushort imageCount);
  </code></pre>
  
  <p>And then I call it:</p>
  
  <pre><code>ushort imageCount = 2;
  ChangeIcon("filePath", "iconPath", imageCount);
  </code></pre>
  
  <p>The application executes the function just fine; however, the message with the following text pops out:</p>
  
  <blockquote>
    <p>Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'foo.exe'.
    Additional Information: A call to PInvoke function 'bar.IconChanger::ChangeIcon' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.</p>
  </blockquote>
  
  <p>If I do not pass the last parameter, the message doesn't pop out, so it must be due to passing short type. I have tried with int too, the same message appears.</p>
  
  <p>Obviously, I'm doing something wrong when passing this numerical parameter. How to match parameters between the two?</p>