c#的dll问题

来源:百度知道 编辑:UC知道 时间:2024/06/17 10:00:37
long fnSendMsg(long nPortNo,char* szHeader,char* szMsg)
其中第一个是传入参数 ,后两个是传出参数
帮我写一下C#的怎么用 谢谢
下面是我写的
int comNo;
string szHeader = "";
string szMsg = "";
int nRc;

nRc = fnReadMsgEx(comNo, ref szHeader, ref szMsg);
然后用Timer 不停的调用就出现下面的错误
FatalExecutionEngineError
The runtime has encountered a fatal error. The address of the error was at 0x79e71bd7, on thread 0xf70. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.

long fnSendMsg(long nPortNo,ref string szHeader,ref string szMsg)
{
long tmp=nPortNo;
szHeader=getHead();
szMsg=getMsg();
return tmp+1;
}
C# 里有 string 不需要用 char* 或者char[] 赋值字符串了
--------------------
我明白了 你是要用这个函数 不是要写这个函数...
封装一下
using System.Runtime.InteropServices;

[DllImport("你的dll.dll", CharSet = CharSet.Auto)]
public static extern long fnSendMsg(long nPortNo,string szHeader,string szMsg);

调用封装好的

long fnSendMsg(long nPortNo,ref char szHeader,ref char szMsg)

或者:long fnSendMsg(long nPortNo,out char szHeader,out char szMsg)

对,dllimport的时候,参数改成string

必须将ref改为out