C# 嘗試讀取或寫入受保護的內存 。這通常指示其他內存已損壞。


在C#中調用別人的DLL的時候有時候出現 嘗試讀取或寫入受保護的內存 。這通常指示其他內存已損壞。

在傳值的時候還是用指針,再在C#中做轉換就好了。

解決辦法:

 

[DllImport("APPLISTCC.dll")]
public static extern string TestFunc1(string param1);

string ret1 = TestFunc1("text");

 

改成:

 

[DllImport("APPLISTCC.dll")]
public static extern IntPtr TestFunc1(IntPtr par1);

IntPtr ptrIn = Marshal.StringToHGlobalAnsi("text");
IntPtr ptrRet = TestFunc1(ptrIn);
string retlust = Marshal.PtrToStringAnsi(ptrRet);

 

 

 

自己在程序里強制釋放COM資源,調用Marshal.ReleaseComObject()方法將不再使用的對象釋放掉

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM