【发布时间】:2016-06-14 10:48:23
【问题描述】:
我遇到了一个关于从管理不善的 C++ dll 导出的函数的奇怪问题,我在 C# 代码中使用该函数:无论我在 C++ 中返回什么,在 C# 中接收到的返回布尔值始终为真。我缩小了范围,得到了一个包含以下代码的 C++ 文件:
#ifdef __cplusplus
extern "C" {
#endif
__declspec(dllexport) bool init()
{
return false;
}
#ifdef __cplusplus
}
#endif
我将它构建成一个 dll 并在 C# 中导入函数:
using System;
using System.Runtime.InteropServices;
namespace Test
{
class TestDll
{
[DllImport( "dlltest_d" )]
public static extern bool init();
}
class Program
{
static void Main( string[] args )
{
if( !TestDll.init() )
{
Console.WriteLine( "init failed" );
return;
}
Console.WriteLine( "init succeeded" );
}
}
}
当我运行它时,我得到以下输出:
初始化成功
我很困惑。有什么想法吗?
【问题讨论】:
标签: c# c++ dllimport dllexport