c# - 从 C# 调用包含函数指针的 DLL 函数

标签 c# c++ dll function-pointers

我有一个用 C++ 编写的 DLL,其中包含导出的函数,该函数具有用作回调函数的函数指针。

// C++ 
DllExport unsigned int DllFunctionPointer( unsigned int i, unsigned int (*TimesThree)( unsigned int number ) ) {
    return TimesThree( i )  ; 
}

我有一个 CSharp 应用程序,我想用它来调用 DLL 函数。

// C#
public unsafe delegate System.UInt32 CallBack( System.UInt32 number ); 
class Program
{
    [DllImport("SimpleDLL.dll")]
    public static extern System.UInt32 DllFunctionPointer( System.UInt32 i, CallBack cb) ;

    static unsafe void Main(string[] args)
    {
        System.UInt32 j = 3;
        System.UInt32 jRet = DllFunctionPointer(j, CallBack );
        System.Console.WriteLine("j={0}, jRet={1}", j, jRet); 
    }

    static System.UInt32 CallBack( System.UInt32 number ) {
        return number * 3 ; 
    }
}

上述代码的问题在于应用程序崩溃并显示以下错误消息。

'CallingACallbackFromADLL.vshost.exe' (Managed (v4.0.30319)): Loaded 'C:\XXXX\CallingACallbackFromADLL.exe', Symbols loaded.
Managed Debugging Assistant 'PInvokeStackImbalance' has detected a problem in 'C:\XXXX\CallingACallbackFromADLL.vshost.exe'.
Additional Information: A call to PInvoke function 'CallingACallbackFromADLL!CallingACallbackFromADLL.Program::DllFunction' has unbalanced the stack. This is likely because the managed PInvoke signature does not match the unmanaged target signature. Check that the calling convention and parameters of the PInvoke signature match the target unmanaged signature.

The program '[9136] CallingACallbackFromADLL.vshost.exe: Managed (v4.0.30319)' has exited with code 1073741855 (0x4000001f).

我不确定下一步该做什么。

我的问题是:

  • 从 C# 应用程序调用包含回调指针的 C++ DLL 函数的正确方法是什么。

最佳答案

这是因为在 C# 中函数的默认调用约定是 __stdcall 但在 C/C++ 中默认是 __cdecl,所以你应该改变你的函数的调用约定如下:

[UnmanagedFunctionPointer(CallingConvention.Cdecl)]
public delegate void TimesTree( uint frame );
[DllImport("SimpleDLL.dll")]
public static extern System.UInt32 DllFunctionPointer( uint i,
    [MarshalAs(UnmanagedType.FunctionPtr)] TimesTree callback ) ;

static unsafe void Main(string[] args)
{
    // ...
    System.UInt32 jRet = DllFunctionPointer(j, CallBack );
    // ...
}

关于c# - 从 C# 调用包含函数指针的 DLL 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13280323/

相关文章:

c# - 如何 - 将一个整数拆分为两个整数?

c++ - 模块间共享内存

c++ - 检查位标志

c# - 为什么 OrderByDescending 不被识别为构建方法

java - 将 Long 转换为 DateTime 从 C# 日期到 Java 日期

c# - 可重复使用的 MVC 代码 : handling value types in linq expressions

c++ - 错误代码 C2451 无法运行基本程序

c++ - dll 注入(inject)失败

c++ - 在 C++ 项目中使用 C DLL

c# - 从 .NET 2.0 应用程序调用 .NET 4.0 WPF 类库