C# 调用C++动态库异常闪退解决

因为牵涉到安全性,所以只放关键部分代码

//申明DLL加载方法
[DllImport("*.dll", CallingConvention = CallingConvention.StdCall)]
public static extern int business_handle(IntPtr inputvalue, int outputlen, [MarshalAs(UnmanagedType.LPStr)]StringBuilder outputdata, [MarshalAs(UnmanagedType.LPStr)]StringBuilder errmsg);

//DLL实现方法
[HandleProcessCorruptedStateExceptions]
public void Businesshandle(string businessNo, string inputvalue, int outputlen, ref string outdata)
        {
            var outputdata = new StringBuilder(outputlen);
            var errmsg = new StringBuilder(4000);
            try
            {
                IntPtr ptrinputvalue = Marshal.StringToHGlobalAnsi(inputvalue);
                var result = business_handle(ptrinputvalue, outputlen, outputdata, errmsg);

                Marshal.FreeHGlobal(ptrinputvalue);
            }
            catch (Exception ex)
            {
            }
        }

 

你可能感兴趣的:(C#,C#调用C++动态库闪退)