c# - WebApp 中的 Dllimport System.StackOverflowException,控制台工作正常

标签 c# c++ .net dll dllimport

我很困惑,一直在四处寻找很长一段时间。 我正在从 C# 调用 C++ DLL 我可以在一个简单的控制台应用程序中使它一切正常,但相同的代码在 WebApp 中不起作用

我使用的是 windows 10 64 位,但我强制一切都使用 32 位并且调用是 Cdecl

C#代码:

namespace WebApp
{
  public class LibWrap
  {
    [DllImport("mdll.dll", ExactSpelling = true, CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Unicode)]
    public static extern Int32 MySize(Int64 start,
      Int32 count,
      Int64[] from,
      Int64[] to,
      Int32[] ids,
      ref Int32 id_cnt);
  }

    public partial class Default : System.Web.UI.Page
    {
        protected void Button1_Click(object sender, EventArgs e)
        {
          Int64 l_start = 1431644400;
          Int64[] l_from = {
                                1431644400,
                                1432114200,
                                1432162800,
                                1432278000,
                                1432335600
                            };
          Int64[] l_to = {
                                1431673200,
                                1432119600,
                                1432200600,
                                1432288800,
                                1432364400
                            };
          Int32[] l_IDs = {
                                1,
                                1,
                                1,
                                1,
                                1
                            };
          int l_idCnt = 0;
          Int32 l_Count = LibWrap.MySize(l_start, l_from.Length, l_from, l_to, l_IDs, ref l_idCnt);
        }
    }
}

被调用的DLL头代码是:

#define DEF_EXPORT   __declspec(dllexport) __cdecl

int32_t DEF_EXPORT MySize(int64_t   start,
                          int32_t   count,
                          int64_t * from,
                          int64_t * to,
                          int32_t * ids,
                          int32_t & id_cnt);

C++ 源代码:

int32_t DEF_EXPORT MySize(int64_t   start,
                          int32_t   count,
                          int64_t * from,
                          int64_t * to,
                          int32_t * ids,
                          int32_t & id_cnt)
{
  MyCore mycore;

  int32_t sz = 0;

  if (count)
  {
    mycore.Count = count;

    mycore.Ids    = ids;
    mycore.From   = from;
    mycore.To     = to;

    mycore.Start = start;

    IDList id_positions[MY_LIMIT];
    id_cnt = 1;

    sz = mycore.OutputSize(id_positions, id_cnt);

  }
  return sz;
}

正如我所说,WebApp 在尝试调用 MySize 时抛出一个 System.StackOverflowException 错误。 使用 native 代码调试 System.StackOverflowException 我一进入 MySize 函数(即在第一行运行之前) 感谢任何见解,我在网上找到的每个解决方案似乎都已包含在我的案例中。

最佳答案

好的,这是提供给控制台应用程序与 Web 应用程序的堆栈内存问题 在 32 位下,Web 应用程序只能获得 256Kb。

罪魁祸首是数组:

IDList id_positions[MY_LIMIT];

将其移至动态分配将其从堆栈中移出并解决问题。

关于c# - WebApp 中的 Dllimport System.StackOverflowException,控制台工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36002291/

相关文章:

c# - 使用 RNGCryptoServiceProvider 生成随机字符串

c# - 奇怪的 String.Format() - 输入字符串的格式不正确

c++ - 重载 operator<< 和 operator!=

.net - (为什么).Net 中的反射如此昂贵?

c# - IPAddress 上的奇怪 .Net 行为等于

c# - 一步一步的 oAuth rest C# winform 示例

c# - 将 WPF Combobox 的 SelectedItem 转换为 Color 会导致异常

c++ - 将 Unicode 字符串写入文件

c++ - 对元素范围为 0 到 9999 的数组进行排序

.net - 如何在 IronPython 中实现接口(interface)?