I have an assembly which calls a native method marked with the
SuppressUnmanagedCodeSecurity attribute (the security implications
of which are not the intended topic of this post). The assembly is
compiled using .NET 1.0 but intended to be used from client
applications running on arbitrary .NET runtimes.
I am fairly confident that both the usage and the P/Invoke
signature of the method is correct. In fact, everything
works fine if any of the following holds:
o The assembly is compiled in debug mode, OR
o The SuppressUnmanagedCodeSecurity attribute is omitted, OR
o The test applications runs with .NET 2.0 or Mono, OR
o A debugger is attached (argh!)
If the release version of the assembly is compiled with
the SuppressUnmanagedCodeSecurity attribute, and the test
application is started from the command line with .NET 1.0
or .NET 1.1, I get a NullReferenceException.
From test outputs in the assembly and the native DLL I know
that the problem occurs on the line where the native method
should be called, and that the code in the native method is
*not* entered. So, the error would seem to be raised from
within code of the runtime. Unfortunately, I was not able
to reproduce this with smaller test projects.
Did anyone else experience a similar behavior?
Any insights would be appreciated.
Christian Fröschlin - 04 Sep 2006 16:28 GMT
> I am fairly confident that both the usage and the P/Invoke
> signature of the method is correct.
Fairly confident obviously was not enough. The problem
turned out to be a calling convention mismatch between
StdCall and cdecl. What really surprised me though is
how far you can get without ever seeing a problem.
I suppose this means that the .NET runtime routinely uses
stack frames even for release builds, but .NET 1.x disables
them when SuppressUnmanagedCodeSecurity is used (even when
the /optimize flag is not specified when compiling).