【问题标题】:C++ DLL Called From C# on Windows CE for ARM Always Returns 0在 Windows CE for ARM 上从 C# 调用的 C++ DLL 始终返回 0
【发布时间】:2009-07-02 22:24:32
【问题描述】:

我目前正在 TI OMAP 处理器(一个 ARM 处理器)上开发适用于 Windows CE 的应用程序。我试图简单地从 C# 调用 C++ DLL 文件中的函数,并且无论我使用哪种数据类型,我总是得到一个 0 的值。这很可能是某种调用约定不匹配吗?我正在从同一个 Visual Studio 解决方案编译 DLL 和主 EXE。

C# 代码片段:

public partial class Form1 : Form
{
    private void button1_Click(object sender, EventArgs e)
    {
        byte test = LibWrap.test_return();
        MessageBox.Show(test.ToString());
    }
}

public class LibWrap
{
    [DllImport("Test_CE.dll")]
    public static extern byte test_return();
}

C++ DLL 代码片段:

extern "C" __declspec (dllexport) unsigned char test_return() {
    return 95;
}

【问题讨论】:

  • 刚刚在 Windows Mobile 2005 R2 Emulator 上测试过并且运行良好。

标签: c# c++ interop windows-ce arm


【解决方案1】:

当我改变时它起作用了:

extern "C" __declspec (dllexport) unsigned char test_return() {
    return 95;
}

extern "C" __declspec (dllexport) unsigned char __cdecl test_return() {
    return 95;
}

在 DLL 代码中。为什么它在为 WinCE 编译时不假设这一点超出了我的理解。

【讨论】:

  • Ben:可能是项目设置问题。默认的 VS2008 Windows Mobile 项目设置对我来说效果很好。
【解决方案2】:

尝试如下导出 test_return():

unsigned char __declspec(dllexport) WINAPI test_return() {
   return 95;
}

【讨论】:

  • 某处 WINAPI 被定义为 __stdcall 应该是 __cdecl
【解决方案3】:

某处 WINAPI 被定义为 __stdcall 应该是 __cdecl

没有。 WINAPI 定义为 __stdcall。

【讨论】:

  • 这应该是 Ben 评论下方的评论,而不是新答案。
猜你喜欢
  • 1970-01-01
  • 2011-02-06
  • 2013-06-28
  • 2021-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-31
  • 2021-06-16
相关资源
最近更新 更多