Reputation: 6101
I'll try to describe my problem as detailed as possible, but if more detailed explanation is needed, please let me know.
To simplify let say I have 3 DLLs (actually I have more, but it is not very important, I guess):
managed-1.dll
- Managed DLL (writen in C# .NET 4.0) - handles requests and during some requests invokes unmanaged code in second DLL
unmanaged.dll
- Unmanaged DLL (writen in old school VC++ 6.0) - performs several operations and sometimes calls the third DLL
managed-2.dll
- Managed DLL (writen in CLI/C++ .NET 3.5) - the root of my problem
I run my code in 3 different scenarios:
I call managed-1.dll
from console application - everything works well
I call managed-1.dll
from ASP.NET Development Server
- everything works well too
I call managed-1.dll
from IIS
- everything works well until whole sequence managed-1.dll -> unmanaged.dll -> managed-2.dll
is involved.
In scenario 3 the StackOverflowException is thrown. The debugger shows that no recursion is involved. Also it is clear that the exception is occured in the following type of call stack:
managed-1.dll::CallUnmanagedCode()
unmanaged.dll::SomeMethod1()
unmanaged.dll::SomeMethod2()
unmanaged.dll::CallManagedCode()
managed-2.dll::CallUnmanagedCode()
!! marked with __declspec(dllexport)
and does not use any managed types !!managed-2.dll::FailingMethod()
!! uses managed types; in a very beginning (even the first line of code is not performed) an exception is occured !!The one more interesting thing: the debugger shows not the same parameter values in the FailingMethod
as compared to the values in the method calling point.
If somebody has any clue, please advice.
Solution: The problem was not related to the managed-unmanaged stuff, but to the IIS stack size. For me the usage of the editbin tool was not acceptable solution. So my solution - to create new thread before call of the unmanaged.dll
and set stack to 1 MB:
var result = unchecked ((int)0x800000FF);
var thread = new Thread(() => { result = pinvoke_func(); }, 1024 * 1024); // 1MB
thread.Start();
thread.Join();
Upvotes: 4
Views: 906
Reputation: 4472
The stack size for normal windows applications is apparently 1MB, but for IIS it is 256KB (see http://support.microsoft.com/kb/932909). This would perhaps explain why the application behaves in the console application but not in IIS.
I don't know exactly what the stack size is for the ASP.NET development server but it's probably 1MB, see this SO question for more information: stackoverflowexception-in-iis7-but-not-in-cassini
Apparently it's possible to increase the stack size for IIS using editbin.
See this article for instructions: Stack sizes in IIS - affects ASP.NET
Upvotes: 4