I've done plenty of managed/unmanaged development/debugging before but this is the 1st time I ran into this annoying problem. I'm using VS 2010 SP1 on Windows 7 64-bit with all latest OS patches.
Basically, I have a simple struct and a simple unmanaged function declaration like this:
typedef struct
{
char* Value1;
int Value2;
} MyStruct;
unsigned long long Foo(const MyStruct* someStruct, const char* name);The C# side looks like this:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MyStruct
{
public string Value1;
public int Value2;
}
[DllImport("some.dll", SetLastError = true, CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
public static extern long Foo(ref MyStruct someStruct, string name);Here is the problem: When I try to step into the unmanaged code when calling it from managed code, I get one of two exceptions: "An unhandled exception of type 'System.Runtime.InteropServices.SEHException' occurred in myApp.exe" or "System.AccessViolationException".
This looks like what you will get from a calling convention mismatch but I tried all 3 and none of them work. The strange thing is that if I step into the unmanaged function (and stop at the opening curly brace), the contents of someStruct is perfectly valid, as well as the value of name. However as soon as I step the next line (before even executing that next line), I get the exception.
The other strange thing is that when I step over the call, it sometimes completes successfully with no problem at all. If I had any declarations wrong it would always fail, not fail sometimes.
Does anyone have an idea what is going on?
Add your 2¢