【问题标题】:c# COM object & "The value of ESP was not properly saved across a function call..."c# COM 对象和“ESP 的值未在函数调用中正确保存...”
【发布时间】:2012-04-06 22:25:41
【问题描述】:

我在 c# 上有一个 COM 对象,我在 c++ dll 中使用它。

在 c++ 中,COM 对象是用#import derective 导入的

问题是,当从 c++ dll 调用 COM 对象的任何函数时,我会遇到运行时检查错误:

运行时检查失败 #0 - ESP 的值未正确保存 通过函数调用。这通常是调用 用一个调用约定和一个函数指针声明的函数 使用不同的调用约定声明。

在c++ import指令生成的com对象包装器中,所有函数都声明为__stdcall

【问题讨论】:

  • 这是 COM 调用的一个非常不寻常的问题,调用约定和参数类型都是机器生成的。您需要更好地描述您的代码,至少发布 C# 声明和 C++ 调用的 sn-ps。简单的解释是您使用的是旧版本的类型库。
  • 看起来我找到了解决方案:我已经为 c# COM 中的所有方法和参数声明了 In/Out/MarshalAs 属性

标签: c# c++ com


【解决方案1】:

好的,看来我找到了解决方案: 我已经为 C# COM 接口中的所有方法声明了 In/Out/MarshalAs 属性:

[Guid("EEB4C1AE-4DB2-4bdb-86D4-A429B27496A3")]
public interface IAXFarCards
{
    [DispId(1)]
    void InitDbConnection([In, MarshalAs(UnmanagedType.BStr)] string connectionString);

    [DispId(2)]
    [return: MarshalAs(UnmanagedType.I4)]
    int GetCardInfo(
        [In, MarshalAs(UnmanagedType.BStr)]             string card,
        [In, MarshalAs(UnmanagedType.VariantBool)]      bool   isTemplate,
        [In, MarshalAs(UnmanagedType.I4)]               int    cashDeskId,
        [Out, MarshalAs(UnmanagedType.VariantBool)] out bool   isActive,
        [Out, MarshalAs(UnmanagedType.I4)]          out int    discountNumber,
        [Out, MarshalAs(UnmanagedType.I8)]          out Int64  amount,
        [In, Out, MarshalAs(UnmanagedType.BStr)]    ref string ownerName,
        [In, Out, MarshalAs(UnmanagedType.I4)]      ref int    clientId
        );

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-09-06
    • 2013-11-30
    • 2011-09-17
    • 2013-07-03
    • 1970-01-01
    • 2018-11-28
    • 2012-04-22
    相关资源
    最近更新 更多