嘿! 我剛開始擺弄pinvoke並遇到問題。我得到了AccessViolationException。首先,有什麼方法可以調試或追蹤哪個字段導致此錯誤?唯一寫入的是結果結構體。Pinvoke結構編組幫助需要 - System.AccessViolationException
C++的呼叫看起來像:
MyFunc(int var1, _tuchar *var2, _tuchar *var3, _tuchar *var4, MyStruct *Result,
_tuchar *var5, _tuchar *var6);
的C++結構:
typedef struct MyStruct
{
_tuchar *id;
_tuchar *ErrorMessages;
int int1;
_tuchar language[3];
_tuchar *result;
int type;
int number;
int *type2;
_tuchar **blocks;
}
C#的結構:
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct MyStruct
{
[MarshalAs(UnmanagedType.LPStr)]
public string Id;
[MarshalAs(UnmanagedType.ByValArray, SizeConst=500)]
public char[] ErrorMessages;
public int int1;
[MarshalAs(UnmanagedType.LPStr)]
public string language;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 15)]
public char[] result;
public int type;
public int number;
public int type2;
[MarshalAs(UnmanagedType.ByValArray)]
public string[] blocks;
C#的方法聲明:
[DllImport(MyPath, EntryPoint = "MyEntryPoint", SetLastError = true,
CharSet = CharSet.Unicode)]
internal static extern int MyFunc(int var1, string var2, string var3,
string var4, ref MyStruct Result, string var5, string var6);
C#的召喚:
var result = new MyStruct();
MyFunc(0, "var2", "var3", "var4", ref result, "var5", "var6");
希望我什麼都不會被排除在外。 感謝您的幫助!
「MyFunc」聲明在C#中是什麼樣的? – Justin 2011-04-28 05:18:01
啊我忘了一些東西,謝謝隊友^^ – Dashu 2011-04-28 05:21:54
在C++中'int'的大小保證是32位/可以用System.Int32轉換嗎? System.Int32有一個非常精確的保證 - 即使是跨性別的。 – 2011-04-28 05:47:54