C# 发生了 System.AccessViolationException
高新娥 2010-04-09 12:03:56 在C#中调用c++写的dll,而dll里面有一个加密函数指针和一个解密函数指针
我在c#中的代码如下
[DllImport("Kernel32.dll")]
//加载dll
public static extern IntPtr LoadLibrary(string dllfile);
[DllImport("Kernel32.dll")]
//获取dll的函数指针
public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport("kernel32.dll", EntryPoint = "FreeLibrary", SetLastError = true)]
//释放dll
public static extern bool FreeLibrary(IntPtr hModule);
//定义委托
public delegate int encry(string gy,string sy,string zz, ref string dd);
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
IntPtr hinst = LoadLibrary("IEncryptionandDecryption.dll");
IntPtr proc = GetProcAddress(hinst, "Encryption");
encry en = Marshal.GetDelegateForFunctionPointer(proc, typeof(encry)) as encry;
string username = "8596586";
en("fakdkafakfdansf", "dkkdkdlfslkfs", "fsfasfsafa", ref username);
FreeLibrary(hinst);
}
但是一运行就会报 发生了 System.AccessViolationException 尝试读取或写入受保护的内存。这通常指示其他内存已损坏。这个错误,这是什么原因啊?