DllImport 堆栈溢出异常

发布于 2024-10-15 13:26:30 字数 1432 浏览 8 评论 0原文

我正在尝试使用 C# 中的 C++ DLL(borland c builder)。函数 writeParameter 工作正常,它将正确的数据写入文件,但随后出现异常“PresentationFramework.dll 中发生了类型为‘System.StackOverflowException’的未处理异常”

C++ 代码:

#include <vcl.h>
#include <windows.h>
#include <fstream.h>
#pragma hdrstop
#pragma argsused
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
  return 1;
}
//---------------------------------------------------------------------------
#pragma pack (push,1)
typedef struct     
{
     int     a;
}ABC;
#pragma pack (pop)
//---------------------------------------------------------------------------

extern "C" void __declspec(dllexport) __cdecl writeParameter(ABC *abc)
{
   ofstream outfile("result.txt");
   outfile<< "A="   <<endl;
   outfile << abc->a <<endl;
   outfile.close();
}

c#:

[StructLayoutAttribute(LayoutKind.Sequential)]
public class ABC
{
    public int a;
}
[DllImport("D:\\monitorVC.dll", EntryPoint = "_writeParameter", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern void WriteParameter( 
            [In,MarshalAs(UnmanagedType.LPStruct)]
            ABC abc
            );

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
                var abc = new ABC() {a = 123};
                WriteParameter(abc);
        }

I'm trying to use C++ DLL(borland c builder) from c#. Function writeParameter works fine, it writes correct data to a file, but then i have an exception "An unhandled exception of type 'System.StackOverflowException' occurred in PresentationFramework.dll"

C++ code:

#include <vcl.h>
#include <windows.h>
#include <fstream.h>
#pragma hdrstop
#pragma argsused
BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fwdreason, LPVOID lpvReserved)
{
  return 1;
}
//---------------------------------------------------------------------------
#pragma pack (push,1)
typedef struct     
{
     int     a;
}ABC;
#pragma pack (pop)
//---------------------------------------------------------------------------

extern "C" void __declspec(dllexport) __cdecl writeParameter(ABC *abc)
{
   ofstream outfile("result.txt");
   outfile<< "A="   <<endl;
   outfile << abc->a <<endl;
   outfile.close();
}

c#:

[StructLayoutAttribute(LayoutKind.Sequential)]
public class ABC
{
    public int a;
}
[DllImport("D:\\monitorVC.dll", EntryPoint = "_writeParameter", CallingConvention = CallingConvention.Cdecl, CharSet = CharSet.Ansi)]
        public static extern void WriteParameter( 
            [In,MarshalAs(UnmanagedType.LPStruct)]
            ABC abc
            );

        private void Grid_Loaded(object sender, RoutedEventArgs e)
        {
                var abc = new ABC() {a = 123};
                WriteParameter(abc);
        }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

小红帽 2024-10-22 13:26:31

阅读这篇关于选择不当的博客文章 UnmanagedType.LPStruct 的名称以及它如何不做每个人认为它做的事情。像这样修正你的声明:

[DllImport(...)]
public static extern void WriteParameter([In] ref ABC abc);

Read this blog post about the very poorly chosen name for UnmanagedType.LPStruct and how it does not do what everybody thinks it does. Fix your declaration like this:

[DllImport(...)]
public static extern void WriteParameter([In] ref ABC abc);
筱果果 2024-10-22 13:26:31

C# 中的类 != 结构体。

此外,C# 版本和 C++ 版本之间的结构打包也不同。

class != struct in C#.

Also, the packing of your structure is not the same between the C# version and the C++ version.

幻想少年梦 2024-10-22 13:26:31

我知道这个问题发布已经有一段时间了,但我在尝试加载到 VS2010 C# 项目(一个使用 CodeGear C++ Builder 2007 构建的 .dll)时也有同样的经历。

解决方法是从我的 .dll 中删除所有 TForms 。在我看来,从这些表单导出的符号(顺便说一下,我无法删除)“导致”加载器堆栈溢出。

问候。

I know that it's been a while since this question was posted but I had the same experience trying to load to a VS2010 C# project, a .dll built with CodeGear C++ Builder 2007.

The work-around was to remove all TForms from my .dll. It seemed to me that the exported symbols from these forms (which by the way I could not remove) where "leading" the loader to a stack overflow.

Regards.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文