1
我要瘋了。DLLImport get嘗試讀取或寫入受保護的內存
我有一個DLL,使用此功能:
function MyFunc(myId: integer; var LstCB: array of char): integer; stdcall;
第一個參數是一個貧窮的整數。 但第二個是一個char [2048]這得到成才這樣
('9', #13, #10, '8', '8', '8', '8', '0', '0', '0', '0', '0', '0', '0', '0', '2', '5', '0', '7', #13, #10, '8', '8', '8', '8', '0', '0', '0', '0', '0', '0', '0', '0', '2', '6', '0', #13, #10, '8', '8', '8', '8', '0', '0', '0', '0', '0', '0', '0', '0', '3', '3', '1', '5', #13, #10, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0, #0,....#0)
我今年進口用的DllImport:
[DllImport(@"MyDll.dll", EntryPoint = "MyFunc", CallingConvention = CallingConvention.StdCall)]
internal static extern int MyFunc(int myId, string list);
我也得到:
Attempted to read or write protected memory. This is often an indication that other memory has been corrupted.
你有一些想法請?
謝謝。
爲了闡明,這個失敗的原因是它不平衡堆棧。 「字符數組」(array.Length字節)的大小與C#字符串指針(4/8字節)不一樣,所以參數會污染堆棧並導致未定義的行爲。 – Polynomial
謝謝你,這麼多,我會試試這個。 – Tkanos