I am using external 32bit c++ library which communicates with some hardware, that library is used as managed assembly inside my code.
When I use it with console application, it works without any issues. When code is hosted on IIS I have several issues.
First, I was getting stackoverflow exception continuously, until I set Application Pool on IIS to enable 32bit application and changed my code to increase stack size. I used example from here How to change stack size for a .NET program?.
Here is the example code after change:
public class SomeExternalLibraryClient
{
[DllImport("SomeExternalLibrary.dll")]
internal static extern int open();
public delegate void OnEventCallback(int reserved1, int eventType, int eventData1, int eventData2, string dataStr, int reserved2);
[DllImport("SomeExternalLibrary.dll", CallingConvention = CallingConvention.Cdecl)]
static extern double subscribeToEvents([MarshalAs(UnmanagedType.FunctionPtr)]OnEventCallback func);
static OnEventCallback callback = new OnEventCallback(OnEvent);
static void OnEvent(int reserved1, int eventType, int eventData1, int eventData2, string dataStr, int reserved2)
{
callback(reserved1, eventType, eventData1, eventData2, dataStr, reserved2);
}
public int Open()
{
int openValue = -1;
var t = new Thread(() =>
{
openValue = OpenCommunication();
}, 4194304);
t.Start();
t.Join();
return openValue;
}
private static int OpenCommunication()
{
return open();
}
public void SubscribeToEvents(OnEventCallback func)
{
callback = func;
subscribeToEvents(OnEvent);
}
}
I tested it again with console application and it worked, callback worked as expected.
In IIS hosted application, I stopped getting stackoverflow exception and same value as in console applicatrion is returned from OpenCommunication() method. Unfortunately, my callback function isn't invoked as it is in console application.
Any suggestions?