文本参数 涉及非托管内存操作,给出两种解决方案:
1,在易语言dll(被调用者)中申请内存,返回内存指针和长度,在C#(调用者)中取出文本并释放内存资源;
易语言dll函数:
相对应C#调用函数,需要引用命名空间:using System.Runtime.InteropServices;
[DllImport("91vpn.dll")]
public static extern bool AutoConnect(ref int msg,ref int bufferlen);
private void buttonConnect_Click(object sender, EventArgs e)
{
string msg = null;
int bufferlen = 0;
int buffer = 0;
bool ret = _91vpn.AutoConnect(ref buffer, ref bufferlen);
IntPtr bufferptr = new IntPtr(buffer);
msg = Marshal.PtrToStringAnsi(bufferptr,bufferlen);
Marshal.FreeHGlobal(bufferptr);
MessageBox.Show(msg);
}
2,在C#(调用者)中申请非托管内存传递给dll,在易语言dll(被调用者)中 使用 写入内存()函数,将文本写入内存,在C#(调用者)中取出文本并释放内存资源;
易语言dll函数:
相对应C#调用函数,需要引用命名空间:using System.Runtime.InteropServices;
[DllImport("91vpn.dll")]
public static extern bool AutoConnect(IntPtr msg, int bufferlen);
private void buttonConnect_Click(object sender, EventArgs e)
{
string msg = null;
int bufferlen = 1024;
IntPtr buffer = Marshal.AllocHGlobal(bufferlen);
bool ret = _91vpn.AutoConnect(buffer, bufferlen);
msg = Marshal.PtrToStringAnsi(buffer);
Marshal.FreeHGlobal(buffer);
MessageBox.Show(msg);
}