tags:

views:

204

answers:

2

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.

+1  A: 

I solved this by specifically targeting the X86 platform when I compile. I do not know why this worked but after 2 hours of pulling my hair out I did not care.

Gary
Thanks gary. I've already tried that out and it didn't help. It was suggested on the link which I've provided.
Wodzu
Thank you so much, I had an `AccessViolation` in a totally different context (`System.ServiceModel`) but targeting 'AnyCPU' instead of 'x86' made it go away for me. Definitely saved me some time!
Josef
A: 

This is an exception thrown in your un-managed code. If that code is completely black-boxed to you then you might want to reach the vendor/community regarding the issue in this particular version of the DLL you are using. It may really be a bug in the dll.

If this error happens occasionally, you might want to catch the exception and retry the call.

Newbie
It happened all the time. Installing IIS 6.0 solved the problem.
Wodzu