System.AccessViolationException when calling DLL from WCF on IIS.

Posted by Wodzu on Stack Overflow See other posts from Stack Overflow or by Wodzu
Published on 2009-10-23T11:45:48Z Indexed on 2010/04/16 16:03 UTC
Read the original article Hit count: 953

Filed under:
|
|
|

Hi guys.

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:

Exception: System.AccessViolationException

Message: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.

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.

Here is how the wrapper for DLL looks like:

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()
    			{
    			}
    	}
}

this is how I call the DLL function:

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);

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.

I am using VS2008 SP.1, WinXP and IIS 5.1. Maybe the ISS itself is a problem?

I've tried all the workarounds that has been described here:

http://forums.asp.net/t/675515.aspx

Thansk for your time.

After edit: Installing Windows 2003 Server and IIS 6.0 solved the problem.

© Stack Overflow or respective owner

Related posts about c#

Related posts about iis