PInvoke calling convention mismatch

By default, the unmanaged C++ compiler generates functions that are _cdecl, but .NET PInvoke requires _stdcall. This causes obvious stack corruption issues and hard-to-find bugs.

Ideally, I would have thought that the .NET runtime could detect that the stack pointer changed after making a PInvoke call, and could have thrown an exception right there. But, since it doesn't, an FxCop rule seems like a good second choice.

It doesn't appear that there is such a rule, I'm hoping that it's possible to either write one myself or better yet have Microsoft supply one.
I already have a not-very-robust rule that simply checks if there is an @ symbol somewhere in the unmanaged function name. Any ideas for a more robust way of checking?

Thanks!

[762 byte] By [NathanB] at [2008-2-23]
# 1
There's a customer debug probe that catches this problem at runtime, see:

http://blogs.msdn.com/adam_nathan/archive/2003/05/21/56690.aspx

To catch it statically, we'd need to be able to retrieve the calling convention information from PDBs (of native functions) and compare this to the managed DllImport declaration. I'll ask our team's pdb expert whether this is possible.

Michael

MichaelFanning-MS at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...
# 2
This information is definitely in the PDB and we could implement the check statically. It's an excellent suggestion and we'll get to it eventually.

Thanks!
Nick

NickGuerrera-MS at 2007-9-9 > top of Msdn Tech,Visual Studio Team System,Visual Studio Code Analysis and Code Metrics...

Visual Studio Team System

Site Classified