Why isn't PInvoke crashing in case of violated calling convention (in .NET 3.5)? -
my solution has unmanaged c++ dll, exports function, , managed application pinvokes function.
i've converted solution .net 3.5 .net 4.0 , got pinvokestackimbalance "a call pinvoke function [...] has unbalanced stack" exception. turned out, calling __cdecl'ed function, __stdcall:
c++ part (callee):
__declspec(dllexport) double testfunction(int param1, int param2); // default __cdecl
c# part (caller):
[dllimport("testlib.dll")] // default callingconvention.stdcall private static extern double testfunction(int param1, int param2);
so, i've fixed bug, i'm interested in how did work in .net 3.5? why (many times repeated) situation when nobody (neither callee, nor caller) cleans stack, did not caused stack overflow or other misbehavior, worked ok? there sort of check in pinvoke, mentioned raymond chen in article? it's interesting, why opposite type of breaking convention (having __stdcall callee pinvoked being __cdecl) not working @ all, causing entrypointnotfoundexception.
pinvokestackimbalance not exception. mda warning, implemented managed debugging assistant. having mda active optional, can configure debug + exceptions dialog. never active when run without debugger.
getting stack imbalanced can cause pretty nasty problems, ranging strange data corruption getting soe or ave. hard diagnose too. can cause no trouble @ all, stack pointer gets restored when method returns.
code compiled 64-bit tends resilient, lot more of function arguments passed through registers instead of stack. fail when forced run on x86, new default vs2010.
Comments
Post a Comment