title: System.AccessViolationException when calling DLL from WCF on IIS
body: <p>I've created just a test WCF service in which I need to call an external DLL.
  Everything works fine under Visutal Studio development server. However, when I try to use my service on IIS I am getting this error:</p>
  
  <blockquote>
    <p>Exception:
    System.AccessViolationException</p>
    
    <p>Message: Attempted to read or write
    protected memory. This is often an
    indication that other memory is
    corrupt.</p>
  </blockquote>
  
  <p>The stack trace leeds to the call of DLL which is presented below.
  After a lot of reading and experimenting I am almost sure that the error is caused by wrong passing strings to the called function.</p>
  
  <p>Here is how the wrapper for DLL looks like:</p>
  
  <pre><code>using System;
  using System.Runtime.InteropServices; 
  using System.Text;
  using System;
  using System.Security;
  using System.Security.Permissions;
  using System.Runtime.InteropServices;
  
  namespace cdn_api_wodzu
  {
          public class cdn_api_wodzu
          {
  
                  [DllImport("cdn_api.dll", CharSet=CharSet.Ansi)]
               // [SecurityPermission(SecurityAction.Assert, Unrestricted = true)]
                  public static extern int XLLogin([In, Out] XLLoginInfo _lLoginInfo, ref int _lSesjaID);
          }
          [Serializable, StructLayout(LayoutKind.Sequential)]
          public class XLLoginInfo
          {
                  public int Wersja;
                  public int UtworzWlasnaSesje;
                  public int Winieta;
                  public int TrybWsadowy;
                  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x29)]
                  public string ProgramID;
                  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x15)]
                  public string Baza;
                  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
                  public string OpeIdent;
                  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 9)]
                  public string OpeHaslo;
                  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 200)]
                  public string PlikLog;
                  [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 0x65)]
                  public string SerwerKlucza;
                  public XLLoginInfo()
                  {
                  }
          }
  }
  </code></pre>
  
  <p>this is how I call the DLL function:</p>
  
  <pre><code>int ErrorID = 0; int SessionID = 0;
  XLLoginInfo Login;
  Login = new XLLoginInfo();
  Login.Wersja = 18; 
  Login.UtworzWlasnaSesje = 1; 
  Login.Winieta = -1; 
  Login.TrybWsadowy = 1; 
  Login.ProgramID = "TestProgram";
  Login.Baza = "TestBase";
  Login.OpeIdent = "TestUser";
  Login.OpeHaslo = "TestPassword";
  Login.PlikLog = "C:\\LogFile.txt";
  Login.SerwerKlucza = "MyServ\\MyInstance";
  ErrorID = cdn_api_wodzu.cdn_api_wodzu.XLLogin(Login, ref SessionID); 
  </code></pre>
  
  <p>When I comment all the string field assigments the function works - it returns me an error message that the program ID has not been given. But when I try to assign a ProgramID  (or any other string fields, or all at once) then I am getting the mentioned exception.</p>
  
  <p>I am using VS2008 SP.1, WinXP and IIS 5.1. Maybe the ISS itself is a problem?</p>
  
  <p>I've tried all the workarounds that has been described here:</p>
  
  <p><a href="http://forums.asp.net/t/675515.aspx" rel="nofollow noreferrer">http://forums.asp.net/t/675515.aspx</a></p>
  
  <p>Thansk for your time.</p>
  
  <p><strong>After edit:</strong> Installing Windows 2003 Server and IIS 6.0 solved the problem.</p>