title: .Net c# marshalling of char *
body: <p>I am trying to call an existing C dll from a C# application, using System.Runtime.InteropServices, and am having difficulty matching the signatures between the PInvoke function and the Target function.</p>
  
  <p>The target function is</p>
  
  <pre><code>__declspec(dllexport) DWORD GetSomeString(char* strOut);
  </code></pre>
  
  <p>And my PInvoke function is</p>
  
  <pre><code>[DllImport("Existing.dll")]
  public static extern uint GetSomeString([MarshalAs(UnmanagedType.LPWStr)]
                              string strDisplay);
  </code></pre>
  
  <p>I make the function call </p>
  
  <pre><code>string tempStr = "My Output String";
  uint retVal = GetSomeString(tempStr);
  </code></pre>
  
  <p>But I get the message</p>
  
  <blockquote>
    <p>Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem...
    ...A call to PInvoke function 'GetSomeString' 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>I have also tried implementing the PInvoke function as</p>
  
  <pre><code>[DllImport("Existing.dll")]
  public static extern uint GetSomeString([MarshalAs(UnmanagedType.LPWStr)]
                              StringBuilder strDisplay);
  </code></pre>
  
  <p>But to no avail.</p>
  
  <p>Does anyone out there have an idea what I could be doing wrong?</p>
  
  <p>Please let me know if further info is required or my question is unclear.</p>
  
  <p>Thanks in advance.</p>